简体   繁体   中英

lxml does not work when no doctype is available?

I'm trying to parse a XML with etree,

import re
from lxml   import etree
from pprint import pprint

doc = etree.parse('123.xml')
print doc.xpath('//jdbc-driver-params/url')

But no matter what xpath query I use, it doc.xpath always return an empty list

Any ideas? Attached xml

<?xml version="1.0" encoding="UTF-8"?>
<jdbc-data-source xmlns="http://xmlns.oracle.com/weblogic/jdbc-data-source">
  <name>jdbc/db_ejemplo</name>
  <jdbc-driver-params>
    <url>jdbc:mysql://localhost:3306/db_ejemplo</url>
    <driver-name>com.mysql.jdbc.Driver</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>root</value>
      </property>
      <property>
        <name>password</name>
        <value>Qwer1234</value>
      </property>
    </properties>
  </jdbc-driver-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/db_ejemplo</jndi-name>
  </jdbc-data-source-params>
</jdbc-data-source>

It looks like the namespace is causing the issue:

doc.xpath(
    '//ns:jdbc-data-source/ns:jdbc-driver-params/ns:url',
    namespaces={
        'ns':'http://xmlns.oracle.com/weblogic/jdbc-data-source'
    }
 )

yields:

[<Element {http://xmlns.oracle.com/weblogic/jdbc-data-source}url at 0x1049877a0>]

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