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
,它会将这两个节点视为一个 *
第二个输入示例:
<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>
我应该在 XSLT 文件上添加什么以使其适用于两个或多个节点父级nodeA
等nodeB
。
目标是删除具有
successive duplicate
相同元素名称item1
item2
等的节点,相同id
和相同method
。一个 XML 节点后跟具有精确
same element name
、same id
和same method
的其他节点same children
将被视为重复。如果被比较的两个节点不共享相同的“nodeA”级别节点,则不应将它们视为要删除的重复项
谢谢约翰