在xsi:type值处验证XML时出错

时间:2019-02-27 12:55:45

标签: xml xsd xmlhttprequest xsd-validation

我得到了我在HTTP请求中一直使用的XML

<?xml version="1.0"?>
<updateList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="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>

和我为此XML生成的XSD看起来像这样

<xs:element name="updateList">
<xs:complexType>
  <xs:sequence>
    <xs:element maxOccurs="unbounded" name="record">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="companyName" type="xs:string" />
        </xs:sequence>
        <xs:attribute name="internalId" type="xs:unsignedByte" use="required" />
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

但是当我尝试针对生成的架构验证相同的XML时,出现此错误-

    This is an invalid xsi:type 'urn:relationships_namespace:Customer'.

表示XML中的所有三个客户记录。但令人困惑的是,HTTP请求可以正常工作,而无需进行任何更改。希望对此有所帮助。被困了很久。

P.S。-还检查了各种StackOverFlow答案,但到目前为止,这些答案都无效。

1 个答案:

答案 0 :(得分:0)

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>

-编辑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>

对于schemaLocation,这是假设所有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文件,将其放在类的主方法中)如果要测试):

// 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.");