如何在XSD中引用全局类型?

时间:2017-02-23 16:25:18

标签: xml xsd xsd-validation xml-validation

如何实现对simpleType的XSD内部引用?

我正面临您在下图中看到的错误。

enter image description here

XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tns="http://example.com/ct-required"
           xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
           targetNamespace="http://example.com/know-type">

  <xs:import namespace="http://www.w3.org/2005/05/xmlmime"
            schemaLocation="http://www.w3.org/2005/05/xmlmime"/>

  <xs:simpleType name="JPEGPictureType"
           xmime:expectedContentTypes="image/jpeg">
    <xs:restriction base="xs:base64Binary"/>
  </xs:simpleType>

  <xs:simpleType name="PNGPictureType"
          xmime:expectedContentTypes="image/png">
    <xs:restriction base="xs:base64Binary"/>
  </xs:simpleType>

  <xs:simpleType name="Money">
        <xs:restriction base="xs:decimal">
            <xs:totalDigits value="13" />
            <xs:fractionDigits value="2" />
            <xs:minInclusive value="0.00" />
            <xs:maxInclusive value="99999999999.99" />
        </xs:restriction>
    </xs:simpleType>

  <xs:element name="Envelope">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Deposit" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ClientId" type="xs:unsignedLong" />
              <xs:element name="Account" type="xs:unsignedShort" />
              <xs:element name="Currency" type="xs:string" />
              <xs:element name="TotalSum" type="xs:Money" />
              <xs:element name="SignedDate" type="xs:date" />
              <xs:element name="Cheques">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="Cheque">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Images">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="Front" type="PNGPictureType" />
                                <xs:element name="Back" type="PNGPictureType" />
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                          <xs:element name="MicrCodeCmc" type="xs:integer" />
                          <xs:element name="Amount" type="xs:unsignedLong" />
                          <xs:element name="PaymentDate" type="xs:date" />
                          <xs:element name="EmissionDate" type="xs:date" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

XML

<?xml version="1.0" encoding="utf-8"?>
<Envelope>
  <Deposit>
    <ClientId>1234567890123</ClientId>
    <Account>0045678</Account>
    <Currency>USD</Currency>
    <TotalSum>500000</TotalSum>
    <SignedDate>2016-02-15</SignedDate>     
    <Cheques>
      <Cheque>
        <Images>
          <Front>
            SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
            AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
            EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
          </Front>
          <Back>
            SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
            AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
            AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
            jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
            LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
            1JtNV/9/rrffuO+h9bx////kfvABABAA
          </Back>
        </Images>
        <MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
        <Amount>99999999999</Amount>
        <PaymentDate>2016-02-15</PaymentDate>
        <EmissionDate>2016-02-15</EmissionDate>
      </Cheque>
      <Cheque>
        <Images>
          <Front>
            SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
            AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
            EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
          </Front>
          <Back>
            SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
            AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
            AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
            AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
            jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
            LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
            1JtNV/9/rrffuO+h9bx////kfvABABAA
          </Back>
        </Images>
        <MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
        <Amount>99999999999</Amount>
        <PaymentDate>2016-02-15</PaymentDate>
        <EmissionDate>2016-02-15</EmissionDate>
      </Cheque>
    </Cheques>
  </Deposit>   
</Envelope>

1 个答案:

答案 0 :(得分:3)

如何在XSD中引用全局类型或元素

使用 "Tag": null

[这半答案解决了原始问题。]

首先在"Tag": { "Id": null, "Name": null, "Source": null } 上为@targetNamespace指定的命名空间定义名称空间前缀:

xs:schema

然后在引用此命名空间中的全局类型的地方使用它。

使用它来引用@targetNamespace,这会导致您的即时错误:

<xs:schema targetNamespace="http://example.com/know-type"
           xmlns:kt="http://example.com/know-type"
           ...
           />

在您引用Money

的地方也可以使用它
<xs:element name="TotalSum" type="kt:Money" />

进行这些更改后,您的XSD将没有错误。

out PNGPictureType

要引用<xs:element name="Front" type="kt:PNGPictureType" /> <xs:element name="Back" type="kt:PNGPictureType" /> ,请将@targetNamespace替换为Money,并确保引用的目标是全局的,而不是本地的,定义的(您的):

type="xs:Money"

对于type="Money"的引用,在这种情况下不需要进行任何更改,因为它已经正确地解决了无命名空间的全局类型,而没有使用不正确的名称空间前缀。