问题上的XPath XSLT文件和XSLT如果声明(Question on XPATH for an

2019-09-19 18:20发布

我有以下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,如果看到的语句如果驱动器节点存在领域totalSpace。 我试图用这样的事情下面但这是不成功的。

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

谢谢你的帮助。

Answer 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表达式为它进行检查。 如果存在,所产生的节点集将是空的,空节点集评估为假。



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

希望这可以帮助



Answer 3:

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

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


文章来源: Question on XPATH for an XSLT File And XSLT If Statement
标签: xml xslt xpath