我需要匹配所有这些开始标记:
<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)
但不是这些:
<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)
我想出了这个,并希望确保我做对了.我只抓住了a-z.
<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)
我相信它说:
/,然后我有这个权利吗?更重要的是,你怎么看?
我有一个xml doc,我试图使用Etree.lxml解析
<Envelope xmlns="http://www.example.com/zzz/yyy">
<Header>
<Version>1</Version>
</Header>
<Body>
some stuff
<Body>
<Envelope>
Run Code Online (Sandbox Code Playgroud)
我的代码是:
path = "path to xml file"
from lxml import etree as ET
parser = ET.XMLParser(ns_clean=True)
dom = ET.parse(path, parser)
dom.getroot()
Run Code Online (Sandbox Code Playgroud)
当我尝试获取dom.getroot()时,我得到:
<Element {http://www.example.com/zzz/yyy}Envelope at 28adacac>
Run Code Online (Sandbox Code Playgroud)
但是我只想要:
<Element Envelope at 28adacac>
Run Code Online (Sandbox Code Playgroud)
当我做
dom.getroot().find("Body")
Run Code Online (Sandbox Code Playgroud)
我没有得到任何回报.但是,当我
dom.getroot().find("{http://www.example.com/zzz/yyy}Body")
Run Code Online (Sandbox Code Playgroud)
我得到了一个结果.
我认为将ns_clean = True传递给解析器会阻止这种情况.
有任何想法吗?
我正在寻找使用ElementTree的XML到字典解析器,我已经找到了一些,但它们排除了属性,在我的情况下,我有很多属性.
在python 2.7(使用etree 1.3)中,我可以在这样的元素上抑制XML前缀:
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as etree
>>> etree.VERSION
'1.3.0'
>>> something = etree.Element('{http://some.namespace}token')
>>> etree.tostring(something)
'<ns0:token xmlns:ns0="http://some.namespace" />'
>>> etree.register_namespace('', 'http://some.namespace')
>>> etree.tostring(something)
'<token xmlns="http://some.namespace" />'
Run Code Online (Sandbox Code Playgroud)
该register_namespace功能在1.3中添加.我试图以与版本1.2.6的python 2.6的etree兼容的方式删除前缀.这是我尝试过的:
Python 2.6.7 (r267:88850, Jul 31 2011, 19:30:54)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on …Run Code Online (Sandbox Code Playgroud) 此XML文件命名为example.xml:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>14.0.0</modelVersion>
<groupId>.com.foobar.flubber</groupId>
<artifactId>uberportalconf</artifactId>
<version>13-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Environment for UberPortalConf</name>
<description>This is the description</description>
<properties>
<birduberportal.version>11</birduberportal.version>
<promotiondevice.version>9</promotiondevice.version>
<foobarportal.version>6</foobarportal.version>
<eventuberdevice.version>2</eventuberdevice.version>
</properties>
<!-- A lot more here, but as it is irrelevant for the problem I have removed it -->
</project>
Run Code Online (Sandbox Code Playgroud)
如果我加载example.xml并使用ElementTree解析它,我可以看到它的命名空间http://maven.apache.org/POM/4.0.0.
>>> from xml.etree import ElementTree
>>> tree = ElementTree.parse('example.xml')
>>> print tree.getroot()
<Element '{http://maven.apache.org/POM/4.0.0}project' at 0x26ee0f0>
Run Code Online (Sandbox Code Playgroud)
我还没有找到一种方法来调用从而Element无需解析str(an_element)元素来获取命名空间.似乎必须有更好的方法.
有没有办法忽略tage名称中的XML命名空间elementtree.ElementTree?
我尝试打印所有technicalContact标签:
for item in root.getiterator(tag='{http://www.example.com}technicalContact'):
print item.tag, item.text
Run Code Online (Sandbox Code Playgroud)
我得到类似的东西:
{http://www.example.com}technicalContact blah@example.com
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是:
technicalContact blah@example.com
Run Code Online (Sandbox Code Playgroud)
有没有办法只显示后缀(sans xmlns),或更好 - 迭代元素而不明确说明xmlns?
我使用 python 2.7 和 ElementTree 库。
我无法使用 lxml lib。
我需要获取字符串中的名称空间namespace_string。为了填充我的命名空间字典。
我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<AX_Bestandsdatenauszug
xmlns="http://www.adv-online.de/namespaces/adv/gid/6.0"
xmlns:adv="http://www.adv-online.de/namespaces/adv/gid/6.0"
xmlns:gco="http://www.isotc211.org/2005/gco"
xmlns:gmd="http://www.isotc211.org/2005/gmd"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:ows="http://www.opengis.net/ows"
xmlns:wfs="http://www.adv-online.de/namespaces/adv/gid/wfs"
xmlns:wfsext="http://www.adv-online.de/namespaces/adv/gid/wfsext"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ogc="http://www.adv-online.de/namespaces/adv/gid/ogc"
xsi:schemaLocation="http://www.adv-online.de/namespaces/adv/gid/6.0 NAS-Operationen.xsd">
<enthaelt>
<gml:featureMember>
<xmlstuff>....a lot of xml stuff....</xmlstuff>
</gml:featureMember>
</enthaelt>
</AX_Bestandsdatenauszug>
Run Code Online (Sandbox Code Playgroud)
代码:
import clr
import sys
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")
import xml.etree.ElementTree as ET
from io import StringIO
xml="file.xml"
tree = ET.parse(xml)
root = tree.getroot()
my_schema = "namespace_string"
my_namespaces = dict([node for _, node in …Run Code Online (Sandbox Code Playgroud) 我正在使用 google 的一些数据 API,使用 python 中的 lxml 库。命名空间在这里是一个很大的麻烦。对于我正在做的很多工作(主要是 xpath 的东西),最好直接忽略它们。
有没有一种简单的方法可以忽略 python/lxml 中的 xml 命名空间?
谢谢!
给定一个如下所示的 xml 文件:
<?xml version="1.0" encoding="windows-1252"?>
<Message xmlns="http://example.com/ns" xmlns:myns="urn:us:gov:dot:faa:aim:saa">
<foo id="stuffid"/>
<myns:bar/>
</Message>
Run Code Online (Sandbox Code Playgroud)
当我用 ElementTree 解析它时,元素标签看起来像:
<?xml version="1.0" encoding="windows-1252"?>
<Message xmlns="http://example.com/ns" xmlns:myns="urn:us:gov:dot:faa:aim:saa">
<foo id="stuffid"/>
<myns:bar/>
</Message>
Run Code Online (Sandbox Code Playgroud)
但我宁愿只是
{http://example.com/ns}Message
{http://example.com/ns}foo
{urn:us:gov:dot:faa:aim:saa}bar
Run Code Online (Sandbox Code Playgroud)
更重要的是,我宁愿将“Message”、“foo”和“bar”传递给find()和findall()方法。
我已经尝试使用替换来审查/sf/answers/1094892361/ 中xmlns:建议的所有属性(如果我找不到更优雅的东西,这可能是我必须做的),并且我试过打电话,但这似乎只对 有帮助,这不是我想要的。ElementTree.register_namespace('', "http://example.com/ns")ElementTree.tostring()
难道没有办法让 ElementTree 假装它从未听说过xmlns吗?
让我们假设即使没有命名空间限定符,我的元素标签也是全局唯一的。在这种情况下,命名空间只是碍手碍脚。
详细处理一些评论:
Joe 链接到Python ElementTree 模块:How to ignore the namespace of XML files to locate matching element when using the method "find", "findall"这与我的问题非常接近,我猜我的问题是重复的。然而,这个问题也没有得到回答。那里给出的建议是:
tree.findall("xmlns:DEAL_LEVEL/xmlns:PAID_OFF", namespaces={'xmlns': 'http://www.test.com'}).
如何告诉ElementTree忽略XML文件中的命名空间?
例如,我更愿意查询modelVersion(如语句1中)而不是 {http://maven.apache.org/POM/4.0.0}modelVersion(如语句2中所示).
pom="""
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
</project>
"""
from xml.etree import ElementTree
ElementTree.register_namespace("","http://maven.apache.org/POM/4.0.0")
root = ElementTree.fromstring(pom)
print 1,root.findall('modelVersion')
print 2,root.findall('{http://maven.apache.org/POM/4.0.0}modelVersion')
1 []
2 [<Element '{http://maven.apache.org/POM/4.0.0}modelVersion' at 0x1006bff10>]
Run Code Online (Sandbox Code Playgroud) 所以我要处理一些看起来像这样的xml:
<ns2:foobarResponse xmlns:ns2="http://api.example.com">
<duration>206</duration>
<artist>
<tracks>...</tracks>
</artist>
</ns2:foobarResponse>
Run Code Online (Sandbox Code Playgroud)
我找到了lxml和它的objectify模块,它允许你以pythonic方式遍历xml文档,就像字典一样.
问题是:每次尝试访问元素时都使用伪造的xml命名空间,如下所示:
from lxml import objectify
tree = objectify.fromstring(xml)
print tree.artist
# ERROR: no such child: {http://api.example.com}artist
Run Code Online (Sandbox Code Playgroud)
它正在尝试使用<artist>父命名空间进行访问,但标记不使用ns.
任何想法如何解决这个问题?谢谢
我正在尝试从<v:imagedata r:id="rId7" o:title="1-REN"/>带有命名空间的 Word 文档中查找所有内容xmlns:v="urn:schemas-microsoft-com:vml",但我无法弄清楚语法到底是什么。
这些文档只涵盖了非常直接的情况,并且在加入了 URN 和 VML 组合后,我似乎无法让我在网上看到的任何示例都可以工作。有人碰巧知道它是什么吗?
我正在尝试做这样的事情:
namespace = {'v': "urn:schemas-microsoft-com:vml"}
results = ET.fromstring(xml).findall("imagedata", namespace)
for image_id in results:
print(image_id)
Run Code Online (Sandbox Code Playgroud)
编辑:@aneroid 所写的是 1000% 正确的答案并且非常有帮助。你应该点赞。也就是说,在理解了所有这些之后 - 我选择了 BS4 答案,因为它在两行中完成了我需要的全部工作。如果您实际上并不关心命名空间,那似乎更容易。
python ×10
xml ×9
elementtree ×8
lxml ×3
dictionary ×2
api ×1
html ×1
python-2.7 ×1
regex ×1
urn ×1
vml ×1
xhtml ×1
xml-parsing ×1