2

I have a set of XPATHS for this XML format. The problem is that sometimes, it has specified namespace.

<?xml version="1.0" encoding="UTF-8"?>
<SHOP xmlns="http://www.zbozi.cz/ns/offer/1.0">
<SHOPITEM>
    <PRODUCTNAME>Crysta váza 4 CM</PRODUCTNAME>
    <DESCRIPTION><![CDATA[Český producent je vážený po celém světě

I can't just do this in this case to get all SHOPITEM items:

root.xpath('//SHOPITEM')

I need to specify namespace and then prefix every TAG.

root.xpath('//g:SHOPITEM',namespaces={'g':'http://www.zbozi.cz/ns/offer/1.0'})

Becasue when I do this:

root.xpath('//SHOPITEM',namespaces={'':'http://www.zbozi.cz/ns/offer/1.0'})

It says:

empty namespace prefix is not supported in xpath

But I can't just prefix all TAGS with 'g'. Is there a better way to do that? Make lxml tree to act like there was no namespace or just automatically parse it?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • Check it out here: https://stackoverflow.com/questions/34009992/python-elementtree-default-namespace Seems like a duplicate question. – Yitzhak Khabinsky Feb 17 '20 at 19:08
  • _"But I can't just prefix all TAGS with 'g'. Is there a better way to do that?"_ No. That's the proper way for dealing with XML Namespaces. If your process consumes document with and without namespace, then it consumes two different XML vocabularies and it should be reflected into the source code. If your process consumes only element from an XML vocabulary with a predefined namespace URI, that also should be reflected into the source code with the proper binding between the URI and **any prefix** you want. – Alejandro Feb 18 '20 at 21:49

0 Answers0