tags:

views:

244

answers:

3

I have the following xml file

<DriveLayout>
<Drive driveVolume="/u" Group="sa" Owner="sa" totalSpace="4" />
<Drive driveVolume="/u" Group="sa" Owner="sa" totalSpace="16" />
<Drive driveVolume="/u" Group="sa" Owner="sa" totalSpace="510" />
<Drive driveVolume="/u" Group="sa" Owner="sa" />
<Drive driveVolume="/u" Group="sa" Owner="sa" totalSpace="15" />
<VolumeGroups>
<VolumeGroup storage="1" />
<VolumeGroup totalSpace="32" />
<VolumeGroup totalSpace="16" />
</VolumeGroups>
</DriveLayout>

I'm trying to access it using an xslt stylesheet which looks something like this.

    <td class="LabelText" Width="10%">
      <xsl:value-of select="/DriveLayout/VolumeGroups/@totalSpace" />
    </td>

This doesn't seem to be correct does anyone know what the correct XPATH would be?

Also, I want to use an xslt if statement to see if the field totalSpace exists in the Drive node. I tried using something like this below but this was unsuccessful.

<xsl:if test="@totalSpace = ''" >

Thanks for any help.

+2  A: 

I think you just missed out one level on your XPath, and for the existence of the attribute you could you the example below:

<xsl:template match="/DriveLayout/VolumeGroups/VolumeGroup">
    <xsl:choose>
        <xsl:when test="not(@totalSpace)">
            There's nothing here
        </xsl:when>
        <xsl:otherwise>
            <td>
                 <xsl:value-of select="@totalSpace" />
            </td>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

hope this helps

chermosillo
+1  A: 

You need to write full paths to make it work. How else would the processor know what you refer to.

The minimum changes from what you currently have would be this:

<td class="LabelText" Width="10%">
  <xsl:value-of select="/DriveLayout/VolumeGroups/VolumeGroup/@totalSpace[1]" />
</td>  <!-- you need to write full paths! -------^^^^^^^^^^^^ -->

and this:

<td class="LabelText" Width="10%">
  <xsl:value-of select="/DriveLayout/VolumeGroups/VolumeGroup/@totalSpace[2]" />
</td>

and this:

<xsl:if test="/DriveLayout/Drive/@totalSpace">
  <!-- ... -->
</xsl:if>

The existence of a node can be checked simply by writing an XPath expression for it. If does not exist, the resulting node-set will be empty, and empty node-sets evaluate to false.

Tomalak
A: 

If you're looking for the sum of all totalSpace attributes at that level, you can use something like

<xsl:value-of select="sum(/DriveLayout/VolumeGroups/VolumeGroup/@totalSpace)"/>
Harry Lime