This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Attributr in XPath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Attributr in XPath" Watch "Attributr in XPath" New topic

Attributr in XPath

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77

First there is a XML:

Second there is my XSLT-stylesheet:

Third there is the output:

Everything works fine, there is one mistake only. In the output should not be for instance p6 or p7. There should appear the real names like Marie Dupont or Philippe Dubois. How can I achieve this?

Regards, Urs

SCJP 1.4
Matthew Brown

Joined: Apr 06, 2010
Posts: 4369

So you need to take the value of the attribute, and use that in an XPath expression to find the name of the person it matches? Try using this expression:

<xsl:value-of select="/FAMILYTREE/PERSON[@ID = current()/@MOTHER]/NAME" />

Translates as "find the PERSON with an ID attribute matching the MOTHER attribute of my current node, and take their name".
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

Or if you expect to do this on larger documents, you might want to consider an <xsl:key> element. The w3schools tutorial about that element has an example which could almost be dropped unchanged into your code.
jQuery in Action, 2nd edition
subject: Attributr in XPath