XPath - context node evaluation problem

T

Thomas Schmidt

Given this document:

<doc>
<head>
<someElementWithID id="X"/>
<someElementWithID id="Y"/>
</head>
<body>
<element>
<otherElement>
<elementWithIDREF idref="X"/>
</otherElement>
</element>
</body>
</doc>

With the <elementWithIDREF> as a context node, I want to use an XPath
to get to the <someElementWithID> with the corresponding id-attribute.
If I use

//head/someElementWithID[@id=@idref]

I don't get what I want, because the idref-attribute is evaluated with
someElementWithID as the context. In XSLT, I use

//head/someElementWithID[@id=current()/@idref]

But "current()" is an XSLT function. Is there some way to do this in
"pure" XPath?

Any help will be greatly appreciated,

Thomas
 
P

Philippe Poulard

hi,

Thomas said:
//head/someElementWithID[@id=current()/@idref]

But "current()" is an XSLT function. Is there some way to do this in
"pure" XPath?

The XPath context consist on a node, a pair of integer (size and
position), a set of namespace bindings, a set of functions, and a set of
variables

If you dont' want to augment the set of functions, you can use a
variable instead :

//head/someElementWithID[@id=$current/@idref]

but you have to set $current before, that depends on the XPath engine
and the host language you use

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 
B

Bjoern Hoehrmann

* Thomas Schmidt wrote in comp.text.xml:
I don't get what I want, because the idref-attribute is evaluated with
someElementWithID as the context. In XSLT, I use

//head/someElementWithID[@id=current()/@idref]

But "current()" is an XSLT function. Is there some way to do this in
"pure" XPath?

Well, can you use the id() function, or variables, or insert the value
into the expression before you have it evaluated? Otherwise there is no
way, you could only check whether there is some such element using

//head/someElementWithID/@id = @idref

which is not what you want.
 
D

Dimitre Novatchev

If you can uniquely select the element with the "idref" attribute" then use
the following expression:

//someElementWithID[@id=$the-id-ref-elemennt-selecting-expression-here/@ideref]

for example, for the provided xml document this could be as simple as:

//someElementWithID[@id = /*/*/*/elementWithIDREF/@idref]


Cheers,
Dimitre Novatchev
 
D

David Carlisle

Thomas said:
With the <elementWithIDREF> as a context node, I want to use an XPath
to get to the <someElementWithID> with the corresponding id-attribute.
If I use

//head/someElementWithID[@id=@idref]

I don't get what I want, because the idref-attribute is evaluated with
someElementWithID as the context. In XSLT, I use

//head/someElementWithID[@id=current()/@idref]

But "current()" is an XSLT function. Is there some way to do this in
"pure" XPath?

Any help will be greatly appreciated,

Thomas

(in xslt its usually better to use a key rather than
/head/someElementWithID[@id=current()/@idref] not that that's relevant
to the problem here)

i suspect it's not possible in xpath1 unless you bind a variable in an
external language. or as Dimitre suggested, know a path to your
referencing node.

if xpath2 is a possibility, then you could use

for $id in @idref return
//head/someElementWithID[@id=$id]

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top