xpath xml 中命名空间的另一个问题
Another issue with namespaces in xml with xpath
并且它对架构有效。但是我在 python.
中使用 xpath 时遇到问题
<spring:beans xmlns="http://membrane-soa.org/proxies/1/"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd">
<fileExchangeStore
id="myFileExchangeStore"
dir="./exchanges"
maxDays="30" />
我写了这部分脚本:
from lxml import etree
a = etree.parse("output.xml")
r=a.xpath('//empty:beans',
namespaces= { 'empty':"http://membrane-soa.org/proxies/1/",
'spring':"http://www.springframework.org/schema/beans",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
'schemaLocation':"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"})
现在我被这个 xpath 困住了。在这种情况下 "r" 是空列表。我尝试了 Whosebug 的几种解决方案,如何处理 xpath 中的名称空间,但没有成功。对于如何获取 "fileExchangeSotre".
内容的任何建议,我将不胜感激
尝试使用下面的代码:
namespaces= { 'empty':"http://membrane-soa.org/proxies/1/",
'spring':"http://www.springframework.org/schema/beans",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
'schemaLocation':"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"}
r = a.xpath('/spring:beans/*[local-name()="fileExchange"]/@*', namespaces=namespaces)
并且它对架构有效。但是我在 python.
中使用 xpath 时遇到问题<spring:beans xmlns="http://membrane-soa.org/proxies/1/"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd">
<fileExchangeStore
id="myFileExchangeStore"
dir="./exchanges"
maxDays="30" />
我写了这部分脚本:
from lxml import etree
a = etree.parse("output.xml")
r=a.xpath('//empty:beans',
namespaces= { 'empty':"http://membrane-soa.org/proxies/1/",
'spring':"http://www.springframework.org/schema/beans",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
'schemaLocation':"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"})
现在我被这个 xpath 困住了。在这种情况下 "r" 是空列表。我尝试了 Whosebug 的几种解决方案,如何处理 xpath 中的名称空间,但没有成功。对于如何获取 "fileExchangeSotre".
内容的任何建议,我将不胜感激尝试使用下面的代码:
namespaces= { 'empty':"http://membrane-soa.org/proxies/1/",
'spring':"http://www.springframework.org/schema/beans",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
'schemaLocation':"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"}
r = a.xpath('/spring:beans/*[local-name()="fileExchange"]/@*', namespaces=namespaces)