提问者:小点点

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


如何实现XSD内部引用simpleType?

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

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个答案

匿名用户

[这一半的答案解决了最初的问题。]

首先在xs: schema上为@target etNamespace指定的命名空间定义一个命名空间前缀:

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

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

使用它来引用Money,这会导致您立即出错:

<xs:element name="TotalSum" type="kt:Money" />

在引用PNGPicture Type的地方也使用它:

<xs:element name="Front" type="kt:PNGPictureType" />
<xs:element name="Back" type="kt:PNGPictureType" />

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

要引用Money,请将type="xs: Money"替换为type="Money",并确保引用的目标是全局定义的,而不是本地定义的(你的是):

<xs:element name="TotalSum" type="Money" />

在这种情况下,对PNGPicture Type的引用不需要更改,因为它已经正确地解决了无命名空间全局类型,而无需使用不正确的命名空间前缀。