gpt4 book ai didi

xml - 元素 'Root' 的命名空间必须来自架构命名空间 'http://www.w3.org/2001/XMLSchema' ?

转载 作者:行者123 更新时间:2023-12-02 01:20:42 26 4
gpt4 key购买 nike

我有一个 xml 文档:

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child name="MyType" compareMode="EQ"></Child>
</Root>

我想在以下 xsd 的帮助下验证此 xml:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

当我尝试验证它时,我收到以下错误:

Exception in thread "main" org.xml.sax.SAXException: Validation failed against correct.xml. ErrorMessage:s4s-elt-schema-ns: The namespace of element 'Root' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.

我的问题是为什么Root必须位于模式的命名空间中?难道是我验证xml文档不正确?

public synchronized boolean isValid(String xmlFragment, File xmlSchema) throws SAXException, IOException{

// 1. Lookup a factory for the W3C XML Schema language

SchemaFactory factory = 
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

// 2. Compile the schema.

Schema schema = factory.newSchema(xmlSchema);

// 3. Get a validator from the schema.
Validator validator = schema.newValidator();

// 4. Parse the document you want to check.
Source source = new StreamSource(new ByteArrayInputStream(xmlFragment.getBytes()));

// 5. Check the document
validator.validate(source);
return true;
}

最佳答案

难道 xmlSchema 保存的不是您向我们展示的模式,而是 W3C 发布的“模式文档的模式”?错误消息片段“ErrorMessage:s4s-elt-schema-ns:”似乎暗示了这一点。

关于xml - 元素 'Root' 的命名空间必须来自架构命名空间 'http://www.w3.org/2001/XMLSchema' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4922452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com