在xsi:type value验证XML时出错

问题描述 投票:1回答:1

我有一个我在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答案,但到目前为止它们都没有。

xml xsd xmlhttprequest xsd-validation
1个回答
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>

--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文件,如果需要,可将其放在类的主要方法中)测试它):

// 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.");
© www.soinside.com 2019 - 2024. All rights reserved.