将所有模板应用于全局变量而不是源

时间:2015-01-12 17:10:14

标签: xslt global-variables xslt-1.0

我使用全局变量从源XML捕获一组节点。对于源XML中的每个节点,我需要检查该节点是否存在于变量中。这可能吗?

另一个选择是将所有模板应用于全局变量而不是源XML。但我不知道这是否可能。

鉴于以下答案。如果我尝试将模板应用于全局变量,我会得到以下代码,但它不起作用。如何将变量传递给其余模板并仍然具有相应的匹配?

<xsl:variable name="transformation_result">   
  <ABC>
    <xsl:copy-of select="/ABC/Data"/>
  </ADT>
</xsl:variable>

  <xsl:template match="/">
    <xsl:apply-templates select="$transformation_result"/>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="remove_whitespace" match="text()">
    <xsl:value-of select="normalize-space()"/>
  </xsl:template>

1 个答案:

答案 0 :(得分:1)

如果您使用

<xsl:template match="/">
  <xsl:apply-templates select="exsl:node-set($var)/node()" mode="m2"/>
</xsl:template>

<xsl:template match="@* | node()" mode="m2">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()" mode="m2"/>
  </xsl:copy>
</xsl:template>

<xsl:template name="remove_whitespace" match="text()" mode="m2">
  <xsl:value-of select="normalize-space()"/>
</xsl:template>

您的样式表中的命名空间exsl需要声明为xmlns:exsl="http://exslt.org/common",然后您可以在第二步中处理变量的结果并删除空格。