Xml XSLT文件和XSLT If语句的XPATH问题

Xml XSLT文件和XSLT If语句的XPATH问题,xml,xslt,xpath,Xml,Xslt,Xpath,我有以下xml文件 <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" /> <Dri

我有以下xml文件

<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 = ''" >


感谢您的帮助。

我认为您刚刚错过了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>

这里什么都没有

希望这对您有所帮助

我认为您刚刚错过了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>

这里什么都没有

希望这对您有所帮助

您需要编写完整路径才能使其正常工作。否则,处理器将如何知道您所指的内容

与当前相比,最小的变化如下:

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

这是:

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

您需要写入完整路径才能使其工作。否则,处理器将如何知道您所指的内容

与当前相比,最小的变化如下:

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

这是:

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

如果要查找该级别的所有
totalSpace
属性之和,可以使用

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

如果要查找该级别的所有
totalSpace
属性的总和,可以使用

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


什么是正确的XPATH?您有两个totalSpace属性,并且您取消了/VolumeGroup.Correct XPATH,原因是什么?您有两个totalSpace属性,并且您取消了/VolumeGroup。