关于 XSLT 文件的 XPATH 和 XSLT If 语句的问题 [英] Question on XPATH for an XSLT File And XSLT If Statement
问题描述
我有以下 xml 文件
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>
我正在尝试使用类似这样的 xslt 样式表来访问它.
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>
这似乎不正确有人知道正确的 XPATH 是什么吗?
This doesn't seem to be correct does anyone know what the correct XPATH would be?
另外,我想使用一个 xslt if 语句来查看字段 totalSpace 是否存在于 Drive 节点中.我尝试使用下面这样的东西,但没有成功.
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 = ''" >
感谢您的帮助.
推荐答案
您需要编写完整路径才能使其工作.处理器怎么会知道你指的是什么.
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! -------^^^^^^^^^^^^ -->
还有这个:
<td class="LabelText" Width="10%">
<xsl:value-of select="/DriveLayout/VolumeGroups/VolumeGroup/@totalSpace[2]" />
</td>
还有这个:
<xsl:if test="/DriveLayout/Drive/@totalSpace">
<!-- ... -->
</xsl:if>
一个节点的存在可以简单地通过为它编写一个 XPath 表达式来检查.如果不存在,则结果节点集将为空,空节点集评估为假.
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.
这篇关于关于 XSLT 文件的 XPATH 和 XSLT If 语句的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!