Xml 使用XSLT转换子节点,但保持其余节点不变

Xml 使用XSLT转换子节点,但保持其余节点不变,xml,xslt,Xml,Xslt,我有一个XML,其中我只想修改一个特定的部分,其余部分保持不变,如何做到这一点?i、 我只想修改节点AA2 <root> <parentHeader> </parentHeader> <body> <ChildAA> <AA1> <foo>bar</foo> <foo>bar2</foo> &l

我有一个XML,其中我只想修改一个特定的部分,其余部分保持不变,如何做到这一点?i、 我只想修改节点AA2

<root>
  <parentHeader>
  </parentHeader>
  <body>
    <ChildAA>
      <AA1>
        <foo>bar</foo>
        <foo>bar2</foo>    
      </AA1>
      <AA2>
        <foo>bar</foo>
        <foo>bar2</foo>    
      </AA2>
     </ChildAA>
     <ChildBB>
      <BB1>
       <foo>bar</foo>
       <foo>bar2</foo>
      </BB1> 
      <BB2>
       <foo>bar</foo>
       <foo>bar2</foo>
      </BB2>   
     </ChildBB>
   </body>
</root>

酒吧
bar2
酒吧
bar2
酒吧
bar2
酒吧
bar2
我有以下XSLT,它只返回修改过的部分。我怎样才能包括所有其他内容

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>

      <!-- Whenever you match any node or any attribute -->
      <xsl:template match="/*"> 
           <xsl:apply-templates/>  
      </xsl:template>


    <xsl:template match="AA2">
       <RenamedAA2>    
         <xsl:copy-of select="."/>
      </RenamedAA2>
    </xsl:template>    
    <xsl:template match="text()"/>

</xsl:stylesheet>

我正在寻找这样的结果

<root>
  <parentHeader>
  </parentHeader>
  <body>
    <ChildAA>
      <AA1>
        <foo>bar</foo>
        <foo>bar2</foo>    
      </AA1>
     <RenamedAA2>
        <foo>bar</foo>
      </RenamedAA2>
      <RenamedAA2>
        <foo>bar2</foo>    
      </RenamedAA2>
     </ChildAA>
     <ChildBB>
      <BB1>
       <foo>bar</foo>
       <foo>bar2</foo>
      </BB1> 
      <BB2>
       <foo>bar</foo>
       <foo>bar2</foo>
      </BB2>   
     </ChildBB>
   </body>
</root>

酒吧
bar2
酒吧
bar2
酒吧
bar2
酒吧
bar2
您想要的是

当您匹配任何节点或任何属性时,包含注释
的模板并没有按照您的想法进行操作。它只匹配根元素

此外,您正在使用最后一个模板剥离所有
text()
节点

以下是您应该做的一个示例:

XSLT1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

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

    <xsl:template match="AA2">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

XML输出

<root>
   <parentHeader/>
   <body>
      <ChildAA>
         <AA1>
            <foo>bar</foo>
            <foo>bar2</foo>
         </AA1>
         <RenamedAA2>
            <foo>bar</foo>
         </RenamedAA2>
         <RenamedAA2>
            <foo>bar2</foo>
         </RenamedAA2>
      </ChildAA>
      <ChildBB>
         <BB1>
            <foo>bar</foo>
            <foo>bar2</foo>
         </BB1>
         <BB2>
            <foo>bar</foo>
            <foo>bar2</foo>
         </BB2>
      </ChildBB>
   </body>
</root>

酒吧
bar2
酒吧
bar2
酒吧
bar2
酒吧
bar2