2009-07-20 69 views
0

我有下面的XML文件问题上的XPath XSLT文件和XSLT的声明如果

<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> 

我试图使用XSLT样式表看起来像这样来访问它。

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

这似乎不正确有人知道什么是正确的XPATH会是什么?

此外,我想使用xslt if语句来查看驱动器节点中是否存在字段totalSpace。我尝试使用下面这样的东西,但这是不成功的。

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

感谢您的任何帮助。

+0

什么正确的XPATH?你有两个总空间属性,你离开/ VolumeGroup。 – 2009-07-20 20:14:40

回答

1

你需要写完整路径使其工作。处理器怎么会知道你所指的是什么。

从你目前拥有的最小变化会是这样:

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

这:

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

这:

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

节点的存在,可简单地通过编写一个XPath表达式来检查它。如果不存在,则生成的节点集将为空,并且空节点集计算为false。

2

我认为你错过了你的XPath的一个级别,而对于属性的存在,你可能在你下面的例子:

<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> 

希望这有助于

0

如果你在该水平寻找所有totalSpace属性的总和,你可以使用像

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