如何获取当前节点的父节点名称?

22

获取当前节点的父节点名称的正确语法是什么?我知道涉及 AxisName 父级,但是正确的语法是什么? 例如以下 XML 示例:

<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="file:/dev/null" iosp="lasp.tss.iosp.ValueGeneratorIOSP" start="0" increment="1">
    <attribute name="title" value="Vector time series"/>
    <dimension name="time" length="100"/>
    <variable name="time" shape="time" type="double">
        <attribute name="units" type="String" value="seconds since 1970-01-01T00:00"/>
    </variable>
    <group name="Vector" tsdsType="Structure" shape="time">
        <variable name="x" shape="time" type="double"/>
        <variable name="y" shape="time" type="double"/>
        <variable name="z" shape="time" type="double"/>
    </group>
</netcdf>

对于元素变量,我应该获得netcdf或组。提前致谢。

2个回答

41

用途:

name(..)

.. 缩写是 parent::node() 的简写。

需要注意的是: 并非每个父节点都有名称。例如,文档节点(/)是文档顶层元素(/*)的父节点,但没有名称。


在我的情况下,使用name(parent::*)来检索父名称,而不是使用它来避免内存错误。 - Carlos Cocom
@CarlosCocom,这只是表明你正在使用的特定XPath实现存在缺陷。如果您可以提供一个XML文档(最好非常简短)和确切的XPath表达式,那将是很有趣的。 - Dimitre Novatchev

11

name(parent::*) 应该就可以了。显然只会有一个父元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接