如果我有这一xml档案:
<root>
<node id="a">
<Items id="a_1" method="pause">
<item id="0" method="pause">
<attribute>
<color>Red</color>
</attribute>
</item>
</Items>
<Items id="a_1" method="pause">
<item id="0" method="stop">
<attribute>
<color>Red</color>
</attribute>
</item>
</Items>
</node>
<node id="b">
<Persons id="b_1">
<person id="b_1b" method="pause">
<attribute>a</attribute>
</person>
<person id="b_1a" method="start">
<attribute>
<name>John</name>
</attribute>
</person>
<person id="b_1b" method="stop">
<attribute>a</attribute>
</person>
<person id="b_1a" method="pause">
<attribute>a</attribute>
</person>
</Persons>
<Persons id="b_1" method="start">
<person id="b_1a" method="stop">
<attribute>a</attribute>
</person>
<person id="b_1b" method="start">
<attribute>a</attribute>
</person>
<person id="b_1b" method="pause">
<attribute>a</attribute>
</person>
</Persons>
<Persons id="b_2">
<person id="b_1a" method="start">
<attribute>
<name>John</name>
</attribute>
</person>
</Persons>
</node>
</root>
预计产出如下:
<root>
<node id="a">
<Items id="a_1" method="pause">
</Items>
<Items id="a_1" method="pause">
<item id="0" method="stop">
<attribute>
<color>Red</color>
</attribute>
</item>
</Items>
</node>
<node id="b">
<Persons id="b_1">
</Persons>
<Persons id="b_1" method="start">
<person id="b_1a" method="stop">
<attribute>a</attribute>
</person>
<person id="b_1b" method="start">
<attribute>a</attribute>
</person>
<person id="b_1b" method="pause">
<attribute>a</attribute>
</person>
</Persons>
<Persons id="b_2">
<person id="b_1a" method="start">
<attribute>
<name>John</name>
</attribute>
</person>
</Persons>
</node>
</root>
算法的关键在于stop方法。
- if it is the last remove all node before and leave the one with stop
- if node with stop method is not the last then remove that node with stop and all node before it (leave all node after that stop).
这必须发生在mong儿童身上,其父母的亲身特性是相同的<>/strong>,并且将只删除儿童节点(即使父母在删除使用者节点后是空洞的)。
For the example above:
- item id=0 pause then item id=0 stop -> the result will be item id=0 stop (parent: Items id=a_1-pause).
- person id=b_1a start then person id=b_1a pause then person id=b1_a stop (parents: Person id=b_1) - so the result become only person id=b_1a stop
- person id=b_1b pause then person id=b_1b stop then person id=b_1b start then person id=b_1b pause -> it will become person id=b_1b start then person id=b_1b pause (again we compare every person under the parent Person id=b_1; in the parent we dont care if one of them does not have method as long as it is the same id as shown on the example)
这是我只为人所知的。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- Ignore person with stop method which are not the last such person -->
<xsl:template match="person[@method= stop ][following::person[@method= stop ]]"/>
<!-- Match other persons -->
<xsl:template match="person">
<!-- Copy the person if there isn t a following person with the same id and stop method -->
<xsl:if test="not(following::person[@id=current()/@id][@method= stop ])">
<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>
Can it be changed to solve all other nodes (the node and parent name can be anything) ? Thanks very much.
kind regards, John