xslt of nested elements

W

webwesen

hello,

an xsl virgin here... please be gentle...

got the following xml (PMD output):
================================
<file name="abc.java">
<violation line="108">
Avoid unnecessary return statements
</violation>
<violation line="179">
Avoid unnecessary return statements
</violation>
</file>
================================

and an xsl (irrelevant lines skipped):
================================
<xsl:template match="file" mode="pmd">
<xsl:value-of select="@name"/>
<xsl:apply-templates select="violation" mode="pmd"/>
</xsl:template>

<xsl:template match="violation" mode="pmd">
<xsl:value-of select="@name"/> <!-- AAA -->
<xsl:value-of select="@line"/>
</xsl:template>
================================

and the line (marked with AAA) is obviously wrong... @name belongs to
<file> and not to <violation>.
how can I reference an attribute of the parent tag from the child?
 
J

Joseph Kesselman

how can I reference an attribute of the parent tag from the child?

"..", or "parent::*", or "ancestor::*[1]", or (in this case)
"parent::file" or "ancestor::file" or...

To take the simplest solution:
<xsl:value-of select="../@name"/>
 
W

webwesen

worked as described. thanks a lot.
and sorry for a newb Q.

how can I reference an attribute of the parent tag from the child?"..", or "parent::*", or "ancestor::*[1]", or (in this case)
"parent::file" or "ancestor::file" or...

To take the simplest solution:
<xsl:value-of select="../@name"/>
 
R

roy axenov

webwesen said:
got the following xml (PMD output):
================================
<file name="abc.java">
<violation line="108">
Avoid unnecessary return statements
</violation>
<violation line="179">
Avoid unnecessary return statements
</violation>
</file>
================================

and an xsl (irrelevant lines skipped):
================================
<xsl:template match="file" mode="pmd">
<xsl:value-of select="@name"/>
<xsl:apply-templates select="violation" mode="pmd"/>
</xsl:template>

<xsl:template match="violation" mode="pmd">
<xsl:value-of select="@name"/> <!-- AAA -->
<xsl:value-of select="@line"/>
</xsl:template>
================================

and the line (marked with AAA) is obviously wrong...
@name belongs to <file> and not to <violation>.
how can I reference an attribute of the parent tag
from the child?

Parent node, not 'tag'. Naturally, you need to use the
parent axis:

<xsl:value-of select="parent::*/@name"/>

or, using a shortcut:

<xsl:value-of select="../@name"/>

Reading an XPath tutorial would've gotten you there in five
minutes or so.
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top