1

入力 XML:

.....
..... <!-- Many lines of code -->
.....
<Attribute Name="Attr1">  
  <Array>
       <Object Type="Object1">
          <Attribute Name="Attr2">
              "123"
              "234"
           </Attribute>
           <Attribute Name="Attr3">"456"</Attribute>
        </Object>
        <Object Type="Object2">
           <Attribute Name="Attr4">
               "444"
               "555"
            </Attribute>               
         </Object>
         <Object Type="Object3">
            <Attribute Name="Attr5">
                "666"   
                "777"
                "888"     
    <!-- My new item should come here -->   
             </Attribute>
          </Object> 
   </Array>
</Attribute>

上記の場所「My new item should come here」に新しいエントリ (999) を追加するために、次の XSLT を試しました。Attribute(<Attribute Name = "Attr5")この新しいアイテムを挿入する前に、ノードの値が「888」かどうかを確認したいと思います。"888" が含まれる場合のみ、その後に "999" を挿入します。これを達成する方法を教えてください。

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    >
     <xsl:output method="xml" indent="yes"/>

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

     <xsl:variable name="obj" select='"Object3"'/>
     <xsl:variable name="attr" select='"Attr5"'/>
     <xsl:param name="evalue">"999"</xsl:param>

     <xsl:template match="Attribute/Array/Object[@Type=$obj]/Attribute[@Name=$attr]">
      <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:value-of select="." />
    <xsl:value-of select="$evalue"/>
  </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>

出力 XML

<Attribute Name="Attr1">  
  <Array>
       <Object Type="Object1">
          <Attribute Name="Attr2">
              "123"
              "234"
           </Attribute>
           <Attribute Name="Attr3">"456"</Attribute>
        </Object>
        <Object Type="Object2">
           <Attribute Name="Attr4">
               "444"
               "555"
            </Attribute>               
         </Object>
         <Object Type="Object3">
            <Attribute Name="Attr5">
                "666"   
                "777"
                "888"     
                "999"
             </Attribute>
          </Object> 
   </Array>
</Attribute>

最終的な XML は次のようになります。私を助けてください

4

1 に答える 1