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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10246371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:27:49  来源:igfitidea点击:

How to get the following sibling in XSLT

xmlxsltxpath

提问by Tintin81

I am fairly new to XSLT and this is my XML:

我对 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>

If I have one project and want to get the sibling that follows it, how can I do that?

如果我有一个项目并且想要获得跟随它的兄弟姐妹,我该怎么做?

This code doesn't seem to work for me:

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

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

回答by Dimitre Novatchev

This is actually a completely XPath question.

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

Use:

使用

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

This selects any first following sibling Projectof any Projectelement that is a child of the top element in the XML document and the string value of at least of one of its titlechildren is the string "Project X".

这将选择作为 XML 文档中顶部元素的子元素Project的任何Project元素的任何第一个后续兄弟元素,并且其至少一个子元素的字符串值title是 string "Project X"

XSLT - based verification:

基于 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>

When this transformation is applied on the provided XML document:

当此转换应用于提供的 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>

the XPath expression is evaluated and the correctly-selected element is copied to the output:

计算 XPath 表达式并将正确选择的元素复制到输出:

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