Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath - context node evaluation problem

Reply
Thread Tools

XPath - context node evaluation problem

 
 
Thomas Schmidt
Guest
Posts: n/a
 
      01-09-2007
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

 
Reply With Quote
 
 
 
 
Philippe Poulard
Guest
Posts: n/a
 
      01-09-2007
hi,

Thomas Schmidt wrote:
>
> //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 !
 
Reply With Quote
 
 
 
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      01-09-2007
* 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.
--
Björn Höhrmann · (E-Mail Removed) · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      01-09-2007
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


"Thomas Schmidt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      01-10-2007
Thomas Schmidt wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Context Node in XPath 1.0 Tim Hallwyl XML 9 11-25-2007 02:18 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments