XSD 是错误的。在带有 targetNamespace urn:core_namespace 的 XSD 中,您应该为您的记录元素定义一个命名类型,例如Record:
让我们将此架构文件称为core.xsd 以供以后参考:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- EDIT 2019-03-17: added missing xmlns -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:core_namespace" targetNamespace="urn:core_namespace" elementFormDefault="qualified">
<xs:complexType name="Record">
<!-- EDIT 2019-03-17: moved 'companyName' element to Customer type in relationships.xsd -->
<xs:attribute name="internalId" type="xs:unsignedByte" use="required" />
</xs:complexType>
<!-- EDIT 2019-02-28: ADDED 'record' element-->
<xs:element name="record" type="Record" />
</xs:schema>
在另一个带有 targetNamespcae urn:relationships_namespace 的 XSD 中,您应该导入之前的 XSD 并将 Customer 类型定义为 Record 类型的扩展:
让我们将此架构文件称为 relationships.xsd 以供以后参考:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:relationships_namespace"
xmlns:pc="urn:core_namespace" elementFormDefault="qualified">
<xs:import namespace="urn:core_namespace" schemaLocation="core.xsd"/>
<xs:complexType name="Customer">
<xs:complexContent>
<xs:extension base="pc:Record">
<!-- EDIT 2019-03-17: added 'companyName' element (moved from Record type in core.xsd) -->
<xs:sequence>
<xs:element name="companyName" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
--EDIT 2019-02-28:在下面添加了架构--
以及定义updateList 元素的最终架构。
让我们将此架构文件称为 messages.xsd 以供以后参考:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:messages_namespace"
xmlns:pc="urn:core_namespace" elementFormDefault="qualified">
<xs:import namespace="urn:core_namespace" schemaLocation="core.xsd"/>
<xs:element name="updateList">
<xs:complexType>
<xs:sequence>
<xs:element ref="pc:record" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
对于schemaLocations,这是假设所有 XSD 都在同一个文件夹中,否则相应地更改 schemaLocations。
编辑 2019-03-17
此外,请修复您的 XML 以确保 updateList 元素在 urn:messages_namespace 中:将 xmlns:m="urn:messages_namespace" 替换为 xmlns="urn:messages_namespace"(默认命名空间)。
这是固定的 XML,假设它在文件 updateList.xml 中:
<?xml version="1.0"?>
<updateList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:messages_namespace" xmlns:listRel="urn:relationships_namespace" xmlns:pc="urn:core_namespace">
<pc:record xsi:type="listRel:Customer" internalId="46" xmlns:listRel="urn:relationships_namespace">
<listRel:companyName>T Tax</listRel:companyName>
</pc:record>
<pc:record xsi:type="listRel:Customer" internalId="44" xmlns:listRel="urn:relationships_namespace">
<listRel:companyName>S Tax</listRel:companyName>
</pc:record>
<pc:record xsi:type="listRel:Customer" internalId="45" xmlns:listRel="urn:relationships_namespace">
<listRel:companyName>K Tax</listRel:companyName>
</pc:record>
</updateList>
这是一段 Java 代码示例,并说明了如何使用 XML 模式文件验证 XML(适用于 Java 8 以及工作目录中的所有 XSD 和 XML 文件,将其放在类的 main 方法中如果你想测试它):
// XSD compiler
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load all XSD files starting with core.xsd
final Source coreXsd = new StreamSource(new File("core.xsd"));
final Source relationshipsXsd = new StreamSource(new File("relationships.xsd"));
final Source messagesXsd = new StreamSource(new File("messages.xsd"));
final Source[] schemaSources = { coreXsd, relationshipsXsd, messagesXsd };
// Compile the final schema for validation
final Schema schema = schemaFactory.newSchema(schemaSources);
// XML to be validated
final Source xml = new StreamSource(new File("updateList.xml"));
// Validate
final Validator validator = schema.newValidator();
validator.validate(xml);
System.out.println(xml.getSystemId() + " is valid.");