多层次元素的 XPath?

XPath for elements at multiple levels?

我有一个关注者XML

<?xml version="1.0" encoding="UTF-8"?>
<stationary>
    <textbook>
        <name>test</name>
    </textbook>
    <notebook>
        <books>
            <name>test</name>
        </books>
    </notebook>
</stationary>

我正在尝试获取所有 name 元素,而不管它们在 stationary[=14 中的位置=]

我试过使用以下语法但没有用:

stationary/*/name/text()

只需将以下表达式与相对路径一起使用:

//name

这似乎捕获了 XML 示例中的两个 <name> 标签:

Element='<name>test</name>'
Element='<name>test</name>'

试试这个:

'stationary//name/text()'

你的 XPath,

/stationary/*/name/text()

只会 select textbook 下的 name 元素中包含的文本节点,因为 /* select 是一个 child 元素,但另一个 name 元素是 stationary 孙子 ,而不是它的子元素。

最简单的更改是将 /*/ 替换为 //(如 @GillesQuenot, +1),

/stationary//name/text()

这将 select 沿着 descendant-or-self 轴,所以它将 select 孙子,你将得到两个 name 元素' text()节点。

请注意,您说您正在尝试获取 name 个元素,因此从技术上讲,您应该删除 text() 步骤,

/stationary//name

这将 select stationary 元素的所有 name 元素后代。然后,作为最后的说明,此 XPath(如@TimBiegeleisen 所述,+1),

//name

将 select 文档中的所有 name 元素,而不考虑根元素。