我有这个输入文件:
<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以适应上述情况吗?非常感谢您的帮助。
干杯, 约翰
答案 0 :(得分: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>