我在 stackoverflow 上找到了很多关于 xmllint 及其“命名空间支持”的主题,但没有一个对我有帮助。
这是我的 xml(准确地说是 xsd)文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation>My Text</xsd:documentation>
</xsd:annotation>
</xsd:schema>
我想从此文件中检索“我的文本”链,因此我尝试这样做:
xmllint myfile.xsd --xpath "/*[namespace-uri()='http://www.w3.org/2001/XMLSchema' and
name()='schema']/*[namespace-uri()='http://www.w3.org/2001/XMLSchema' and
name()='annotation']/*[namespace-uri()='http://www.w3.org/2001/XMLSchema' and
name()='documentation']/text()"
但它不起作用。我收到“段错误”。
更新
我还有一个问题: 我应该使用什么来获取此文件中标签的属性“toto”值:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:toto="testAtributeValue">
<xsd:annotation>
<xsd:documentation>myText</xsd:documentation>
</xsd:annotation>
</xsd:schema>
换句话说,我想获得“testAtributeValue”
最佳答案
您的 XML 无效,因为未声明“xsd”前缀(甚至在问题的第二个 .version 中编辑的命令输出中报告了这一点)。
您需要 xmlns:xsd=
而不是 xmlns=
。
此外name()
返回一个QName。您想使用 local-name()
来匹配本地名称。
关于parsing - xmllint - 使用命名空间解析 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19425559/