0

I'm parsing Google products XML file that has "g" and also empty namespace.

<feed ...
    <entry>
        <g:id>2</g:id>
        <title>Some product

Trying to use namespaces attribute while getting xpath:

tree.xpath('/feed/entry/g:title',namespaces:{'': 'http://www.w3.org/2005/Atom', 'g': 'http://base.google.com/ns/1.0'})

which returns

empty namespace prefix is not supported in XPath

how to use the empty namespace?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • 1
    Can you edit your question and show a short, well formed, representative sample of the xml? – Jack Fleeting May 24 '21 at 00:59
  • 1
    Does this answer your question? [how do I use empty namespaces in an lxml xpath query?](https://stackoverflow.com/questions/8053568/how-do-i-use-empty-namespaces-in-an-lxml-xpath-query) – fakedad May 24 '21 at 02:18
  • Also—the [lxml documentation](https://lxml.de/dev/xpathxslt.html#namespaces-and-prefixes) says that XPath does not support default namespaces. Hence, the approach taken in the accepted answer to the question linked is necessary. – fakedad May 24 '21 at 02:20
  • I suspect those elements are not truly in an empty namespace. Could you post the entirety of the outermost `` element? – Forensic_07 May 25 '21 at 00:17

1 Answers1

0

The important thing to remember is that the namespace prefix is merely an arbitrary prefix. It's not required to be empty, and you can make it anything you want.

From context, I'm assuming this is what your XML looks like?

If so, the elements without a prefix are not in an empty namespace. They're in the http://www.w3.org/2005/Atom namespace, and you need only assign it an arbitrary prefix in your function call.

In [1]: from lxml import etree

In [2]: string = '''
   ...: <feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
   ...:     <entry>
   ...:         <g:id>2</g:id>
   ...:         <title>Some product</title>
   ...:     </entry>
   ...: </feed>
   ...: '''

In [3]: root = etree.fromstring(string)

In [4]: root.xpath('/foo:feed/foo:entry/foo:title',namespaces={'foo': 'http://www.w3.org/2005/Atom', 'bar': 'http:/
   ...: /base.google.com/ns/1.0'})
Out[4]: [<Element {http://www.w3.org/2005/Atom}title at 0x10a3d7d40>]

In [5]: root.xpath('/foo:feed/foo:entry/bar:id',namespaces={'foo': 'http://www.w3.org/2005/Atom', 'bar': 'http://b
    ...: ase.google.com/ns/1.0'})
Out[5]: [<Element {http://base.google.com/ns/1.0}id at 0x10a9ed4c0>]
Forensic_07
  • 1,125
  • 1
  • 6
  • 10