简体   繁体   中英

How can I print XPaths of lxml tree elements?

I'm trying to print XPaths of all elements in XML tree, but I get strange output when using lxml. Instead of xpath which contains name of each node within path, I get strange "*"-kind of output. Do you know what might be the issue here? Here the code, as well as XML I am trying to analyze.

from lxml import etree

xml = """
<filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <bundles xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-bundlemgr-oper">
    <bundles>
      <bundle>
        <data>
            <bundle-status/>
            <lacp-status/>
            <minimum-active-links/>
            <ipv4bfd-status/>
            <active-member-count/>
            <active-member-configured/>
        </data>
        <members>
            <member>
                <member-interface/>
                <interface-name/>
                <member-mux-data>
                    <member-state/>
                </member-mux-data>
            </member>
        </members>
        <bundle-interface>{{bundle_name}}</bundle-interface>
      </bundle>
    </bundles>
  </bundles>
  <bfd xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ip-bfd-oper">
    <session-briefs>
        <session-brief>
            <state/>
            <interface-name>{{bundle_name}}</interface-name>
        </session-brief>
    </session-briefs>
  </bfd>
</filter>
"""


root = etree.XML(xml)
tree = etree.ElementTree(root)
for element in root.iter():
    print(tree.getpath(element))

The output looks like this (there should be node names instead of "*"):

/*
/*/*[1]
/*/*[1]/*
/*/*[1]/*/*
/*/*[1]/*/*/*[1]
/*/*[1]/*/*/*[1]/*[1]
/*/*[1]/*/*/*[1]/*[2]
/*/*[1]/*/*/*[1]/*[3]
/*/*[1]/*/*/*[1]/*[4]
/*/*[1]/*/*/*[1]/*[5]
/*/*[1]/*/*/*[1]/*[6]
/*/*[1]/*/*/*[2]
/*/*[1]/*/*/*[2]/*
/*/*[1]/*/*/*[2]/*/*[1]
/*/*[1]/*/*/*[2]/*/*[2]
/*/*[1]/*/*/*[2]/*/*[3]
/*/*[1]/*/*/*[2]/*/*[3]/*
/*/*[1]/*/*/*[3]
/*/*[2]
/*/*[2]/*
/*/*[2]/*/*
/*/*[2]/*/*/*[1]
/*/*[2]/*/*/*[2]

Thanks a lot! Dragan

I found that besides getpath , etree contains also a "sibling" method called getelementpath , giving proper result also for namespaced elements.

So change your code to:

for element in root.iter():
    print(tree.getelementpath(element))

For your source sample, with namespaces shortened for readability, the initial part of the result is:

.
{http://cisco.com/ns}bundles
{http://cisco.com/ns}bundles/{http://cisco.com/ns}bundles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM