xml与xsd架构不匹配 - 为什么?

时间:2013-10-07 08:27:48

标签: xml xsd-validation

我需要xml来匹配XSD架构,但它不匹配。 XSD架构中可能出现什么问题? xsi:type =“...”的东西不对。我可能会错过一些东西希望你能注意到什么是错的。我一直在尝试在线使用xsd validate并修复它但不起作用。所以我放弃并决定写在这里。非常感谢您的帮助。谢谢!

以下是XML:

<?xml version="1.0" encoding="utf-8" ?>
<ValidatorList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <FieldValidator xsi:type="RequiredFieldValidator"  PropertyName="BankTransactionNumber">
   <Next xsi:type="AsciiValidator" />
  </FieldValidator>
  <FieldValidator xsi:type="RequiredFieldValidator"  PropertyName="CurrencyIndicator">
   <Next xsi:type="StringLengthValidator" MaxLength="3" MinLength="3">
   <Next xsi:type="AlphaValidator" />
  </Next>
 </FieldValidator>
</ValidatorList>

此XML的XSD架构:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ValidatorList">
<xs:complexType>
  <xs:sequence>
    <xs:element name="FieldValidator" maxOccurs="unbounded" minOccurs="0">
      <xs:complexType mixed="true">
        <xs:sequence>
          <xs:element name="Next" minOccurs="0">
            <xs:complexType mixed="true">
              <xs:sequence>
                <xs:element name="Next" minOccurs="0">
                  <xs:complexType mixed="true">
                    <xs:sequence>
                      <xs:element name="Next" minOccurs="0">
                        <xs:complexType>
                          <xs:simpleContent>
                            <xs:extension base="xs:string">
                              <xs:attribute type="xs:byte" name="DecimalPlaces"/>
                            </xs:extension>
                          </xs:simpleContent>
                        </xs:complexType>
                      </xs:element>
                    </xs:sequence>
                    <xs:attribute type="xs:byte" name="MaxLength" use="optional"/>
                    <xs:attribute type="xs:byte" name="MinLength" use="optional"/>
                    <xs:attribute type="xs:string" name="AllowedValues" use="optional"/>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
              <xs:attribute type="xs:byte" name="MaxLength" use="optional"/>
              <xs:attribute type="xs:byte" name="MinLength" use="optional"/>
              <xs:attribute type="xs:byte" name="DefaultValue" use="optional"/>
            </xs:complexType>
          </xs:element>
          <xs:element type="xs:string" name="RegEx" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute type="xs:string" name="PropertyName" use="optional"/>
        <xs:attribute type="xs:byte" name="DefaultValue" use="optional"/>
        <xs:attribute type="xs:string" name="TypeProperty" use="optional"/>
        <xs:attribute type="xs:string" name="Regex" use="optional"/>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

我收到如下错误消息:

Error - Line 4, 39: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 39; cvc-elt.4.2: Cannot resolve 'AsciiValidator' to a type definition for element 'Next'.
Error - Line 6, 87: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 87; cvc-elt.4.2: Cannot resolve 'RequiredFieldValidator' to a type definition for element 'FieldValidator'.
Error - Line 7, 72: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 72; cvc-elt.4.2: Cannot resolve 'StringLengthValidator' to a type definition for element 'Next'.
Error - Line 8, 41: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 41; cvc-elt.4.2: Cannot resolve 'AlphaValidator' to a type definition for element 'Next'.

1 个答案:

答案 0 :(得分:2)

如果模式中具有名称类型的层次结构,并且声明类型为基类型但实际类型为其中之一的元素,则应使用xsi:type属性。派生类型。典型示例是address类型,其子类型为usAddressukAddress

在您的架构中,FieldValidatorNext元素是使用匿名嵌套<complexType>定义而不是命名类型声明的,因此您无法从这些元素派生其他类型,因此没有使用xsi:type的感觉。我将使用顶级命名类型以不同方式构造架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="FieldValidatorType">
    <xs:sequence>
      <xs:element name="Next" type="FieldValidatorType" minOccurs="0" />
    </xs:sequence>
    <xs:attribute name="PropertyName" type="xs:string" use="optional" />
  </xs:complexType>

  <xs:complexType name="RequiredFieldValidator">
    <xs:complexContent>
      <xs:extension base="FieldValidatorType" />
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="AsciiValidator">
    <xs:complexContent>
      <xs:extension base="FieldValidatorType" />
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="StringLengthValidator">
    <xs:complexContent>
      <xs:extension base="FieldValidatorType">
        <xs:attribute type="xs:byte" name="MaxLength" use="optional"/>
        <xs:attribute type="xs:byte" name="MinLength" use="optional"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <!-- and similar complexType declarations for the other types such as
       AlphaValidator -->

  <xs:element name="ValidatorList">
    <xs:complexType>
      <xs:sequence>
        <!-- define the FieldValidator element by referring to the base
             FieldValidatorType, instance documents can use xsi:type to
             substitute subtypes such as StringLengthValidator -->
        <xs:element name="FieldValidator" maxOccurs="unbounded" minOccurs="0"
                    type="FieldValidatorType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

除了使xsi:type属性正常工作之外,这还有将不同方面与各自的验证方类型联系起来的优势(例如MaxLength仅适用于StringLengthValidator)和还支持Next嵌套的任意深度(因为类型定义是递归的)。