使用xpath的xml中的名称空间的另一个问题

2024-05-19 01:08:07 发布

您现在位置:Python中文网/ 问答频道 /正文

它对schema有效。但是我在使用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”是空列表。我试过stackoverflow的几个解决方案,如何在xpath中处理名称空间,但都没有成功。我将感谢任何建议如何获得“FileExchangeStore”的内容。你知道吗


Tags: orghttpschemawwwxpathxsdspringproxies
1条回答
网友
1楼 · 发布于 2024-05-19 01:08:07

尝试使用以下代码:

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)

相关问题 更多 >

    热门问题