0

我有一个 XSL 文件的以下输入:

<node>
    <xsl:value-of select="ROOT/LEVEL1/LEVEL2" />
</node>

我的预期输出是:

<node2>
    <xsl:value-of select="ROOT/LEVEL1/LEVEL2" />
</node2>

如何得到这个?

我能做的最好的:

<xsl:template match="node" >
    <node2>
        <xsl:copy-of select="." />
    </node2>
</xsl:template>

产生:

<node2><node>
    <xsl:value-of select="ROOT/LEVEL1/LEVEL2"/>
</node></node2>
4

1 回答 1

2

这样做<xsl:copy-of select="." />将复制现有节点,但在您的情况下,您只想复制子节点。而是试试这个

<xsl:copy-of select="node()" />

实际上,使用标识模板可能会更好,因为这将允许您对节点元素的子元素进行进一步的转换,而不仅仅是照原样复制。例如:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

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

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>
于 2012-05-18T08:11:50.360 回答