The namespace agnostic syntax I've seen around is confusing me.
Say I have:
<root>
<parent attribute="A">A<child>A</child></parent>
<parent attribute="B">B<child>B</child></parent>
</root>
So far I see how:
/root/parent/child/text()
translates to:
/*[local-name()='root']/*[local-name()='parent']/*[local-name()='child']/text()
but i'm struggling with things like this:
/root/parent[@attribute="A"]/child/text()
or:
/root/parent[text()="B"]/child/text()
or:
/root/parent[1]/child/text()
How do these translate?
Thanks,
EDIT: One More :-)
<root>
<parent>
<childName>serverName</childName>
<childValue>MyServer</childValue>
</parent>
<parent>
<childName>ServerLocation</childName>
<childValue>Somewhere</childValue>
</parent>
</root>
How does this translate?
/root/parent[childName="serverName"]/childValue/text()
The namespace agnostic syntax I've seen around is confusing me.
First, I would advise you not to use this syntax, especially if it is confusing. It can also result in errors -- see the end of my answer for details.
The standard way to specify in an XPath expression names that are in a namespace is to register a namespace with your XPath engine (see the respective, vendor-specific documentation) and then to use the prefix bound to the registered namespace (say "x") with names like x:someName
There are plenty of good answers on this topic -- jus t use one of them.
Now, if due to some reason you still decide to use the confusing syntax, then:
but i'm struggling with things like this:
/root/parent[@attribute="A"]/child/text()
Use:
/*[local-name()='root']/*[local-name()='parent' and @attribute='A']
then:
or:
/root/parent[text()="B"]/child/text()
Use:
/*[local-name()='root']/*[local-name()='parent' and text()='B']
/*[local-name()='child']/text()
then:
or:
/root/parent[1]/child/text()
Use:
/*[local-name()='root']/*[local-name()='parent'][1]
/*[local-name()='child']/text()
then:
One More :-)
<root> <parent> <childName>serverName</childName> <childValue>MyServer</childValue> </parent> <parent> <childName>ServerLocation</childName> <childValue>Somewhere</childValue> </parent> </root>
How does this translate?
/root/parent[childName="serverName"]/childValue/text()
Use:
/*[local-name()='root']
/*[local-name()='parent'][*[local-name()='childName"]='serverName']
/*[local-name()='childValue']/text()
Do note:
Such expressions may not select the wanted nodes if in the XML documents there are elements with the same local-name that belong to two different namespaces.