xml xmllint 无法使用 xpath 正确查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8264134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xmllint failing to properly query with xpath
提问by ailnlv
I'm trying to query an xml file generated by adium. xmlwf says that it's well formed. By using xmllint's debug option i get the following:
我正在尝试查询由 adium 生成的 xml 文件。xmlwf 说它的格式很好。通过使用 xmllint 的调试选项,我得到以下信息:
$ xmllint --debug doc.xml
DOCUMENT
version=1.0
encoding=UTF-8
URL=doc.xml
standalone=true
ELEMENT chat
default namespace href=http://purl.org/net/ulf/ns/0.4-02
ATTRIBUTE account
TEXT
[email protected]
ATTRIBUTE service
TEXT compact
content=MSN
TEXT compact
content=
ELEMENT event
ATTRIBUTE type
Everything seems to parse just fine. However, when I try to query even the simplest things, I don't get anything:
一切似乎都解析得很好。但是,当我尝试查询即使是最简单的事情时,也没有得到任何信息:
$ xmllint --xpath '/chat' doc.xml
XPath set is empty
What's happening? Running that exact same query using xpath returns the correct results (however with no newline between results). Am I doing something wrong or is xmllint just not working properly?
发生了什么?使用 xpath 运行完全相同的查询会返回正确的结果(但是结果之间没有换行符)。我做错了什么还是 xmllint 工作不正常?
Here's a shorter, anonymized version of the xml that shows the same behavior:
这是 xml 的一个较短的匿名版本,它显示了相同的行为:
<?xml version="1.0" encoding="UTF-8" ?>
<chat xmlns="http://purl.org/net/ulf/ns/0.4-02" account="[email protected]" service="MSN">
<event type="windowOpened" sender="[email protected]" time="2011-11-22T00:34:43-03:00"></event>
<message sender="[email protected]" time="2011-11-22T00:34:43-03:00" alias="foo"><div><span style="color: #000000; font-family: Helvetica; font-size: 12pt;">hi</span></div></message>
</chat>
回答by Daniel Haley
I don't use xmllint, but I think the reason your XPath isn't working is because your doc.xml file is using a default namespace (http://purl.org/net/ulf/ns/0.4-02).
我不使用 xmllint,但我认为您的 XPath 无法正常工作的原因是您的 doc.xml 文件使用了默认命名空间 ( http://purl.org/net/ulf/ns/0.4-02)。
From what I can see, you have 2 options.
据我所知,您有两个选择。
A.Use xmllint in shell mode and declare the namespace with a prefix. You can then use that prefix in your XPath.
A.在 shell 模式下使用 xmllint 并使用前缀声明命名空间。然后,您可以在 XPath 中使用该前缀。
xmllint --shell doc.xml
/ > setns x=http://purl.org/net/ulf/ns/0.4-02
/ > xpath /x:chat
B.Use local-name()to match element names.
B.使用local-name()匹配的元素名称。
xmllint --xpath /*[local-name()='chat']
You may also want to use namespace-uri()='http://purl.org/net/ulf/ns/0.4-02'along with local-name()so you are sure to return exactly what you are intending to return.
您可能还想namespace-uri()='http://purl.org/net/ulf/ns/0.4-02'与 with一起使用,local-name()以便确保准确返回您打算返回的内容。
回答by codesniffer
I realize this question is very old now, but in case it helps someone...
我意识到这个问题现在已经很老了,但万一它对某人有帮助......
Had the same problem and it was due to the XML having a namespace (and sometimes it was duplicated in various places in the XML). Found it easiest to just remove the namespace before using xmllint:
有同样的问题,这是由于 XML 具有命名空间(有时它在 XML 的不同位置重复)。发现在使用 xmllint 之前删除命名空间最简单:
sed -e 's/xmlns=".*"//g' file.xml | xmllint --xpath "..." -
In my case the XML was UTF-16 so I had to convert to UTF-8 first (for sed):
在我的情况下,XML 是 UTF-16,所以我必须先转换为 UTF-8(对于 sed):
iconv -f utf16 -t utf8 file.xml | sed -e 's/encoding="UTF-16"?>/encoding="UTF-8"?>/' | sed -e 's/xmlns=".*"//g' | xmllint --xpath "..." -

