4

特定のセクションのみを変更し、残りはそのまま残したい XML があります。これを行うにはどうすればよいですか? つまり、ノード 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>

変更されたセクションのみを返す次の 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>
4

1 に答える 1

7

あなたが望むのは恒等変換です。

コメントのWhenever you match any node or any attributeあるテンプレートは、あなたが思っていることをしていません。ルート要素のみに一致します。

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>
于 2012-06-04T02:41:58.303 に答える