And*_*ndi 6 xpath default namespaces xml-namespaces
我见过的名称空间不可知语法令我困惑.
说我有:
<root>
<parent attribute="A">A<child>A</child></parent>
<parent attribute="B">B<child>B</child></parent>
</root>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我看到如何:
/root/parent/child/text()
Run Code Online (Sandbox Code Playgroud)
翻译为:
/*[local-name()='root']/*[local-name()='parent']/*[local-name()='child']/text()
Run Code Online (Sandbox Code Playgroud)
但我正在努力做这样的事情:
/root/parent[@attribute="A"]/child/text()
Run Code Online (Sandbox Code Playgroud)
要么:
/root/parent[text()="B"]/child/text()
Run Code Online (Sandbox Code Playgroud)
要么:
/root/parent[1]/child/text()
Run Code Online (Sandbox Code Playgroud)
这些翻译怎么样?
谢谢,
编辑:再一次:-)
<root>
<parent>
<childName>serverName</childName>
<childValue>MyServer</childValue>
</parent>
<parent>
<childName>ServerLocation</childName>
<childValue>Somewhere</childValue>
</parent>
</root>
Run Code Online (Sandbox Code Playgroud)
这是如何翻译的?
/root/parent[childName="serverName"]/childValue/text()
Run Code Online (Sandbox Code Playgroud)
Dim*_*hev 10
我见过的名称空间不可知语法令我困惑.
首先,我建议你不要使用这种语法,特别是如果它令人困惑.它也可能导致错误 - 请参阅我的答案的结尾以获取详细信息.
在命名空间中指定XPath表达式名称的标准方法是使用XPath引擎注册命名空间(请参阅相应的特定于供应商的文档),然后使用绑定到已注册命名空间的前缀(例如"x") )名字像 x:someName
关于这个主题有很多好的答案 - 不要使用其中一个.
现在,如果由于某种原因你仍然决定使用令人困惑的语法,那么:
但我正在努力做这样的事情:
/root/parent[@attribute="A"]/child/text()
用途:
/*[local-name()='root']/*[local-name()='parent' and @attribute='A']
Run Code Online (Sandbox Code Playgroud)
然后:
要么:
/root/parent[text()="B"]/child/text()
用途:
/*[local-name()='root']/*[local-name()='parent' and text()='B']
/*[local-name()='child']/text()
Run Code Online (Sandbox Code Playgroud)
然后:
要么:
Run Code Online (Sandbox Code Playgroud)/root/parent[1]/child/text()
用途:
/*[local-name()='root']/*[local-name()='parent'][1]
/*[local-name()='child']/text()
Run Code Online (Sandbox Code Playgroud)
然后:
多一个 :-)
Run Code Online (Sandbox Code Playgroud)<root> <parent> <childName>serverName</childName> <childValue>MyServer</childValue> </parent> <parent> <childName>ServerLocation</childName> <childValue>Somewhere</childValue> </parent> </root>这是如何翻译的?
Run Code Online (Sandbox Code Playgroud)/root/parent[childName="serverName"]/childValue/text()
用途:
/*[local-name()='root']
/*[local-name()='parent'][*[local-name()='childName"]='serverName']
/*[local-name()='childValue']/text()
Run Code Online (Sandbox Code Playgroud)
请注意:
如果在XML文档中存在具有相同本地名称的属于两个不同名称空间的元素,则这样的表达式可能不会选择所需节点.