XPath - search by reference

Questions about XML that are not covered by the other forums should go here.
Kazuyuki
Posts: 1
Joined: Tue Oct 27, 2015 1:47 pm

XPath - search by reference

Post 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?
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: XPath - search by reference

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply