- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些 xml,其中包含多个同名元素,但每个元素使用不同的语言,例如:
<Title xml:lang="FR" type="main">Les Tudors</Title>
<Title xml:lang="DE" type="main">Die Tudors</Title>
<Title xml:lang="IT" type="main">The Tudors</Title>
通常,我会使用如下属性检索元素:
titlex = info.find('.//xmlns:Title[@someattribute=attributevalue]', namespaces=nsmap)
如果我尝试使用 [@xml:lang="FR"] 执行此操作(例如),我会收到回溯错误:
File "D:/Python code/RBM CRID, Title, Genre/CRID, Title, Genre, Age rating, Episode Number, Descriptions V1.py", line 29, in <module>
titlex = info.find('.//xmlns:Title[@xml:lang=PL]', namespaces=nsmap)
File "lxml.etree.pyx", line 1457, in lxml.etree._Element.find (src\lxml\lxml.etree.c:51435)
File "C:\Python34\lib\site-packages\lxml\_elementpath.py", line 282, in find
it = iterfind(elem, path, namespaces)
File "C:\Python34\lib\site-packages\lxml\_elementpath.py", line 272, in iterfind
selector = _build_path_iterator(path, namespaces)
File "C:\Python34\lib\site-packages\lxml\_elementpath.py", line 256, in _build_path_iterator
selector.append(ops[token[0]](_next, token))
File "C:\Python34\lib\site-packages\lxml\_elementpath.py", line 134, in prepare_predicate
token = next()
File "C:\Python34\lib\site-packages\lxml\_elementpath.py", line 80, in xpath_tokenizer
raise SyntaxError("prefix %r not found in prefix map" % prefix) SyntaxError: prefix 'xml' not found in prefix map
我对此并不感到惊讶,但我想要有关如何解决该问题的建议。
谢谢!
根据要求,一个精简但完整的代码集(如果我删除 [bitsinsquarebrackets],它会按预期工作):
import lxml
import codecs
file_name = (input('Enter the file name, excluding .xml extension: ') + '.xml')# User inputs file name
print('Parsing ' + file_name)
#----- Sets up import and namespace
from lxml import etree
parser = lxml.etree.XMLParser()
tree = lxml.etree.parse(file_name, parser) # Name of file to test goes here
root = tree.getroot()
nsmap = {'xmlns': 'urn:tva:metadata:2012',
'mpeg7': 'urn:tva:mpeg7:2008'}
#----- This code writes the output to a file
with codecs.open(file_name+'.log', mode='w', encoding='utf-8') as f: # Name the output file
f.write(u'CRID|Title|Genre|Rating|Short Synopsis|Medium Synopsis|Long Synopsis\n')
for info in root.xpath('//xmlns:ProgramInformation', namespaces=nsmap):
titlex = info.find('.//xmlns:Title[xml:lang="PL"]', namespaces=nsmap) # Retreve the title
title = titlex.text if titlex != None else 'Missing' # If there isn't a title, print an alternative word
f.write(u'{}\n'.format(title)) # Write all the retrieved values to the same line with bar seperators and a new line
最佳答案
xml:lang
中的xml
前缀不需要在XML文档中声明,但是如果要使用xml:lang
在 XPath 查找中,您必须在 Python 代码中定义前缀映射。
xml
前缀是保留的(与任意的“普通” namespace 前缀相反)并定义为绑定(bind)到 http://www.w3.org/XML/1998/命名空间
。查看Namespaces in XML 1.0 W3C 推荐。
例子:
from lxml import etree
# Required mapping
nsmap = {"xml": "http://www.w3.org/XML/1998/namespace"}
XML = """
<root>
<Title xml:lang="FR" type="main">Les Tudors</Title>
<Title xml:lang="DE" type="main">Die Tudors</Title>
<Title xml:lang="IT" type="main">The Tudors</Title>
</root>"""
doc = etree.fromstring(XML)
title_FR = doc.find('Title[@xml:lang="FR"]', namespaces=nsmap)
print(title_FR.text)
输出:
Les Tudors
如果 xml
前缀没有映射,您将收到“前缀映射中未找到前缀‘xml’”错误。如果映射到 xml
前缀的 URI 不是 http://www.w3.org/XML/1998/namespace
,则 find
方法上面的代码片段中不返回任何内容。
关于Python lxml - 使用 xml :lang attribute to retrieve an element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31250641/
关于 this页面,我看到以下代码: if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) 但我不明白为什么会变成这样。
函数pthread_mutex_init允许您指定指向属性的指针。但是我还没有找到关于pthread属性是什么的很好的解释。我一直只是提供NULL。这个论点有用吗? 该文档,对于那些忘记它的人: PT
我们有一个 xml 节点“item”,其属性为“style”,即“Header1”。但是,这种风格可以改变。我们有一个名为 Header1 的属性集,它定义了它在 PDF 中的外观,通过 xsl:fo
我的任务是在用户点击它时从输入框中删除占位符并使标签可见。如果用户未在其中再次填写任何内容,请放回占位符并使标签不可见。 我可以隐藏它但不能重新分配它。我试过 element.setAttribute
我从文章中编写代码,并且有: public IActionResult Create([Bind(Include="Imie,Nazwisko,Stanowisko,Wiek")] Pracownik
你能给我解释一下以下属性吗? 1) [MonoTouch.Foundation.Register("SomeClass")] 这个属性是否只用于向IB注册类?以编程方式扩展 iOS 类时是否必须使用此
我正在编写一个 C++ 程序,在调试时我在执行以下函数: int CClass::do_something() { ... // I've put a breakpoint here } 我的 C
我已经在 polymer 0.5 中构建了我的应用程序。 现在我已经将它更新到 polymer 1.0。 对于响应式布局,我使用了一个布局属性,它使用 Polymer 0.5 中布局属性的自定义逻辑。
我是使用 Jade 的新手——到目前为止它很棒。 但是我需要发生的一件事是具有“itemscope”属性的元素: 我的 Jade 符是: header(itemscope, itemtype='ht
我正在研究一个厨师实现,有时在过去的地方使用了 attribute.set,attribute.default 会这样做。为了解决这个问题,我对 Chef 属性优先范式非常熟悉。我知道“正常”属性(使
我经常看到html data-attribute (s) 将特定值/参数添加到 html 元素,例如使用它们将按钮“链接”到要打开的模式对话框等的 Bootstrap。 现在,我看到一个几乎著名的
假设如下: def create_new_salt self.salt = self.object_id.to_s + rand.to_s end 为什么使用“ self ”更好。而不是实例变量“
根据我的理解,Backbone.js 模型的属性应该通过以下方式声明为有点私有(private)的成员变量 this.set({ attributeName: attributeValue }) //
我有一个看起来像下面的XML文档: ... ... ... ...
我正在实现一个 JSF 组件,需要有条件地添加一些属性。这个问题类似于之前的 JSF: p:dataTable with f:attribute results in "argument type m
我正在尝试将应用程序发布到 Android 电子市场,但出现以下错误: W/ResourceType(16964): No known package when getting value for r
抱歉这么具体的应用程序,但我注意到另一篇关于 Maya 开发的回答很好的帖子。 我刚刚为 Maya 编写了一个插件节点。它只是根据湍流函数杀死一堆粒子。湍流由许多可在属性编辑器中调整的属性驱动。 在属
我在 html 元素中的数据属性为 Update .它具有数据属性的 bool 值。 跟下面的元素Update有什么区别吗?因为数据属性用双引号引起来。 html是否支持 bool 值? 最佳答案 b
我正在尝试为企业库 5.0 的异常处理 block 创建自定义异常处理程序。据我了解,我需要使用属性开始上课“[ConfigurationElementType(typeof(CustomHandle
我找不到这两个选择器之间的区别。两者似乎都做同样的事情,即根据包含给定字符串的特定属性值选择标签。 对于 [attribute~=value] :http://www.w3schools.com/cs
我是一名优秀的程序员,十分优秀!