是否可以在同一个 XSLT 样式表中预处理 xml 源代码?
Is it possible to preprocess xml source within the same XSLT Stylesheet?
在相同的 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.
我怎样才能做到这一点?代码示例会很好吗?
是的,这是可能的。一种可能性是按如下方式进行:
<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。
- 在处理过程中,我们只保留q属性的值设置为小于等于5的元素。
在相同的 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.
我怎样才能做到这一点?代码示例会很好吗?
是的,这是可能的。一种可能性是按如下方式进行:
<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。
- 在处理过程中,我们只保留q属性的值设置为小于等于5的元素。