How to reference global types in XSD?

早过忘川 提交于 2021-02-10 08:21:22

问题


How to implement XSD inner reference to simpleType?

I am facing the error you see in the image below.

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:


How to reference global types or elements in XSD

With @targetNamespace

[This half of the answer addresses the original question.]

First define a namespace prefix on xs:schema for the namespace specified by @targetNamespace:

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

Then use it where you reference global types in this namespace.

Use it to reference Money, which is causing your immediate error:

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

Use it also where you reference PNGPictureType:

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

After making these changes, your XSD will be free of errors.

Without @targetNamespace

To reference Money, replace type="xs:Money" with type="Money", and be sure that the target of the reference is globally, not locally, defined (your is):

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

No change is needed in this case for the references to PNGPictureType because it already properly addresses the no-namespace global types without using an improper namespace prefix.



来源:https://stackoverflow.com/questions/42421169/how-to-reference-global-types-in-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!