XSD 导入另一个命名空间并使用其类型声明时出现问题
XSD problem importing another namespace and using its type declarations
我正在习惯 XML 架构并尝试将另一个架构导入我自己的架构。
初始模式文件test.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="test"
xmlns="test"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" >
<xs:complexType name="Test">
<xs:all>
<xs:element name="test-import" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
导入模式test2.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="test2"
xmlns="test2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:test="test"
elementFormDefault="qualified" >
<xs:import namespace="test" schemaLocation="./test.xsd" />
<xs:element name="project">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="test:Test" />
<xs:element name="test2" type="Model" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Model">
<xs:all>
<xs:element name="model-test" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
最后是导入命名空间 test.xml
:
的 test.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<file xmlns="test2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="./test2.xsd">
<test>
<test-import>Text</test-import>
</test>
<test2>
<model-test>Text</model-test>
</test2>
</file>
最后,标签 test2 可以正常工作,但在 <test-import>Text</test-import>
我会收到错误消息:
- test-import
One of the following is expected:
- test-import
Error indicated by:
{test}
with code:xml(cvc-complex-type.2.4.a)```
问题太多无法列出,但您可以在下面找到所有错误已更正,因此您的 XML 将成功验证您的 XSD。
另见
- How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?
已更正所有错误的文件集
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://example.com/test2"
xmlns:test="https://example.com/test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://example.com/test2 test2.xsd">
<test>
<test:test-import>Text</test:test-import>
</test>
<test2>
<model-test>Text</model-test>
</test2>
</project>
test2.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="https://example.com/test2"
xmlns="https://example.com/test2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:test="https://example.com/test"
elementFormDefault="qualified" >
<xs:import namespace="https://example.com/test" schemaLocation="test.xsd" />
<xs:element name="project">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="test:Test" />
<xs:element name="test2" type="Model" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Model">
<xs:all>
<xs:element name="model-test" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="https://example.com/test"
xmlns="https://example.com/test"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" >
<xs:complexType name="Test">
<xs:all>
<xs:element name="test-import" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
我正在习惯 XML 架构并尝试将另一个架构导入我自己的架构。
初始模式文件test.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="test"
xmlns="test"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" >
<xs:complexType name="Test">
<xs:all>
<xs:element name="test-import" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
导入模式test2.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="test2"
xmlns="test2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:test="test"
elementFormDefault="qualified" >
<xs:import namespace="test" schemaLocation="./test.xsd" />
<xs:element name="project">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="test:Test" />
<xs:element name="test2" type="Model" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Model">
<xs:all>
<xs:element name="model-test" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
最后是导入命名空间 test.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<file xmlns="test2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="./test2.xsd">
<test>
<test-import>Text</test-import>
</test>
<test2>
<model-test>Text</model-test>
</test2>
</file>
最后,标签 test2 可以正常工作,但在 <test-import>Text</test-import>
我会收到错误消息:
- test-import
One of the following is expected:
- test-import
Error indicated by:
{test}
with code:xml(cvc-complex-type.2.4.a)```
问题太多无法列出,但您可以在下面找到所有错误已更正,因此您的 XML 将成功验证您的 XSD。
另见
- How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?
已更正所有错误的文件集
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://example.com/test2"
xmlns:test="https://example.com/test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://example.com/test2 test2.xsd">
<test>
<test:test-import>Text</test:test-import>
</test>
<test2>
<model-test>Text</model-test>
</test2>
</project>
test2.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="https://example.com/test2"
xmlns="https://example.com/test2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:test="https://example.com/test"
elementFormDefault="qualified" >
<xs:import namespace="https://example.com/test" schemaLocation="test.xsd" />
<xs:element name="project">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="test:Test" />
<xs:element name="test2" type="Model" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Model">
<xs:all>
<xs:element name="model-test" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="https://example.com/test"
xmlns="https://example.com/test"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" >
<xs:complexType name="Test">
<xs:all>
<xs:element name="test-import" minOccurs="0" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>