Cannot find element with a namespace in xml

jirikadlec2

I am using the lxml library in python 3.5 to parse a xml file. The xml content is:

xml_content = """
<wps:ExecuteResponse xmlns:gml="http://www.opengis.net/gml"
xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:wps="http://www.opengis.net/wps/1.0.0"
xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wps/1.0.0 
http://schemas.opengis.net/wps/1.0.0/wpsExecute_response.xsd"
service="WPS" version="1.0.0" xml:lang="en-US" 
serviceInstance="http://192.168.2.72:5000/wps?service=WPS&amp;request=GetCapabilities" statusLocation="http://192.168.2.72:5000/output/9fbbf322-496d-11e8-9a87-0242ac110002.xml">
<wps:Process wps:processVersion="None">
  <ows:Identifier>run_checks</ows:Identifier>
  <ows:Title>Run checks process</ows:Title>
  <ows:Abstract>Process performing qc tool checks.</ows:Abstract>
</wps:Process>
<wps:Status creationTime="2018-04-26T16:19:41Z">
  <wps:ProcessSucceeded>PyWPS Process Run checks process finished</wps:ProcessSucceeded>
</wps:Status>
<wps:DataInputs>
  <wps:Input>
    <ows:Identifier>filepath</ows:Identifier>
    <ows:Title>Local filesystem path to the product to be checked.</ows:Title>
      <wps:Data>
        <wps:LiteralData dataType="string">/mnt/bubu/bebe</wps:LiteralData>
      </wps:Data>
   </wps:Input>
  <wps:Input>
    <ows:Identifier>product_type_name</ows:Identifier>
    <ows:Title>The type of the product denoting group of checks to be performed.</ows:Title>
    <wps:Data>
      <wps:LiteralData dataType="string">dummy</wps:LiteralData>
    </wps:Data>
  </wps:Input>
  <wps:Input>
    <ows:Identifier>optional_check_idents</ows:Identifier>
    <ows:Title>Comma separated identifiers of optional checks to be performed.</ows:Title>
    <wps:Data>
      <wps:LiteralData dataType="string">dummy</wps:LiteralData>
    </wps:Data>
  </wps:Input>
</wps:DataInputs>
<wps:OutputDefinitions>
  <wps:Output>
    <ows:Identifier>result</ows:Identifier>
    <ows:Title>Result of passed checks in json format.</ows:Title>
  </wps:Output>
</wps:OutputDefinitions>
<wps:ProcessOutputs>
  <wps:Output>
    <ows:Identifier>result</ows:Identifier>
    <ows:Title>Result of passed checks in json format.</ows:Title>
    <wps:Data>
      <wps:LiteralData dataType="urn:ogc:def:dataType:OGC:1.1:string"> {"dummy": {"status": "ok", "message": "Dummy check has passed.", "params":   "{'dummy_param1': 'dummy value1', 'dummy_param2': 'dummy value2'}"}}
     </wps:LiteralData>
  </wps:Data>
  </wps:Output>
</wps:ProcessOutputs>
</wps:ExecuteResponse>
"""

My python code to parse the file is:

from lxml import etree

ns = {'wps': 'http://www.opengis.net/wps/1.0.0', 
      'ows': 'http://schemas.opengis.net/ows/1.1.0'}
tree = etree.fromstring(xml_content)

# this works, the wps:Process tag is found successfully
wps_process_tag = tree.xpath('//wps:Process', namespaces=ns)
if len(wps_process_tag) > 0:
    print('wps:Process tag found!')

# this does not work and the ows:Identifier tag is not found
ows_identifier_tag = tree.xpath('//wps:Process/ows:Identifier', namespaces=ns)
if len(ows_identifier_tag) > 0:
    print('ows:Identifier tag found!')
else:
    print('ows:Identifier tag not found!')

As shown in my sample code, the wps:Process tag is found correctly. On the other hand, the ows:Identifier tag is not found although it exists right under wps:Process in the xml document. I have provided the namespaces dictionary to the tree.xpath function in order to find elements from the wps and ows namespaces. But it only finds elements that start with wps: and it cannot find elements starting with ows:

I checked the URL http://schemas.opengis.net/ows/1.1.0/ and it seems to be a valid URL.

How do I find the ows:Identifier element with lxml?

Matt Jones

In your code, you're declaring the ows namespace like this:

'ows': 'http://schemas.opengis.net/ows/1.1.0'

But in the XML the ows namespace has a different URI declared: http://www.opengis.net/ows/1.1.

Correcting the URI in your code should fix the issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jaxb: Unmarshaller cannot find element with namespace and prefix

How to find a XML child element which has a default namespace in Python?

XML Schema: Cannot find the declaration of element 'users'

XML Schema issue, cannot find declaration of element

XML namespace on child element

Typescript: Cannot find namespace

Cannot find namespace angular

Cannot find namespace 'StateContext'

Python3 xml, how can I find this non-namespace xml element?

Linq to XML - get element with namespace

Create XML element without namespace

Create an XML element with namespace in XMLDocument

Remove XML element attribute with namespace

Cannot find name inside namespace

Liquibase "Cannot find the declaration of element 'databaseChangeLog'" in xml changeLog file

Validating XML against schema - cannot find declaration of element

ErrorCode "12200" XML Validation warning: " Cannot find the declaration of element 'html'."

Dozer-Mapping.xml: Cannot find the declaration of element 'mappings'

XML Schema Validation : Cannot find the declaration of root element

Create XML element without namespace prefix inside element with namespace prefix

ElementTree cannot find element

Cannot find the element for a button

XML file, XSD schema Errors: Cannot resolve the name to a(n) 'type definition', Cannot find the declaration of element

Find element in xml

Find an XML element in Groovy

How to code a default namespace and a namespace with a prefix for single XML element in JAVA

TypeScript : Cannot find namespace 'Parse' namespace error for parse in?

Cannot write XML file with default namespace

Update Xml file with namespace in element names (PowerShell)