我有XML数据,其结构如下。我正在尝试评估Xid值,以查看停靠点是否为多个值,例如位置1或位置2,但仅匹配它在结构中找到的第一个匹配项。然后,我需要它来输出该停靠点的“日期”值。
<ShipmentStop>
<StopSequence>1</StopSequence>
<LocationRef>
<LocationGid>
<Gid>
<Xid>LOCATION 1</Xid>
</Gid>
</LocationGid>
<ArrivalTime>
<EventTime>
<EstimatedTime>
<Date>20181128070000</Date>
</EstimatedTime>
</EventTime>
</ArrivalTime>
</ShipmentStop>
<ShipmentStop>
<StopSequence>2</StopSequence>
<LocationRef>
<LocationGid>
<Gid>
<Xid>LOCATION 2</Xid>
</Gid>
</LocationGid>
<ArrivalTime>
<EventTime>
<EstimatedTime>
<Date>20181129070000</Date>
</EstimatedTime>
</EventTime>
</ArrivalTime>
</ShipmentStop>
我正在尝试构建以下代码,但是遇到了一些问题。
<xsl:for-each select="ShipmentStop[/LocationRef/LocationGid/Gid/Xid='LOCATION 1' or /LocationRef/LocationGid/Gid/Xid='LOCATION 2'][1]">
<ArrivalTime>
<xsl:value-of select="ArrivalTime/EventTime/EstimatedTime/Date"/>
</ArrivalTime>
</xsl:for-each>
答案 0 :(得分:0)
我在下面假设格式正确的XML。我添加了一个名为root
的根节点:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<ShipmentStop>
<StopSequence>1</StopSequence>
<LocationRef>
<LocationGid>
<Gid>
<Xid>LOCATION 1</Xid>
</Gid>
</LocationGid>
</LocationRef>
<ArrivalTime>
<EventTime>
<EstimatedTime>
<Date>20181128070000</Date>
</EstimatedTime>
</EventTime>
</ArrivalTime>
</ShipmentStop>
<ShipmentStop>
<StopSequence>2</StopSequence>
<LocationRef>
<LocationGid>
<Gid>
<Xid>LOCATION 2</Xid>
</Gid>
</LocationGid>
</LocationRef>
<ArrivalTime>
<EventTime>
<EstimatedTime>
<Date>20181129070000</Date>
</EstimatedTime>
</EventTime>
</ArrivalTime>
</ShipmentStop>
</root>
您在原始xpath中使用过
ShipmentStop[/LocationRef/LocationGid/Gid/Xid='LOCATION 1'
or /LocationRef/LocationGid/Gid/Xid='LOCATION 2'][1]
请注意在/
中使用/LocationRef
。当您这样做时,您正在从XML的根节点进行搜索,因此它将失败。要使用上下文节点,只需在斜杠前添加一个句点,例如
ShipmentStop[./LocationRef/LocationGid/Gid/Xid='LOCATION 1'
or ./LocationRef/LocationGid/Gid/Xid='LOCATION 2'][1]
,或者您可以删除xpath中的./
。如:
ShipmentStop[LocationRef/LocationGid/Gid/Xid='LOCATION 1'
or LocationRef/LocationGid/Gid/Xid='LOCATION 2'][1]
因为LocationRef
是ShipmentStop
节点的子代。