我有以下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] >> .]更多推荐
发布评论