加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – XPath每个元素的最后一次出现

发布时间:2020-12-16 23:31:12 所属栏目:百科 来源:网络整理
导读:我喜欢 XML root aOne/a aTwo/a bThree/b cFour/c aFive/a b aSix/a /b/root 并且需要选择root中任何子节点名称的最后一次出现.在这种情况下,所需的结果列表将是: cFour/caFive/ab aSix/a/b 任何帮助表示赞赏! 解决方法 基于XSLT的解决方案: ?xml version
我喜欢 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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读