2

我正在使用 Eclipse 运行 XSL 2.0 (XPATH 2.0),并且我有以下来源:

<testTop>
    <Level1 id="abc" Text="from 1-2"/>
    <Level1 id="pqr" Text="from 3-44" />
    <Level1 id="xyz" Text="from 49-101" />
</testTop>

当我在 Eclipse 中测试以下表达式时, //*[matches(@Text, '\d+-\d+')] 我得到了正确的节点,但不是 Text 属性本身

Level1 ID=abc
Level1 ID=pqr
Level1 ID=xyz

...而//@Text给我 Text 属性。谁能帮我理解为什么?我想获取 Text 属性值并使用字符串函数解析它们。最终结果应如下所示:

<output originalText="from 1-2" value1="1" value2="2" />

我不应该获得每个匹配节点的所有属性吗?

4

1 回答 1

1

Your XPath is selecting the elements that have the attributes you want. If you want to select the @Text that match your pattern, you need to adjust your XPath to select the attributes rather than the element.

You could use this XPath:

//*[matches(@Text, '\d+-\d+')]/@Text

or this XPath:

//*/@Text[matches(., '\d+-\d+')]
于 2011-07-27T01:51:33.477 回答