XML namespaces and Python parsing methods

Source: Internet
Author: User

In XML, the element name is defined by the developer, and when two different documents use the same element name, a naming conflict occurs, and the XML namespace provides a way to avoid element naming conflicts.

The XML namespace attribute is placed in the start tag of the element and uses the following syntax:
xmlns:namespace-prefix= "NamespaceURI"
When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.
Note: The address used to mark the namespace is not used by the parser to find information. Its only function is to give the namespace a unique name.

In addition to explicit definitions, defining a default namespace for an element allows us to omit the work of using prefixes in all child elements. This means that all tags without a prefix will have a default namespace
Please use the following syntax:
Xmlns= "NamespaceURI"

When parsing an XML file with namespaces, be aware that the name of the tag is a combination of a namespace and a label, such as the following XML file:

<?xml version= ' 1.0 ' encoding= ' UTF-8 '? ><NVD xmlns:vuln= "Http://bulabula" xmlns:cvss= "Http://abulaabula" xmlns= "Http://alulalula" >  <entry id= "cve-2011-0001" >     <vuln:cvss>      <cvss:base_ metrics>        <cvss:score>5.0</cvss:score>      </cvss:base_metrics>    </vuln:cvss>  </entry></nvd>

Where the default namespace is xmls, two namespaces Xmlns:vuln and Xmlns:cvss are also declared. If you use Python's ElementTree parsing, the following code is wrong

Node.find (' score '). Text
When running, the error will be as follows:
Attributeerror: ' Nonetype ' object has no attribute ' text '
In other words, Node.find (' score ') did not find any results.

The correct way to access this should be to add a namespace:

Cvss = "{Http://abulabula}" Node.find (Cvss + ' score ')
For examples of entry that do not have an explicit prefix, they are implicitly prefixed with xmlns, so the value of xmlns should be added when accessing


XML namespaces and Python parsing methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.