XPath 2.0

编程入门 行业动态 更新时间:2024-10-27 18:28:37
XPath 2.0 - 选择2个元素之间的所有节点(XPath 2.0 - select all nodes between 2 elements)

我有以下XML文件:

<document> <article> <head>headline 1</head> <text> <paragraph>foo</paragraph> <paragraph>bar</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>some text</source> <portal>ABC</portal> <ID number="1"/> </article> <article> <head>headline 2</head> <text> <paragraph>lorem ipsum</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>another source</source> <portal>DEF</portal> <ID number="2"/> </article> </document>

现在,我想返回在头节点之后和门户节点之前发生的每篇文章的所有节点。 因此我正在研究XPath 2节点比较(<<和>>操作符)。

我到目前为止是以下内容,它返回空白:

<xsl:template match="/"> <xsl:copy-of select="/document/article/head/following-sibling::*[. << ./article/portal]"/> </xsl:template>

任何想法如何修复xpath查询?

I have the following XML file:

<document> <article> <head>headline 1</head> <text> <paragraph>foo</paragraph> <paragraph>bar</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>some text</source> <portal>ABC</portal> <ID number="1"/> </article> <article> <head>headline 2</head> <text> <paragraph>lorem ipsum</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>another source</source> <portal>DEF</portal> <ID number="2"/> </article> </document>

Now I'd like to return all nodes of each article that occur after the head node and before the portal node. Therefore I was looking into XPath 2 node comparison (<< and >> operators).

What I have so far is the following, which returns empty:

<xsl:template match="/"> <xsl:copy-of select="/document/article/head/following-sibling::*[. << ./article/portal]"/> </xsl:template>

Any ideas how to fix that xpath query?

最满意答案

使用

/*/*/node()[. >> ../head and ../portal >> .]

这是一个完整的转型

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <xsl:sequence select="/*/*/node()[. >> ../head and ../portal >> .]"/> </xsl:template> </xsl:stylesheet>

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

<document> <article> <head>headline 1</head> <text> <paragraph>foo</paragraph> <paragraph>bar</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>some text</source> <portal>ABC</portal> <ID number="1"/> </article> <article> <head>headline 2</head> <text> <paragraph>lorem ipsum</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>another source</source> <portal>DEF</portal> <ID number="2"/> </article> </document>

产生了想要的,正确的结果

<text> <paragraph>foo</paragraph> <paragraph>bar</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>some text</source> <text> <paragraph>lorem ipsum</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>another source</source>

更新

在评论中,Roman Pekar指出了一个新的要求:他希望获得每篇文章的第一 head和portal之间的所有这样的节点。

当然,这很简单 - 只需将上述表达方式更改为:

/*/*/node()[. >> ../head[1] and ../portal[1] >> .]

Use:

/*/*/node()[. >> ../head and ../portal >> .]

Here is a complete transformation:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <xsl:sequence select="/*/*/node()[. >> ../head and ../portal >> .]"/> </xsl:template> </xsl:stylesheet>

When this transformation is applied on the provided XML document:

<document> <article> <head>headline 1</head> <text> <paragraph>foo</paragraph> <paragraph>bar</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>some text</source> <portal>ABC</portal> <ID number="1"/> </article> <article> <head>headline 2</head> <text> <paragraph>lorem ipsum</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>another source</source> <portal>DEF</portal> <ID number="2"/> </article> </document>

the wanted, correct result is produced:

<text> <paragraph>foo</paragraph> <paragraph>bar</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>some text</source> <text> <paragraph>lorem ipsum</paragraph> </text> <date> <day>10</day> <month>05</month> <year>2002</year> </date> <source>another source</source>

Update:

In a comment Roman Pekar has specified a new requirement: he wants to get all such nodes that are between the first head and portal of each article.

Of course, this is straightforward -- just change the above expresssion to:

/*/*/node()[. >> ../head[1] and ../portal[1] >> .]

更多推荐

本文发布于:2023-04-28 01:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!