我喜欢
XML
<root> <a>One</a> <a>Two</a> <b>Three</b> <c>Four</c> <a>Five</a> <b> <a>Six</a> </b> </root>
并且需要选择root中任何子节点名称的最后一次出现.在这种情况下,所需的结果列表将是:
<c>Four</c> <a>Five</a> <b> <a>Six</a> </b>
任何帮助表示赞赏!
解决方法
基于XSLT的解决方案:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="root/*"> <xsl:variable name="n" select="name()"/> <xsl:copy-of select=".[not(following-sibling::node()[name()=$n])]"/> </xsl:template> </xsl:stylesheet>
产量:
<c>Four</c> <a>Five</a> <b> <a>Six</a> </b>
第二种解决方案(您可以将其用作单个XPath表达式):
<xsl:template match="/root"> <xsl:copy-of select="a[not(./following-sibling::a)] | b[not(./following-sibling::b)] | c[not(./following-sibling::c)]"/> </xsl:template>