首页 文章

XSLT将元素移动到其他节点

提问于
浏览
0

我有以下问题 .

我有这个XML

<?xml version="1.0"?>
<root>
    <first>a</first>
    <third>
        <goal>b</goal>
    </third>
</root>

我需要这个布局

<?xml version="1.0"?>
<root>
    <first>a</first>
    <second>
        <goal>b</goal>
    </second>
</root>

所以我认为XSLT对我来说是正确的工具但是我没有让它工作 .

那是我的XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>

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



    <xsl:template match="/root/third/goal">
                   <goal><xsl:value-of select="."/></goal>
        </xsl:template> 
</xsl:stylesheet>

欢迎任何帮助:-)

1 回答

  • 0

    如果要更改 third 元素,则需要编写一个模板:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="ISO-8859-1"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
    
    
        <xsl:template match="root/third[goal]">
            <second>
              <xsl:apply-templates/>
            </second>
        </xsl:template>
    
    </xsl:stylesheet>
    

相关问题