This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Simple XSL problem: parent attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Simple XSL problem: parent attributes" Watch "Simple XSL problem: parent attributes" New topic

Simple XSL problem: parent attributes

Dave Flynn

Joined: May 09, 2001
Posts: 1
My XML looks like this:

<A name"alpha">
<B id="0">zero</B>
<B id="1">one</B>

What I want to output is this:


If I use some XSL to style this...

<xsl:for-each select="A\B">
<xsl:value-of select="???" />:<xsl:value-of select="." />

...what do I use in ??? to select the parent attribute @name? What if B has an attribute @name too? Are there any other ways to do this that you can think of?

*had to disable similies :P
[This message has been edited by Dave Flynn (edited May 09, 2001).]
Mapraputa Is
Leverager of our synergies

Joined: Aug 26, 2000
Posts: 10065
Dave, you can use <xsl:value-of select="../@name"/> construction
".." is a shorthand for parent axis, so "../@name" will select name attribute of parent element only.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Sanjay Mishra
Ranch Hand

Joined: Jul 08, 2000
Posts: 84
Hi Dave,
I am posting both xml file and xsl file which will produce
the result you want. This is based upon what Mapraputa has said.
XML File: "1.xml"
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="1.xsl"?>
<A name="alpha">
<B id="0">zero</B>
<B id="1">one</B>
XSL File: "1.xsl"
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="">
<xsl:template match="/">
<xsl:apply-templates />
<xsl:template match="A">
<xsl:template match="B">
<xsl:value-of select="../@name"/> :
<xsl:value-of select="."/>
I agree. Here's the link:
subject: Simple XSL problem: parent attributes
It's not a secret anymore!