kgjunk
kgjunk

Reputation: 39

XSLT Retrieving Attribute Info to Display in table

Hi I'm new to XSLT and I'm trying to display the value of a parent node along with the values of my data.

I have this XML..

<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
<DataView Client="Client1" ID="1000" TimeStamp="12/7/2011 5:35:09 PM">
<Group ID="5000" Name="GroupName1">
<SubGroup ID="7000" Order="0" Name="NameIWant1">
<Data ID="1" Name="DataName1" Order="0">1</Data>
<Data ID="2" Name="DataName2" Order="0">2</Data>
<Data ID="3" Name="DataName3" Order="0">3</Data>
<Data ID="12" Name="DataName4" Order="0">4</Data>
</SubGroup>
<SubGroup ID="8000" Order="0" Name="NameIWant2">
<Data ID="1" Name="DataName1" Order="0">6</Data>
<Data ID="2" Name="DataName2" Order="0">7</Data>
<Data ID="3" Name="DataName3" Order="0">8</Data>
<Data ID="12" Name="DataName4" Order="0">9</Data>
</SubGroup>
</Group>
</DataView>

Ive written the basic XSL to walk the values

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>My Data</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>DataName1</th>
      <th>DataName2</th>
      <th>DataName3</th>
      <th>DataName4</th>
      </tr>
      <xsl:for-each select="DataView/Group/SubGroup">
      <tr>

        <xsl:for-each select="Data">
                <td><xsl:value-of select="."/></td>
        </xsl:for-each>

      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

How do I retrieve and display the attribute value of Subgroup "Name" so my table looks like this...

MyData
NameIWant1 1 2 3 4
NameIWant2 6 7 8 9

Any help is very much appreciated!!

Upvotes: 2

Views: 436

Answers (2)

Wayne
Wayne

Reputation: 60414

The simple, short answer is to add the following just before the inner for-each loop:

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

You're already in the context of a DataView/Group/SubGroup node, so you just need to use the attribute axis specifier (@) to select one of its attributes by name.

However, (as usual) I think this is better expressed using individual templates. The for-each loop is almost never necessary in XSLT. The following stylesheet produces the desired result:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <html>
            <body>
                <h2>My Data</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Subgroup</th>
                        <th>DataName1</th>
                        <th>DataName2</th>
                        <th>DataName3</th>
                        <th>DataName4</th>
                    </tr>
                    <xsl:apply-templates/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="SubGroup">
        <tr>
            <td><xsl:value-of select="@Name"/></td>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="Data">
        <td><xsl:apply-templates/></td>
    </xsl:template>
</xsl:stylesheet>

Output:

<html>
   <body>
      <h2>My Data</h2>
      <table border="1">
         <tr bgcolor="#9acd32">
            <th>Subgroup</th>
            <th>DataName1</th>
            <th>DataName2</th>
            <th>DataName3</th>
            <th>DataName4</th>
         </tr>
         <tr>
            <td>NameIWant1</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
         </tr>
         <tr>
            <td>NameIWant2</td>
            <td>6</td>
            <td>7</td>
            <td>8</td>
            <td>9</td>
         </tr>
      </table>
   </body>
</html>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167516

<table border="1">
 <thead>
  <tr bgcolor="#9acd32">
    <th>SubGroupName</th>
    <th>DataName1</th>
  <th>DataName2</th>
  <th>DataName3</th>
  <th>DataName4</th>
  </tr>
 </thead>
 <tbody>
  <xsl:for-each select="DataView/Group/SubGroup">
  <tr>
    <td>
      <xsl:value-of select="@Name"/>
    </td>
    <xsl:for-each select="Data">
            <td><xsl:value-of select="."/></td>
    </xsl:for-each>

  </tr>
  </xsl:for-each>
 </tbody>
</table>

Upvotes: 0

Related Questions