Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用XPath是否可以将目标节点路径中每个节点的名称作为字符串获取?_Xml_Xpath_Xpath 2.0 - Fatal编程技术网

Xml 使用XPath是否可以将目标节点路径中每个节点的名称作为字符串获取?

Xml 使用XPath是否可以将目标节点路径中每个节点的名称作为字符串获取?,xml,xpath,xpath-2.0,Xml,Xpath,Xpath 2.0,使用XPath是否可以将目标节点路径中每个节点的名称作为字符串获取? Example.xml 真的 真的 假的 选择目标为真的所有祖先 //节点()[target=“true”]/祖先或自身:* 有可能得到这个结果吗 “父/子/目标” “parent/childTwo/target”这个XPath 2.0表达式怎么样: string-join(for $node in //node()[text() = 'true'] return string-join(for $ancestor in

使用XPath是否可以将目标节点路径中每个节点的名称作为字符串获取?

Example.xml


真的
真的
假的
选择目标为真的所有祖先

//节点()[target=“true”]/祖先或自身:*

有可能得到这个结果吗

“父/子/目标”


“parent/childTwo/target”

这个XPath 2.0表达式怎么样:

string-join(for $node in //node()[text() = 'true'] return string-join(for $ancestor in $node/ancestor-or-self::* return concat('/',local-name($ancestor)), ''), '
')
这将返回以换行符分隔的路径字符串。如果希望为每个路径使用单独的结果节点,则可能需要使用

for $node in //node()[text() = 'true'] return string-join(for $ancestor in $node/ancestor-or-self::* return concat('/',local-name($ancestor)), '')
相反

请注意,我必须将
target='true'
更改为
text()='true'
,以包含最低级别的标记

连接部分路径字符串的技巧受以下答案启发:

for $node in //node()[text() = 'true'] return string-join(for $ancestor in $node/ancestor-or-self::* return concat('/',local-name($ancestor)), '')