Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the parent node attributes in XSL

Tags:

xslt

nodes

parent

In my XML I have the following:

<a>
  <b>
    <c something="false">
      <d>
        <e>
          <f>someResult</f>
        </e>
      </d>
    </c>
  </b>
</a>

Now in the XSL within a loop I can do the following:

<xsl:value-of select="f"></xsl:value-of>

But how can I get the attribute in c?

I've tried doing the following

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

As well as trying parent and nothing seems to be working. Can you get parent nodes like this?

Also, I cannot just do:

<xsl:value-of select="/a/b/c/@something"></xsl:value-of>

As there can be multiple of c.

like image 779
ingh.am Avatar asked Feb 28 '12 16:02

ingh.am


People also ask

How do I navigate to parent node in XPath?

A Parent of a context node is selected Flat element. A string of elements is normally separated by a slash in an XPath statement. You can pick the parent element by inserting two periods “..” where an element would typically be. The parent of the element to the left of the double period will be selected.

What is XSL attribute?

The xsl:attribute element is used to add an attribute value to an xsl:element element or literal result element, or to an element created using xsl:copy. The attribute must be output immediately after the element, with no intervening character data.

What is string XSLT?

XSLT string length is defined as a string function and takes a single string argument and returns an integer value representing the number of characters in the string. It converts any declared type into a string except that an empty parenthesis cannot be converted. The whitespaces are taken into the count.

What does the purpose of the XSL value of block what does XSL refer to?

<? xml version="1.0" encoding="UTF-8"?>


2 Answers

To move up the tree you use ".." per level ie in this instance probably

select="../../../@something" 

You can also select an ancestor node by name (approx)

select="ancestor::c[1]/@something"   

See http://www.stackoverflow.com/questions/3672992 for further examples

like image 197
kaj Avatar answered Sep 23 '22 16:09

kaj


Use:

ancestor::c[1]/@something 

This selects the attribute named something of the first (from the current node upwards) ancestor named c.

like image 44
Dimitre Novatchev Avatar answered Sep 21 '22 16:09

Dimitre Novatchev