如何在 XSLT 中获得以下兄弟 [英] How to get the following sibling in XSLT
本文介绍了如何在 XSLT 中获得以下兄弟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 XSLT 还很陌生,这是我的 XML:
I am fairly new to XSLT and this is my 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
推荐答案
这实际上是一个完全 XPath 的问题.
This is actually a completely XPath question.
使用:
/*/project[title = 'Project X']/following-sibling::project[1]
这将选择作为 XML 文档中顶部元素的子元素的任何 Project
元素的任何第一个以下同级 Project
和至少以下元素的字符串值它的 title
子元素是字符串 "Project X"
.
This selects any first following sibling Project
of any Project
element that is a child of the top element in the XML document and the string value of at least of one of its title
children is the string "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>
这篇关于如何在 XSLT 中获得以下兄弟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文