XSL - 调用模板和其他标签的匹配输出

问题描述:

我有以下问题。我正在尝试使用XSL-T转换XML文件。我有一个XSL文件是这样的:XSL - 调用模板和其他标签的匹配输出

<!-- here are some imports--> 
<xsl:import href="..."/> 

<!-- here is template--> 
<xsl:template match="..."> 
    <!-- here are some new tags to be added to the document --> 
</xsl:template> 

<!-- here is second template--> 
<xsl:template match="..."> 
    <!-- here are some new tags and template-calls from imported xsl documents, such as: --> 
    <xsl:call-template name="..."/> 
</xsl:template> 

<!-- here is the place, where I want to create a match for output from all previous lines... --> 
<!-- ... --> 

因此,给定的代码段注释显示在这个XSL文件会发生什么。我有多个导入和很多模板调用。不幸的是,我需要在所有给定行的输出中添加一些标记,并且必须在此文件中执行此操作。我宁愿使用另一个模板和匹配属性,但我怎么能做到这一点?

我无法编辑所有导入的文档。另外,我不想创建临时帮助文件。 XSL版本是1.0。

预先感谢您:)

+1

请显示确切的匹配模式和模板内容以及样例输入和您想要的结果。 –

+1

你的问题并不完全清楚。通常,XSLT样式表在XML输入上运行,而不是在它自己的输出上运行。如果要处理应用模板的结果,请在变量中应用,然后处理该变量。或者让第二个样式表处理由第一个样式表创建的文档。 –

当您申请转换到另一个转换的输出,这就是通常所说的管道。在XSLT中实现管道有两种主要技术:一种是针对每个转换使用一个单独的样式表,并使用某些外部技术(如Ant,XProc,ShellScript,Java应用程序或某种框架)将它们链接在一起如Coccoon。另一种方法是一个样式表,其中,典型的编码模式是内管道

<xsl:template match="/"> 
    <xsl:variable name="temp1"> 
    <xsl:apply-templates select="." mode="phase-1"/> 
    </xsl:variable> 
    <xsl:variable name="temp2"> 
    <xsl:apply-templates select="$temp1" mode="phase-2"/> 
    </xsl:variable> 
    <xsl:apply-templates select="$temp2" mode="phase-3"/> 
</xsl:template> 

的多样式的方法具有两个优点:(a)的代码是更加模块化,因此更可重复使用的,和(b)上述代码在XSLT 1.0中实际上不起作用,因为您不能将常规处理应用于“结果树片段” - 您可以使用EXSLT node-set()扩展函数在大多数XSLT 1.0处理器中解决此问题,因此它成为

<xsl:apply-templates select="exslt:node-set($temp1)" mode="phase-2"/> 

但你是正确的轨道上看着流水线处理 - 一个复杂的转换分解成sequenc简单的步骤绝对是正确的路要走。

+0

非常感谢。你的回答简直太棒了。 我已经决定使用另一个样式表,并将转换的下一步添加到Ant构建工具。这种方法更加可重用,并且更容易维护和理解逻辑。 – KP13