Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml xpath 1.0第一个和最后一个节点_Xml_Xslt_Xpath - Fatal编程技术网

Xml xpath 1.0第一个和最后一个节点

Xml xpath 1.0第一个和最后一个节点,xml,xslt,xpath,Xml,Xslt,Xpath,如何使用xpath 1.0语句仅选择第一个和最后一个节点 车辆/汽车[position()=1]工作正常 车辆/汽车[position()=last]工作正常 然而,当我尝试将它们放在一起时,它只返回第一个结果,而忽略了第二个条件。 我已经厌倦了我认为应该起作用的东西: Vehicle/Car[position() = 1 or position() = last()] 但这只是回到了第一个位置 我也尝试过其他方法,例如: Vehicle/Car[position() = 1][positio

如何使用xpath 1.0语句仅选择第一个和最后一个节点

车辆/汽车[position()=1]
工作正常

车辆/汽车[position()=last]
工作正常

然而,当我尝试将它们放在一起时,它只返回第一个结果,而忽略了第二个条件。 我已经厌倦了我认为应该起作用的东西:

Vehicle/Car[position() = 1 or position() = last()]
但这只是回到了第一个位置

我也尝试过其他方法,例如:

Vehicle/Car[position() = 1][position() = last()]
这一切似乎只返回第一个,而不是第一个和最后一个

根据要求添加了*示例输入和输出

输入示例:

<Vehicle>
    <Car>
        <CarNumber>123</CarNumber>
    </Car>
    <Car>
        <CarNumber>456</CarNumber>
    </Car>
    <Car>
        <CarNumber>789</CarNumber>
    </Car>
</Vehicle>
<xsl:template match="/">
    <xsl:value-of select="Vehicle/Car[position() = 1 or position() = last()]"/>
</xsl:template>
实际输出:

123789
123
xslt:

<Vehicle>
    <Car>
        <CarNumber>123</CarNumber>
    </Car>
    <Car>
        <CarNumber>456</CarNumber>
    </Car>
    <Car>
        <CarNumber>789</CarNumber>
    </Car>
</Vehicle>
<xsl:template match="/">
    <xsl:value-of select="Vehicle/Car[position() = 1 or position() = last()]"/>
</xsl:template>

此表达式

Vehicle/Car[position() = 1 or position() = last()]
这是正确的。我怀疑问题在于你如何使用结果。也许您正在使用一个类似
xsl:value of
的构造(在XSLT1.0中),它会丢弃除第一个元素以外的所有元素。

而不是:

<xsl:template match="/">
    <xsl:value-of select="Vehicle/Car[position() = 1 or position() = last()]"/>
</xsl:template>

尝试:



如前所述,在XSLT 1.0
xsl:value of
中,仅返回所选节点集中第一个节点的值。

您的第一个代码运行良好我制作xml
Opel Mazda Chevrolet
xpath返回
Element='Opel'Element='Chevrolet'
请在
xpath
问题中包含示例
xml
。如果您正在使用XSLT-1.0,这个xpath位于
xsl:value of
中,它只会给出第一个值。请尝试使用XSLT-2.0并同时使用这两个值。@gardni请向我们展示输入、预期输出和完整代码。这看起来像是我遇到的问题,因为我正在用XSLT测试xpath。xpath语法是正确的,但xslt有问题吗?