3

How to implement XSD inner reference to simpleType?

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

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>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
NoWar
  • 36,338
  • 80
  • 323
  • 498

1 Answers1

4

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Hi! I dont have any real namespace like you mentioned xmlns:kt="http://example.com/know-type". I guessed is possible to define simpleType "locally" within a file. That what I need... Is it possible to do? – NoWar Feb 23 '17 at 16:50
  • 1
    @Dimi Please show (as text, not in an image) a complete input XML document that should be a valid instance of your schema. Thanks! More help: http://stackoverflow.com/help/mcve. – Mathias Müller Feb 23 '17 at 16:56
  • 2
    @Dimi If your instance documents are in no namespace at all, then you need to remove the `targetNamespace` attribute from your XML Schema. Having this attribute means that instance documents look similar to: ``. – Mathias Müller Feb 23 '17 at 17:02
  • Yes. You are right. Also in this case I have took out the `xs:` and keep the type name only. It is like . Please correct your answer as well. And thank you so much! – NoWar Feb 23 '17 at 17:09
  • 1
    @Dimi: The XSD that you posted has a `xs:schema/@targetNamespace`, which means that any XML meant to be validated against it will have to be in the namespace given by the value of `xs:schema/@targetNamespace`. It also means that references to elements and types within the XSD have to take into account the namespace. The changes I've shown in this answer demonstrate how to do this; no corrections are necessary unless you've changed your XSD to remove the `targetNamespace`. – kjhughes Feb 23 '17 at 17:12
  • @Dimi: I've added a *without* `targetNamespace` explanation to the answer to cover both ways for you. – kjhughes Feb 23 '17 at 17:23
  • Hi! Would you mind to help with another XSD issue please? http://stackoverflow.com/questions/42440634/xml-signature-element-is-not-declared – NoWar Feb 24 '17 at 14:05
  • @Dimi: Answered that one for you too. – kjhughes Feb 24 '17 at 14:21