0

これは入力ファイルです:

<root> 
        <node id="N1">
            <fruit id="small_fruit" action="create">
                <orange id="1" action="create">
                    <attribute>
                        <color>yellow</color>
                    </attribute>
                </orange>
            </fruit>

            <fruit id="small_fruit" action="create">
                <orange id="1" action="destroy">
                    <attribute>
                        <color>green</color>
                    </attribute>
                </orange>
            </fruit>            
        </node>

        <node id="N2">
            <dog id="small_dog">    
                <poodle id="1" action="create">
                    <attribute>
                        <color>Yellow</color>    
                    </attribute>
                </poodle>       

                <terrier id="2" action="create">
                    <attribute>
                        <color>White</color>    
                    </attribute>
                </terrier>

                <poodle id="1" action="change">
                    <attribute>
                        <color>Brown</color>        
                    </attribute>
                </poodle>

                <terrier id="2" action="destroy">
                    <attribute>
                        <color>Blue</color>    
                    </attribute>
                </terrier>
            </dog>

            <dog id="small_dog" action="create">               
                <poodle id="1" action="destroy">
                    <attribute>
                        <color>Black</color>        
                    </attribute>
                </poodle>

                <terrier id="2" action="change">
                    <attribute>
                        <color>White</color>        
                    </attribute>
                </terrier>
                <terrier id="2" action="change">
                    <attribute>
                        <color>Grey</color>        
                    </attribute>
                </terrier>
            </dog>

            <dog id="large_dog">                
                <poodle id="1" action="create">
                    <attribute>
                        <color>Red</color>
                    </attribute>
                </poodle>
            </dog>
        </node>
    </root>

これは期待される出力です:

<root> 
<node id="N1">
    <fruit id="small_fruit" action="create">

    </fruit>

    <fruit id="small_fruit" action="create">
        <orange id="1" action="destroy">
            <attribute>
                <color>green</color>
            </attribute>
        </orange>
    </fruit>            
</node>

<node id="N2">
    <dog id="small_dog">                           

    </dog>

    <dog id="small_dog" action="create">               
        <poodle id="1" action="destroy">
            <attribute>
                <color>Black</color>        
            </attribute>
        </poodle>

        <terrier id="2" action="change">
            <attribute>
                <color>White</color>        
            </attribute>
        </terrier>
        <terrier id="2" action="change">
            <attribute>
                <color>Grey</color>        
            </attribute>
        </terrier>
    </dog>

    <dog id="large_dog">                
        <poodle id="1" action="create">
            <attribute>
                <color>Red</color>
            </attribute>
        </poodle>
     </dog>
</node>
</root>

ルール:

  1. 「destroy」メソッドを持つノードが同じ親 (果物または動物) の最後に表示される場合、以前のノードをすべて削除します。

  2. そうでない場合は、「destroy」メソッドを使用したノードを含むすべてのノードを削除し、残りは変更せずに残します。

単純化するには:

  • xxx/破棄 -> 破棄
  • xxx/destroy/aaa/bbb -> aaa/bbb

要約すると、同じ ID とノード名(orange-id:1 または terrier-id:2 または poodle-id:1)を持つノードをチェックし、同じ親exの下にある必要があります。(果物または犬)

4

1 に答える 1

1

ご希望のルールを説明した方法と、期待される結果を完全に一致させることができませんでした。ただし、予想される出力と入力を比較すると、これが必要な条件であると思います。

<xsl:if test="not(following::*[../@id = current()/../@id][@action='destroy'])">

したがって、次の XSLT を考えると、

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="node/*/*">
      <xsl:if test="not(following::*[../@id = current()/../@id][@action='destroy'])">
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
      </xsl:if>
   </xsl:template>

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

現在の入力 XML に適用すると、次のような出力が得られます。これは、現在予想される出力と一致します。

<root>
   <node id="N1">
      <fruit id="small_fruit" action="create"/>
      <fruit id="small_fruit" action="create">
         <orange id="1" action="destroy">
            <attribute>
               <color>green</color>
            </attribute>
         </orange>
      </fruit>
   </node>
   <node id="N2">
      <dog id="small_dog"/>
      <dog id="small_dog" action="create">
         <poodle id="1" action="destroy">
            <attribute>
               <color>Black</color>
            </attribute>
         </poodle>
         <terrier id="2" action="change">
            <attribute>
               <color>White</color>
            </attribute>
         </terrier>
         <terrier id="2" action="change">
            <attribute>
               <color>Grey</color>
            </attribute>
         </terrier>
      </dog>
      <dog id="large_dog">
         <poodle id="1" action="create">
            <attribute>
               <color>Red</color>
            </attribute>
         </poodle>
      </dog>
   </node>
</root>
于 2012-04-29T10:20:11.603 に答える