多层次元素的XPath?

多层次元素的XPath?,xpath,xpath-2.0,Xpath,Xpath 2.0,我有以下XML <?xml version="1.0" encoding="UTF-8"?> <stationary> <textbook> <name>test</name> </textbook> <notebook> <books> <name>test</name> <

我有以下XML

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

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

//name
这似乎捕获了XML示例中的两个
标记:

Element='<name>test</name>'
Element='<name>test</name>'
Element='test'
元素class='test'
试试这个:

'stationary//name/text()'
你的XPath

/stationary/*/name/text()
将仅选择
教科书
名称
元素中包含的文本节点,因为
/*
选择子元素,但另一个
名称
元素是
静止
的孙子,而不是它的子元素

最简单的更改是将
/*/
替换为
/
(如 @吉列斯昆诺,+1)

它将沿子代轴或自轴进行选择,因此它将选择子代,您将获得两个
name
元素的
text()
节点

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

/stationary//name
它将选择
静止
元素的所有
名称
元素后代。最后一点,这个XPath(正如@TimBiegeleisen所提到的,+1)

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

/stationary//name/text()
/stationary//name
//name