XML XPath查找具有属性及其父节点的所有节点
我想编写将从此xml返回的XPath查询XML XPath查找具有属性及其父节点的所有节点,xml,xpath,Xml,Xpath,我想编写将从此xml返回的XPath查询 <?xml version="1.0" encoding="UTF-8"?> <contents> <content type="menu_0" nested="yes"> <content type="name" lang="pol"><![CDATA[PIES]]></content> <content type="menu_1" nested
<?xml version="1.0" encoding="UTF-8"?>
<contents>
<content type="menu_0" nested="yes">
<content type="name" lang="pol"><![CDATA[PIES]]></content>
<content type="menu_1" nested="yes">
<content type="name" lang="pol"><![CDATA[Thing1]]></content>
<content type="name" lang="pol"><![CDATA[Thing2]]></content>
<content type="name" lang="pol"><![CDATA[Thing3]]></content>
</content>
</content>
</contents>
有人能帮忙吗?:)使用XPath 1.0如果将
PIES\
连接到Thing*
,则无法获得所需的所有节点,您需要使用三个XPath表达式,如下所示:
concat( //content[@type='menu_0']/content[@type='name']/text(), '\', //content[@type='menu_0']/content[@type='menu_1']/content[1]/text())
concat( //content[@type='menu_0']/content[@type='name']/text(), '\', //content[@type='menu_0']/content[@type='menu_1']/content[2]/text())
concat( //content[@type='menu_0']/content[@type='name']/text(), '\', //content[@type='menu_0']/content[@type='menu_1']/content[3]/text())
使用XPath 2.0,您可以使用for循环在一个XPath表达式中返回列表,如下所示:
for $thing in //content[@type='menu_0']/content[@type='menu_1']/content/text()
return concat( //content[@type='menu_0']/content[@type='name']/text(), '\', $thing )
这不是一个很好的解析结构。下面是这些元素组的多个实例吗?我可以使用//content[@type='menu\u 1']/*/text()提取子节点,但是,我认为您不能预先附加“派”带straigt xpath的字符串。@OldProgrammer我只是想处理现有的XML,但我想让它快一点:)谢谢它工作起来并不完美,但对我来说它是一个开始:)
for $thing in //content[@type='menu_0']/content[@type='menu_1']/content/text()
return concat( //content[@type='menu_0']/content[@type='name']/text(), '\', $thing )