0

我有这个输入文件:

<myroot>
    <list id="xxx">
        <node id="a">
            <section id="i">
                <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
                <item1 id="a0" method="start"> <!-- this one is successive from the previous so we eliminate -->
                    <somechild>a</somechild>
                </item1>
                <item1 id="a0" method="stop"/>                
                <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
            </section>  

            <section id="i">
                <item1 id="a0" method="start"> <!-- this one is successive from the previous so we eliminate -->
                    <somechild>a</somechild>
                </item1>
                <item1 id="a0" method="stop"/>  
                 <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
                 <item1 id="a0" method="start"> <!-- this one is successive from the previous so we eliminate -->
                    <somechild>a</somechild>
                </item1>
            </section>                
        </node>
    </list>
</myroot>

我的输出:

 <myroot>
        <list id="xxx">
            <node id="a">
                <section id="i">
                    <item1 id="a0" method="start">
                        <somechild>a</somechild>
                    </item1>
                    <item1 id="a0" method="stop"/>                
                </section>  

                <section id="i" />
            </node>
        </list>
    </myroot>

**While the expected output should be:**

<myroot>
    <list id="xxx">
        <node id="a">
            <section id="i">
                <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
                <item1 id="a0" method="stop"/>                
                <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
            </section>  

            <section id="i">
                <item1 id="a0" method="stop"/>  
                 <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
            </section>                
        </node>
    </list>
</myroot>

XSLT 文件:

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

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

    <xsl:template match="/*/*/*">
        <xsl:copy>
            <xsl:variable name="first-in-group" as="element()*">
                <xsl:for-each-group select="*" group-by="concat(node-name(.), '|', @id,'|', @method)">
                    <xsl:for-each-group select="current-group()/*" group-by="concat(@id, '|', @method)">
                        <xsl:sequence 
              select="for $pos in 1 to count(current-group())
                      return current-group()[$pos]
                              [every $item 
                              in subsequence(current-group(), 1, $pos - 1) 
                              satisfies not(deep-equal($item, current-group()[$pos]))] "/>
                    </xsl:for-each-group>
                </xsl:for-each-group>
            </xsl:variable>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates>
                <xsl:with-param name="first-in-group" select="$first-in-group" tunnel="yes"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*/*/*/*/*">
        <xsl:param name="first-in-group" tunnel="yes"/>
        <xsl:if test="$first-in-group intersect .">
            <xsl:call-template name="identity"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

另一种情况:

<myroot>
    <list id="xxx">
        <node id="a">
            <section id="i">
                <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
            </section>  

            <section id="i">               
                <item1 id="a1" method="start"> 
                    <somechild>a</somechild>
                </item1>
                <item1 id="a0" method="start"> <!-- this one is successive from the previous, because the previous node has id of 'a1', so we eliminate -->
                    <somechild>a</somechild>
                </item1>
                <item1 id="a0" method="start"> <!-- this one is successive from the previous so we eliminate -->
                    <somechild>a</somechild>
                </item1>
            </section>                
        </node>
    </list>
</myroot>

预期输出:

<myroot>
    <list id="xxx">
        <node id="a">
            <section id="i">
                <item1 id="a0" method="start">
                    <somechild>a</somechild>
                </item1>
            </section>  

            <section id="i">               
                <item1 id="a1" method="start">
                    <somechild>a</somechild>
                </item1>
            </section>                
        </node>
    </list>
</myroot>

这个想法是用相同的方法删除连续的节点。在示例中:

  • 开始/开始/停止/开始/开始 --> 开始/停止/开始

XSLT 可以删除连续节点,但不适用于上述场景。

谁能指出我应该在哪里更改 xslt 以适应上述情况?非常感谢您的帮助。

干杯,约翰

4

1 回答 1

1

也许我错过了一些东西,但看起来你把它复杂化了。您应该能够匹配/剥离任何等于同名前一个元素的元素。

XSLT 2.0

<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()][1])]"/>

</xsl:stylesheet>

XML 输出(使用上面更新的 xml)

<myroot>
   <list id="xxx">
      <node id="a">
         <section id="i">
            <item1 id="a0" method="start">
               <somechild>a</somechild>
            </item1>
            <item1 id="a0" method="stop"/>
            <item1 id="a0" method="start">
               <somechild>a</somechild>
            </item1>
         </section>
         <section id="i">
            <item1 id="a0" method="stop"/>
            <item1 id="a0" method="start">
               <somechild>a</somechild>
            </item1>
         </section>
      </node>
   </list>
</myroot>
于 2012-06-03T04:07:00.733 回答