如何在 XPATH 2.0 中使用“<<”运算符?

How to use "<<" operator in XPATH 2.0?

我正在研究 XPath 2.0 中节点比较的概念。 我正在使用前置运算符 << 并收到以下错误:

The value of attribute "select" associated with an element type "xsl: sequence" must not contain the '<' character.

我尝试使用 XPath 2.0 先于运算符

<xsl:sequence select="/Root/*[../H1[2] << .]"/>

下面是我试过的代码。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<H1>first</H1>
<p>Test</p>
<H1>second</H1>
<p/>
<p/>
<H1/>
<p/>
<p/>
</Root>

Processing:我正在使用 << 运算符选择前面的元素。

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <xsl:sequence select="/Root/*[../H1[2] << .]"/>     
    </xsl:template>
    </xsl:stylesheet>

问题:

The value of attribute "select" associated with an element type "xsl:sequence" must not contain the '<' character.

预期结果: 使用<<运算符查找H1[2]元素的前一个元素。

XPath 语法是 << 但在 XML/XSLT 文档中您需要将其转义为 &lt;&lt;.

添加到 Martin 的回答中,如果你想避免将 << 不可读地转义为 &lt;&lt;,你可以简单地切换 << 运算符的参数顺序和使用 >> (不需要转义),因此:

而不是

<xsl:sequence select="/Root/*[../H1[2] &lt;&lt; .]"/>

使用:

<xsl:sequence select="/Root/*[. >> ../H1[2]]"/>