Page 1 of 1

XPath - search by reference

Posted: Tue Oct 27, 2015 3:37 pm
by Kazuyuki
Hello!
I have a general question regarding XPath.
How can I search for the value of Object.name in data:One only by knowing the value of Object.name in data:Three, using XPath?

<data:Three abc:ID="3">
<data:Object.name>CCC</data:Object.name>
<data:Object.Container abc:resource="#2"/>
</data:Three>

<data:Two abc:ID="2">
<data:Object.name>BBB</data:Object.name>
<data:Two.Parent abc:resource="#1"/>
</data:Two>

<data:One abc:ID="1">
<data:Object.name>This is the value</data:Object.name>
</data:One>

I have searched manually using:

/abc:ABC/data:Three[data:Object.name/contains(.,'CCC')]
/abc:ABC/data:Two[@abc:ID='2']
/abc:ABC/data:One[@abc:ID='1']
/abc:ABC/data:One[@abc:ID='1']/data:Object.name

However, I would like one XPath query to search for "CCC" (first query) and then follow the abc:resource from "Three" to "One".
Alternatively find all "CCC" that are referenced by a specific data:One element.

Is this possible?

Re: XPath - search by reference

Posted: Tue Oct 27, 2015 6:07 pm
by adrian
Hi,
However, I would like one XPath query to search for "CCC" (first query) and then follow the abc:resource from "Three" to "One".
Using # in the abc:resource value (#1, #2) makes things difficult since you will have to trim the # before searching.
The abc:resource from data:Three is pinpointed like this:

Code: Select all

/abc:ABC/data:Three[data:Object.name/contains(.,'CCC')]/data:Object.Container/@abc:resource
which leads to the second expression (with data:Two/@abc:ID = @abc:resource, with trimmed# ) written like this

Code: Select all

/abc:ABC/data:Two[@abc:ID=replace(/abc:ABC/data:Three[data:Object.name/contains(.,'CCC')]/data:Object.Container/@abc:resource, '^#', '')]
and the third (all chained together to pinpoint data:Object.name) like this:

Code: Select all

/abc:ABC/data:One[@abc:ID=replace(/abc:ABC/data:Two[@abc:ID=replace(/abc:ABC/data:Three[data:Object.name/contains(.,'CCC')]/data:Object.Container/@abc:resource, '^#', '')]/data:Two.Parent/@abc:resource, '^#', '')]/data:Object.name
It's not pretty, but it works. Without the # in abc:resource, the XPath would become cleaner since you would not need the replace() functions.

Regards,
Adrian