是否可以在同一XSLT样式表中预处理xml源?
在相同的XSLT(2.0)样式表和转换中,我希望:是否可以在同一XSLT样式表中预处理xml源?,xslt,xslt-2.0,Xslt,Xslt 2.0,在相同的XSLT(2.0)样式表和转换中,我希望: 1) first preprocess the whole XML Datasource (Add a attribute with a specific calculation to certain elements) 然后 2: transform the changed XML Datasource with the sylesheet's templates. 我怎样才能做到这一点?一个代码示例会很好?是的,这是可能的
1) first preprocess the whole XML Datasource (Add a attribute
with a specific calculation to certain elements)
然后
2: transform the changed XML Datasource with the sylesheet's templates.
我怎样才能做到这一点?一个代码示例会很好?是的,这是可能的。一种可能性是按以下方式进行:
<xsl:template match="/">
<!-- store the modified content in a variable -->
<xsl:variable name="preprocessed.doc">
<xsl:apply-templates mode="preprocess" />
</xsl:variable>
<!-- process the modified contents -->
<xsl:apply-templates select="$preprocessed.doc/*" />
</xsl:template>
<!-- first pass: sample process to add an attribute named "q" -->
<xsl:template match="*" mode="preprocess">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="q"><xsl:number count="*" level="any" /></xsl:attribute>
<xsl:apply-templates mode="preprocess" />
</xsl:copy>
</xsl:template>
<!-- "normal" processing of the modified content. It is able to used the newly processed attribute. -->
<xsl:template match="*[@q <= 5]">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*"/>
是的,这是可能的。一个示例输入XML文档以及一些关于您希望使用属性添加/修改的内容的说明就很好了。在单个样式表中运行一系列转换(管道)当然是可能的,如@potame所示。然而,将转换作为单独的样式表运行通常更可取:这意味着您可以更容易地重用代码(例如,您可以使用不同的第二步运行相同的预处理步骤)。@Micheal-Kay“无论如何,potame解决方案对我来说是完美的。我昨天刚买了你的书,因为尽管它可以追溯到2008年,但它是我能在XSLT2.0上找到的最好的参考资料。现在,我正在寻找让xslt动态生成模板的方法(在类似LISP宏的样式表中)。但我想这是不可能的,我只能在书中找到XPath表达式的eval