How to get relative depth of XML element using XPATH

后端 未结 1 587
后悔当初
后悔当初 2021-01-22 02:35

I am trying to find the relative depth of given XML element from specific element in the given XML file, I tried to use XPATH but I\'m not very familiar with XML parsing and I\'

相关标签:
1条回答
  • 2021-01-22 03:24

    The pivotal method for getting the depth of any node is by counting its ancestors (which include the parent, the parent of the parent etc):

    count(NM109_BillingProviderIdentifier/ancestor-or-self::*)
    

    This will give you the count up to the root. To get the relative count, i.e. from anything other than the root, assuming the names do not overlap, you can do this:

    count(NM109_BillingProviderIdentifier/ancestor-or-self::*)
    - count(NM109_BillingProviderIdentifier/ancestor::TS837_2000A_Loop/ancestor::*)
    

    Depending on whether the current, or the base element should be included in the count, use the ancestor-or-self or ancestor axis.


    PS: you should probably thank Pietro Saccardi for so kindly making your post and your huge (4kB on one line..) sample XML readable.

    0 讨论(0)
提交回复
热议问题