Search code examples
xsdschemaxsd-validation

How to skip validation on specific fields in XSD schema


I have a schema XSD file, Main.xsd and Child.xsd.

Main.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" schemaLocation="Child.xsd"/>
    <xsd:element name="Invoice">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:any namespace="##any" processContents="skip"/>
                <xsd:element ref="cbc:UBLVersionID" minOccurs="0"/>
                <xsd:element ref="cbc:CustomizationID"/>
                <xsd:element ref="cbc:ProfileID"/>
                <xsd:any namespace="##any" processContents="skip" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Child.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:element name="UBLVersionID" type="cbc:UBLVersionIDType"/>
    <xsd:element name="CustomizationID" type="cbc:CustomizationIDType"/>
    <xsd:element name="ProfileID" type="cbc:ProfileIDType"/>
    <xsd:complexType name="UBLVersionIDType">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:anyAttribute namespace="##any" processContents="skip"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
    <xsd:simpleType name="CustomizationIDType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="ProfileIDType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

As you can see in Main.xsd, we are checking UBLVersionID which is optional, CustomizationID and ProfileID, both are mandatory. The xsd:any statement before UBLVersionID and after ProfileID, to skip any elements or fields because we just need to check the fields which are defined in Main.xsd. Child.xsd has the fields defined, rules etc...

MY XSDs are working fine with All 2 XMLs I have: A.xml, B.xml:

A.XML: has UBLExtensions and UBLVersionID

<Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ext:UBLExtensions xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
        <ext:UBLExtension>
            <ext:ExtensionContent>
                <seef:SEEFExtensionWrapper xmlns:seef="urn:www.energie-efactuur.nl:profile:invoice:ver1.0">
                    <seef:UtilityConsumptionPoint>
                        <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">871685920003767063</cbc:ID>
                        <seef:Address>
                            <cac:Address xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
                                <cbc:StreetName xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46</cbc:StreetName>
                                <cbc:CityName xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">AMSTERDAM</cbc:CityName>
                                <cbc:PostalZone xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">1095 KN</cbc:PostalZone>
                                <cac:Country>
                                    <cbc:IdentificationCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">NL</cbc:IdentificationCode>
                                </cac:Country>
                            </cac:Address>
                        </seef:Address>
                    </seef:UtilityConsumptionPoint>
                </seef:SEEFExtensionWrapper>
            </ext:ExtensionContent>
        </ext:UBLExtension>
    </ext:UBLExtensions>
    <cbc:UBLVersionID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2.1</cbc:UBLVersionID>
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
    <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
    <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
    <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
    <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
    <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
</Invoice>

B.XML: Has UBLVersionID and not UBLExtensions:

<Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <cbc:UBLVersionID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2.1</cbc:UBLVersionID>
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
    <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
    <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
    <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
    <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
    <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
</Invoice>

My C.XML is producing validation error because that do not have UBLExtensions and UBLVersionID segments. As you can see, xsd:any and UBLVersionID in schema, both are optional.

C.XML:

<Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
    <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
    <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
    <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
    <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
    <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
</Invoice>

I tried a lot, what should I change in my schema so that it works with all 3 my xmls. Please help.


Solution

  • The error message is

    The element 'Invoice' in namespace 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' has invalid child element 'ProfileID' in namespace 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'. List of possible elements expected: 'UBLVersionID, CustomizationID' in namespace 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'.

    What that is telling you that either either expects UBLVersionID or, as UBLVersionID is optional, it is expecting CustomizationID. Which of course would strike you as strange as it is actually there.

    If you add CustomizationID again like below, it will actually validate.

    <Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
        <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
        <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
        <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
        <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
        <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
        <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
        <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
        <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
        <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
    </Invoice>
    

    What this indicates is that the <xsd:any namespace="##any" processContents="skip" /> that is matching CustomizationID.

    What you need is to actually define the other fields but as optional.

    Main.xml

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:seef="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:import schemaLocation=".\A1.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" />
      <xs:import schemaLocation=".\A3.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
      <xs:annotation>
        <xs:appinfo>
          <references xmlns="http://schemas.microsoft.com/BizTalk/2003">
            <reference targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
            <reference targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" />
            <reference targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" />
            <reference targetNamespace="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" />
          </references>
        </xs:appinfo>
      </xs:annotation>
      <xs:element name="Invoice">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="0" ref="ext:UBLExtensions" />
            <xs:element minOccurs="0" ref="cbc:UBLVersionID" />
            <xs:element ref="cbc:CustomizationID" />
            <xs:element ref="cbc:ProfileID" />
            <xs:element ref="cbc:ID" />
            <xs:element ref="cbc:IssueDate" />
            <xs:element ref="cbc:DueDate" />
            <xs:element ref="cbc:InvoiceTypeCode" />
            <xs:element ref="cbc:Note" />
            <xs:element ref="cbc:DocumentCurrencyCode" />
            <xs:element ref="cbc:BuyerReference" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    Child1.xml

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns:tns="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:import schemaLocation=".\Child2.xsd" namespace="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" />
      <xs:element name="UBLExtensions">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="UBLExtension">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="ExtensionContent">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element xmlns:q1="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" ref="q1:SEEFExtensionWrapper" />
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    Child2.xml

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns:tns="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:import schemaLocation=".\Child3.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
      <xs:import schemaLocation=".\Child4.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" />
      <xs:element name="SEEFExtensionWrapper">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="UtilityConsumptionPoint">
              <xs:complexType>
                <xs:sequence>
                  <xs:element xmlns:q1="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q1:ID" />
                  <xs:element name="Address">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element xmlns:q2="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" ref="q2:Address" />
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    Child3.xml

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns:tns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="ID" type="xs:unsignedLong" />
      <xs:element name="StreetName" type="xs:string" />
      <xs:element name="CityName" type="xs:string" />
      <xs:element name="PostalZone" type="xs:string" />
      <xs:element name="IdentificationCode" type="xs:string" />
      <xs:element name="UBLVersionID" type="xs:decimal" />
      <xs:element name="CustomizationID" type="xs:string" />
      <xs:element name="ProfileID" type="xs:string" />
      <xs:element name="IssueDate" type="xs:date" />
      <xs:element name="DueDate" type="xs:date" />
      <xs:element name="InvoiceTypeCode" type="xs:unsignedShort" />
      <xs:element name="Note" type="xs:string" />
      <xs:element name="DocumentCurrencyCode" type="xs:string" />
      <xs:element name="BuyerReference" type="xs:unsignedInt" />
    </xs:schema>
    

    Child4.xml

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns:tns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:import schemaLocation=".\Child3.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
      <xs:element name="Address">
        <xs:complexType>
          <xs:sequence>
            <xs:element xmlns:q1="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q1:StreetName" />
            <xs:element xmlns:q2="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q2:CityName" />
            <xs:element xmlns:q3="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q3:PostalZone" />
            <xs:element name="Country">
              <xs:complexType>
                <xs:sequence>
                  <xs:element xmlns:q4="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q4:IdentificationCode" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>