在同一个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.
我怎样才能做到这一点?一个代码示例会很好吗?
答案 0 :(得分:2)
是的,这是可能的。一种可能性是按如下方式进行:
<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="*"/>