user1055483
user1055483

Reputation: 845

XSLT How to get an attribute value of the following-sibling node?

Supose I have this XML:

<Items>
  <Car name="12">Mercedes</Car>
  <Bike name="23">Gt</Bike>
  <House name="gt">123</House>
  <Skate name="as">111</Skate>
  <Plane name="bb">5522</Plane>
  <tv name="sss">Sony</tv>
</Items>

And the following XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Items">
    <table>
      <xsl:for-each select="*[position() mod 2 != 0]">
        <tr>
          <td>
            <xsl:value-of select="name()"/>
          </td>
          <td>
            <xsl:value-of select="."/>
          </td>
          <td>
            <xsl:value-of select="name(following-sibling::*)"/>
          </td>
          <td>
            <xsl:value-of select="following-sibling::*"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

The output Im getting is:

    <table>
  <tr>
    <td>Car</td>
    <td>Mercedes</td>
    <td>Bike</td>
    <td>Gt</td>
  </tr>
  <tr>
    <td>House</td>
    <td>123</td>
    <td>Skate</td>
    <td>111</td>
  </tr>
  <tr>
    <td>Plane</td>
    <td>5522</td>
    <td>tv</td>
    <td>Sony</td>
  </tr>
</table>

But what I need is the attribute @name instead of the node name... how can I do that?? What I need is this:

<table>
  <tr>
    <td>12</td>
    <td>Mercedes</td>
    <td>23</td>
    <td>Gt</td>
  </tr>
  <tr>
    <td>gt</td>
    <td>123</td>
    <td>as</td>
    <td>111</td>
  </tr>
  <tr>
    <td>bb</td>
    <td>5522</td>
    <td>ss</td>
    <td>sony</td>
  </tr>
</table>

I know that in the first <td> I can use @name, but how can I get the attribute "name" of the following sibling node in the other <td>?

Upvotes: 1

Views: 11564

Answers (1)

Justin Lessard
Justin Lessard

Reputation: 11961

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

This should return the name attribute of the following sibling.

Edit


It appears

 <xsl:value-of select="following-sibling::*[1]/@name"/>

Is the correct way to do it.

Upvotes: 4

Related Questions