parsing - xmllint - 使用命名空间解析 xml 文件

标签 parsing namespaces xmllint

我在 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/

相关文章:

java - org.xml.sax.SAXParseException : The character reference must end with the ';' delimiter. 需要解决方法

php - 如何检查尚未自动加载的类是否存在

xml - xmllint 中的缩进级别

maven - 提取pom.xml中的版本

xml - 使用 xmllint 添加属性

java - 如何从类内同名但数据不同的类中获取数据?

java - Java中的Json解析仅通过提供一次键从数组中获取值?

java - 以线程安全方式格式化日期抛出 java.text.ParseException :

命名空间中允许使用 PHP 保留关键字? (公共(public),私有(private),默认)

javascript - 如何在同一个 namespace 中创建两个不同的 namespace ,避免覆盖第一个 namespace ?