2009-07-20 48 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)"/>