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

xml – 如何在XSLT中获得以下兄弟姐妹

发布时间:2020-12-16 07:53:52 所属栏目:百科 来源:网络整理
导读:我相当新的XSLT,这是我的 XML: projects project number1/number titleProject X/title /project project number2/number titleProject Y/title /project project number3/number titleProject Z/title /project/projects 如果我有一个项目,想要得到跟随它
我相当新的XSLT,这是我的 XML:
<projects>
    <project>
        <number>1</number>
        <title>Project X</title>
    </project>
    <project>
        <number>2</number>
        <title>Project Y</title>
    </project>
    <project>
        <number>3</number>
        <title>Project Z</title>
    </project>
</projects>

如果我有一个项目,想要得到跟随它的兄弟姐妹,那我该如何做呢?

这段代码对我来说似乎不起作用:

/projects[title="Project X"]/following-sibling

感谢任何帮助!

这实际上是一个完全XPath的问题.

使用:

/*/project[title = 'Project X']/following-sibling::project[1]

这将选择任何作为XML文档中顶级元素的子项的Project元素的以下同级项目,并且至少其一个标题子项的字符串值为字符串“Project X”.

基于XSLT的验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select=
      "/*/project[title = 'Project X']/following-sibling::project[1]"/>
 </xsl:template>
</xsl:stylesheet>

当将此转换应用于提供的XML文档时:

<projects>
    <project>
        <number>1</number>
        <title>Project X</title>
    </project>
    <project>
        <number>2</number>
        <title>Project Y</title>
    </project>
    <project>
        <number>3</number>
        <title>Project Z</title>
    </project>
</projects>

评估XPath表达式,并将正确选择的元素复制到输出:

<project>
   <number>2</number>
   <title>Project Y</title>
</project>

(编辑:李大同)

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

    推荐文章
      热点阅读