我如何合并多个 xslt 文件和 xml 文件以相互输入以获得最终输出? [英] how do i merge multiple xslt files and xml files to input as one another to get the final output?

查看:24
本文介绍了我如何合并多个 xslt 文件和 xml 文件以相互输入以获得最终输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个 xsl 文件和一个输入文件:

I have three xsl files and one input file:

-input.xml process1.xsl output1.xml
-output1.xml process2.xsl output3.xml

现在我只想把它作为:

input.xml process.xslt output.xml

process1.xsl、process2.xsl 及其输出应作为输入到 xsl 文件并在同一个 process.xsl 文件中生成 output.xml.

process1.xsl, process2.xsl and their outputs should passed as the input to the xsl file and generate output.xml in the same process.xsl file.

我如何在 xslt 中执行此操作,我已经提到了 xslt 应用导入,但我没有得到将 xml 输出分配为另一个 xsl 文件的输入的正确参考,所有这些都在一个 xsl 中.有人可以帮我吗?

how do i do this in xslt , i have referred to xslt apply imports but i am not getting proper reference for assigning the xml output as input for another xsl file all in one xsl.. can anyone help me out?

这里我调用了 input.xml 并使用了 process1.xsl 作为第一步生成的输出存储在 $content 变量中,现在我被困在这里,如何导入 process2.xsl 并将其分配给变量 $content 中的前一个输出,我只能显示它的输出我想分配它到下一个 xsl 文件:

here i have invoked the input.xml and used process1.xsl for first step and the output generated is stored in the $content variable now i am stuck here that how do i import the process2.xsl and assing it to the previous output in variable $content, i am just able to display its output i want to assing it to next xsl file:

<xsl:import href="process1.xsl"/>

<xsl:output method="xml" indent="yes"/>



<xsl:template match="/">

 <xsl:variable name="content">
    <xsl:apply-imports/>
 </xsl:variable>

<xsl:apply-templates select="exsl:node-set($content)/*" mode="m"/>
</xsl:template>

<xsl:template match="@*|*|text()" mode="m">

<xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="@*|*|text()" mode="m"/>
</xsl:copy>

</xsl:template>

类似的东西,但这不起作用?

something like this but this does not work?

推荐答案

使用 XSLT 3.0 你可以使用 fold-left 函数 (https://www.w3.org/TR/xpath-functions-31/#func-fold-left) 与 transform 函数(https://www.w3.org/TR/xpath-functions-31/#func-transform) 链接转换:

Using XSLT 3.0 you could use the fold-left function (https://www.w3.org/TR/xpath-functions-31/#func-fold-left) together with the transform function (https://www.w3.org/TR/xpath-functions-31/#func-transform) to chain the transformations:

<xsl:stylesheet
 version="3.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:fn="http://www.w3.org/2005/xpath-functions"
 exclude-result-prefixes="fn xs">

 <xsl:param name="sheet-uris" as="xs:string*" select="'process1.xsl', 'process2.xsl'"/>

 <xsl:param name="input-uri" as="xs:string" select="'input.xml'"/>
 <xsl:param name="input-doc" as="document-node()" select="doc($input-uri)"/>

 <xsl:mode on-no-match="shallow-copy"/>

 <xsl:template name="xsl:initial-template">
    <xsl:apply-templates select="fold-left($sheet-uris, $input-doc, function($input, $sheet-uri) { transform(map { 'stylesheet-location' : $sheet-uri, 'source-node' : $input })?output })"/>
 </xsl:template>

 <xsl:template match="/*">
     <xsl:comment select="'Processed by ' || document-uri(document(''))"/>
     <xsl:next-match/>
 </xsl:template>

</xsl:stylesheet>

显然,在真正的转换中,您的样式表不仅会输出注释以指示其处理输入,还可以使用上面用 Saxon 9.7 EE 和 -it -xsl:process.xsl 调用的样式表,其中输入是

Obviously in a real transformation your stylesheet would do more than outputting a comment to indicate its processing the input but using above stylesheet called with Saxon 9.7 EE and -it -xsl:process.xsl, where the input is

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <foo>bar</foo>
</root>

和两个样式表 process1.xslprocess2.xsl 是例如

and the two stylesheets process1.xsl and process2.xsl are for instance

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="array fn map math xhtml xs">

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="/*">
        <xsl:comment select="'Processed by ' || document-uri(document(''))"/>
        <xsl:next-match/>
    </xsl:template>

</xsl:stylesheet>

输出是

<?xml version="1.0" encoding="UTF-8"?><!--Processed by file:/SomePath/process1.xsl--><!--Processed by file:/SomePath/process2.xsl--><!--Processed by file:/SomePath/process.xsl--><root>
    <foo>bar</foo>
</root>

所以链接正在工作.

除了将输入文档作为参数传递之外,您还可以将其提供为主要输入 -s 并将主样式表更改为

Instead of passing the input document as a parameter you could also provide it as the primary input -s and change the main stylesheet to

<xsl:stylesheet
 version="3.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:fn="http://www.w3.org/2005/xpath-functions"
 exclude-result-prefixes="fn xs">

 <xsl:param name="sheet-uris" as="xs:string*" select="'process1.xsl', 'process2.xsl'"/>

 <xsl:mode on-no-match="shallow-copy"/>

 <xsl:template match="/">
    <xsl:apply-templates select="fold-left($sheet-uris, ., function($input, $sheet-uri) { transform(map { 'stylesheet-location' : $sheet-uri, 'source-node' : $input })?output })/node()"/>
 </xsl:template>

 <xsl:template match="/*">
     <xsl:comment select="'Processed by ' || document-uri(document(''))"/>
     <xsl:next-match/>
 </xsl:template>

</xsl:stylesheet>

如果您想使用 Java 链接转换,请参阅 https://stackoverflow.com/a/35845231/252228举个例子.

If you want to chain transformations with Java then see https://stackoverflow.com/a/35845231/252228 for an example.

这篇关于我如何合并多个 xslt 文件和 xml 文件以相互输入以获得最终输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆