0

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

这是 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'.
4

1 回答 1

2

当您在架构中具有命名类型的层次结构和声明类型为基类型但其实际类型是派生类型之一的元素xsi:type时,应使用该属性。原型示例是具有子类型和addressusAddressukAddress

在您的架构中,FieldValidatorandNext元素是使用匿名嵌套<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,仅对 a 有效StringLengthValidator)并且还支持任意深度的Next嵌套(因为类型定义是递归的)。

于 2013-10-07T10:31:59.397 回答