[英]XSLT: how to copy the output of an identity transformation to an xslt variable?
使用以下代码,我将XML输入中所有出现的元素QUOTE替换为一个字符串,该字符串是QUOTE / @ ID属性的值。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="QUOTE">
<xsl:value-of select="@ID"/>
</xsl:template>
</xsl:stylesheet>
现在,我想将上述转换的输出复制到变量中,以便将其作为模板中的参数传递,然后将XPATH应用于此变量,如下所示:
<xsl:template name="second-pass">
<!-- variable which holds the identity transformation -->
<xsl:param name="pre-processed-xml"/>
<!-- call SUMMARY template with parameter -->
<xsl:call-template name="SUMMARY">
<xsl:with-param name="pre-processed-xml" select="exsl:node-set($pre-processed-xml)"/>
</xsl:call-template>
</xsl:template>
<!-- SUMMARY template -->
<xsl:template name="SUMMARY">
<xsl:param name="pre-processed-xml"/>
<xsl:value-of select="$pre-processed-xml//SUMMARY">
</xsl:template>
我的问题如下:
您可以做的就是简单地匹配文档节点/
,然后将xsl:apply-templates
的结果存储在变量中,然后可以将其传递给second-pass
模板
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common">
<xsl:template match="/">
<xsl:variable name="doc">
<xsl:apply-templates />
</xsl:variable>
<xsl:call-template name="second-pass">
<xsl:with-param name="pre-processed-xml" select="$doc" />
</xsl:call-template>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="QUOTE">
<xsl:value-of select="@ID"/>
</xsl:template>
<xsl:template name="second-pass">
<!-- variable which holds the identity transformation -->
<xsl:param name="pre-processed-xml"/>
<!-- call SUMMARY template with parameter -->
<xsl:call-template name="SUMMARY">
<xsl:with-param name="pre-processed-xml" select="exsl:node-set($pre-processed-xml)"/>
</xsl:call-template>
</xsl:template>
<!-- SUMMARY template -->
<xsl:template name="SUMMARY">
<xsl:param name="pre-processed-xml"/>
<xsl:value-of select="$pre-processed-xml//SUMMARY" />
</xsl:template>
</xsl:stylesheet>
话虽如此,这似乎有点过头了。 给定您在问题中显示的内容,只需执行此操作即可获得相同的结果。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="//SUMMARY" />
</xsl:template>
<xsl:template match="QUOTE">
<xsl:value-of select="@ID"/>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.