0

XSLT:

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

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

    <xsl:template match="*[*][deep-equal(.,preceding::*[name()=current()/name()][@id=current()/@id][1])]"/>
</xsl:stylesheet>

私の入力に適用されたとき:

<myroot>
    <nodeA id="a">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- second consecutive create, we will remove this -->
                <somechild>a</somechild>
            </item1>
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
            <item1 id="0" method="change"> <!-- second consecutive create, we will remove this -->
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>
</myroot>

正しい出力を教えてください:

<myroot>
    <nodeA id="a">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>
</myroot>

*しかし、問題は、別のnodeAノードidがある場合、これら2つのノードを1つとして扱うことです*

2 番目の入力例:

<myroot>
    <nodeA id="a">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- second consecutive create, we will remove this -->
                <somechild>a</somechild>
            </item1>
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
            <item1 id="0" method="change"> <!-- second consecutive create, we will remove this -->
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>

    <nodeA id="b">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- second consecutive create, we will remove this -->
                <somechild>a</somechild>
            </item1>
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
            <item1 id="0" method="change"> <!-- second consecutive create, we will remove this -->
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>
</myroot>

私の出力:

<myroot>
   <nodeA id="a">
      <section id="i">
         <item1 id="0" method="create">
            <somechild>a</somechild>
         </item1>
         <item1 id="1" method="create">
            <otherchild>a</otherchild>
         </item1>
      </section>
      <section id="i">
         <item1 id="0" method="create"><!-- third consecutive create, BUT children have different value , so we don't remove this --><somechild>bbb</somechild>
         </item1>
         <item1 id="3" method="create">
            <other>xx</other>
         </item1>
         <item1 id="0" method="change">
            <otherchild>a</otherchild>
         </item1>
      </section>
   </nodeA>
   <nodeA id="b">
      <section id="i">
         <item1 id="0" method="create">
            <somechild>a</somechild>
         </item1>
      </section>
      <section id="i">
         <item1 id="0" method="create"><!-- third consecutive create, BUT children have different value , so we don't remove this --><somechild>bbb</somechild>
         </item1>
         <item1 id="0" method="change">
            <otherchild>a</otherchild>
         </item1>
      </section>
   </nodeA>
</myroot>

期待される出力の場所:

<myroot>
    <nodeA id="a">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>

    <nodeA id="b">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>
</myroot>

2 つ以上のノードの親などに対して機能させるには、XSLT ファイルに何を追加する必要がありますnodeAか。nodeB

  • successive duplicate目的は、同じ要素名item1 item2など、同じidと同じでノードを削除することですmethod

  • same element name正確な、same idsame methodおよびを含む他のノードが続く XML ノードは、same children重複していると見なされます。

  • 比較されている 2 つのノードが同じ「nodeA」レベルのノードを共有していない場合、それらは削除される重複と見なされるべきではありません。

ありがとうジョン

4

2 に答える 2

3

この変換:

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

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

 <xsl:template match=
   "section/*
      [for $current in .,
           $curNodeAnc in $current/ancestor::*[starts-with(name(), 'node')][1]
         return
           some $preceding in $current/preceding::*
              satisfies
                 $preceding/parent::section
                and
                 $curNodeAnc is $preceding/ancestor::*[starts-with(name(), 'node')][1]
                and
                 deep-equal($current, $preceding)

      ]
   "/>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<myroot>
        <nodeA id="a">
            <section id="i">
                <item1 id="0" method="create">
                    <somechild>a</somechild>
                </item1>

                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>

            <section id="i">
                <item1 id="0" method="create"> <!-- second consecutive create, we will remove this -->
                    <somechild>a</somechild>
                </item1>
                <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                    <somechild>bbb</somechild>
                </item1>
                <item1 id="3" method="create">
                    <other>xx</other>
                </item1>

                <item1 id="0" method="change">
                    <otherchild>a</otherchild>
                </item1>
                <item1 id="0" method="change"> <!-- second consecutive create, we will remove this -->
                    <otherchild>a</otherchild>
                </item1>
            </section>
        </nodeA>

        <nodeA id="b">
            <section id="i">
                <item1 id="0" method="create">
                    <somechild>a</somechild>
                </item1>

                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>

            <section id="i">
                <item1 id="0" method="create"> <!-- second consecutive create, we will remove this -->
                    <somechild>a</somechild>
                </item1>
                <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                    <somechild>bbb</somechild>
                </item1>
                <item1 id="3" method="create">
                    <other>xx</other>
                </item1>

                <item1 id="0" method="change">
                    <otherchild>a</otherchild>
                </item1>
                <item1 id="0" method="change"> <!-- second consecutive create, we will remove this -->
                    <otherchild>a</otherchild>
                </item1>
            </section>
        </nodeA>
</myroot>

要件が正しく定義されておらず、実際には望ましい結果と矛盾していますが、正確に望ましい結果が得られます-明らかに、ジョンにはこれに大きな改善の余地があります

<myroot>
   <nodeA id="a">
      <section id="i">
         <item1 id="0" method="create">
            <somechild>a</somechild>
         </item1>
         <item1 id="1" method="create">
            <otherchild>a</otherchild>
         </item1>
      </section>
      <section id="i">
         <item1 id="0" method="create"><!-- third consecutive create, BUT children have different value , so we don't remove this -->
         <somechild>bbb</somechild>
         </item1>
         <item1 id="3" method="create">
            <other>xx</other>
         </item1>
         <item1 id="0" method="change">
            <otherchild>a</otherchild>
         </item1>
      </section>
   </nodeA>
   <nodeA id="b">
      <section id="i">
         <item1 id="0" method="create">
            <somechild>a</somechild>
         </item1>
         <item1 id="1" method="create">
            <otherchild>a</otherchild>
         </item1>
      </section>
      <section id="i">
         <item1 id="0" method="create"><!-- third consecutive create, BUT children have different value , so we don't remove this -->
          <somechild>bbb</somechild>
         </item1>
         <item1 id="3" method="create">
            <other>xx</other>
         </item1>
         <item1 id="0" method="change">
            <otherchild>a</otherchild>
         </item1>
      </section>
   </nodeA>
</myroot>
于 2012-06-11T12:53:42.927 に答える
1

このドキュメントを入力ドキュメントとして使用します。

<myroot>
    <nodeA id="a">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- second consecutive create, we will remove this -->
                <somechild>a</somechild>
            </item1>
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
            <item1 id="0" method="change"> <!-- second consecutive create, we will remove this -->
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>

    <nodeA id="b">
        <section id="i">  
            <item1 id="0" method="create"> 
                <somechild>a</somechild>
            </item1>

            <item1 id="1" method="create">
                <otherchild>a</otherchild>
            </item1>
        </section>        

        <section id="i">
            <item1 id="0" method="create"> <!-- second consecutive create, we will remove this -->
                <somechild>a</somechild>
            </item1>
            <item1 id="0" method="create"> <!-- third consecutive create, BUT children have different value , so we don't remove this -->
                <somechild>bbb</somechild>
            </item1>
            <item1 id="3" method="create">
                <other>xx</other>    
            </item1>

            <item1 id="0" method="change"> 
                <otherchild>a</otherchild>
            </item1>
            <item1 id="0" method="change"> <!-- second consecutive create, we will remove this -->
                <otherchild>a</otherchild>
            </item1>
        </section>
    </nodeA>
</myroot>

..そしてこのスタイルシート..。

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

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

    <xsl:template match="*[*]
         [deep-equal(.,
           preceding::*[name()=current()/name()]
                       [@id = current()/@id]
                       [../../@id = current()/../../@id]
                       [1])]" />
</xsl:stylesheet>

...指定された必要な出力を生成します..。

<?xml version="1.0" encoding="UTF-8"?>
<myroot>
   <nodeA id="a">
      <section id="i">
         <item1 id="0" method="create">
            <somechild>a</somechild>
         </item1>
         <item1 id="1" method="create">
            <otherchild>a</otherchild>
         </item1>
      </section>
      <section id="i">
         <item1 id="0" method="create"><!-- third consecutive create, BUT children have different value , so we don't remove this --><somechild>bbb</somechild>
         </item1>
         <item1 id="3" method="create">
            <other>xx</other>
         </item1>
         <item1 id="0" method="change">
            <otherchild>a</otherchild>
         </item1>
      </section>
   </nodeA>
   <nodeA id="b">
      <section id="i">
         <item1 id="0" method="create">
            <somechild>a</somechild>
         </item1>
         <item1 id="1" method="create">
            <otherchild>a</otherchild>
         </item1>
      </section>
      <section id="i">
         <item1 id="0" method="create"><!-- third consecutive create, BUT children have different value , so we don't remove this --><somechild>bbb</somechild>
         </item1>
         <item1 id="3" method="create">
            <other>xx</other>
         </item1>
         <item1 id="0" method="change">
            <otherchild>a</otherchild>
         </item1>
      </section>
   </nodeA>
</myroot>

このわずかに変化するスタイルシートを使用して、検証を支援できることに注意してください。ノードを削除する代わりに、ノードを挿入して削除するように注釈を付けます。その出力を見ると、何が起こっているのかを視覚化するのに役立ちます。

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

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

    <xsl:template match="*[*]
         [deep-equal(.,
           preceding::*[name()=current()/name()]
                       [@id = current()/@id]
                       [../../@id = current()/../../@id]
                       [1])]" >
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <removed>
             <xsl:value-of select="'Parent of this node will be REMOVED.'" />
             <xsl:value-of select="../../@id" />
            </removed>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>
于 2012-06-11T12:42:57.693 に答える