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:
$ 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
content=foo@bar.com
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?
Here's a shorter, anonymized version of the xml that shows the same behavior:
<?xml version="1.0" encoding="UTF-8" ?>
<chat xmlns="http://purl.org/net/ulf/ns/0.4-02" account="foo@bar.com" service="MSN">
<event type="windowOpened" sender="foo@bar.com" time="2011-11-22T00:34:43-03:00"></event>
<message sender="foo@bar.com" 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>
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
).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.
B. Use
local-name()
to match element names.You may also want to use
namespace-uri()='http://purl.org/net/ulf/ns/0.4-02'
along withlocal-name()
so you are sure to return exactly what you are intending to return.