22

what's the right syntax to get the current node's parent node's name? I know it's about the AxisName parent but what's the right syntax? for example of the following 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>

for the element variable I should get netcdf or group. Thanks in advance.

Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
user851380
  • 339
  • 1
  • 7
  • 10

2 Answers2

41

Use:

name(..)

The .. abbreviation is a shorthand for parent::node().

Do note: Not every parent has a name. For example the document node (/) is the parent of the top element (/*) of the document and has no name.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • using that for retrive parent name in my case drive to Out Memory error, instead use name(parent::*) – Carlos Cocom Sep 09 '14 at 18:41
  • @CarlosCocom, This only shows that the particular XPath implementation you are using is buggy. It would be interesting if you can provide an XML document (preferably very short) and the exact XPath expression. – Dimitre Novatchev Sep 09 '14 at 19:03
11

name(parent::*) should do it. There's only going to be one parent, obviously.

Flynn1179
  • 11,925
  • 6
  • 38
  • 74