具有xml结构的xslt变量

时间:2019-04-03 09:15:35

标签: xml xslt xslt-1.0

我需要进入一个节点的内部结构的变量中,问题是节点由于不同的原因而具有不同的路径。到目前为止,我正在做的是

<xsl:variable name="my_variable">
  <xsl:choose>
    <xsl:when test="One/Path/To/TheNode">
      <xsl:value-of select="One/Path/To/TheNode"/>
    </xsl:when>
    <xsl:when test="One/Different/Path/To/TheNode">
      <xsl:value-of select="One/Different/Path/To/TheNode"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="//TheNode"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

然后,如果我使用此变量来获取某些子节点,则执行以下操作:

   <xsl:variable name="other_variable" select="$my_variable/Subnode"/>

我在运行时XPath error : Invalid type Evaluating global variable var/param being computed failed遇到此错误,我也尝试这样做:

<xsl:variable name="other_variable" select="ext:node-set($my_variable)/SubNode"/>

执行xslt时没有任何错误,但是other_variable为空。通过执行my_variable<xsl:value-of select="$my_variable"/>来检查<xsl:apply-templates mode="copy" select="ext:node-set($my_variable)"/>的内容,我可以看到数据,但是没有xml结构,我的意思是我可以看到bar但没有<foo>bar</bar>

有没有办法在不使用<xsl:variable name="my_variable" select="//TheNode"/>的情况下从其他路径获取此结构?

谢谢。

1 个答案:

答案 0 :(得分:1)

xsl:value-of指令将创建一个文本节点,该文本节点具有所选节点的字符串值,或更确切地说是所选节点集的第一个节点的字符串值(在XSLT 1.0中) 。

如果要保留结构,请改用xsl:copy-of指令。