0
votes

EDIT 13/07/2020 :

What I'm trying to achieve is validate the 3 elements in the XML. The DocumentId element will always contain a string, the ID element will always contain a string, and the element01 will always contain a string and have the attribute xsi:type="ns11:creditNote". The element Document and DocumentList can have any attributes from any namespaces.

The 3 elements to validate are :

  • /po:DocumentList/po:Document/po:DocumentId
  • /po:DocumentList/po:Document/po:ID
  • /po:DocumentList/po:Document/po:element01 with the attribute xsi:type="ns11:creditNote"

This is the XML :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<po:DocumentList xmlns:po="http://www.example.com/PO1" 
xmlns:ns11="urn:oasis:names:specification:ubl:schema:xsd:Invoice-1" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/PO1 Main.xsd" indexing="false" numPages="2" pageId="0" itemsPerPage="2" itemCount="3">
    <po:Document>
        <po:DocumentId>b6f066ea-1d48-566c-967a-f81a2d935d59</po:DocumentId>
        <po:ID>documents/b6f066ea-1d48-566c-967a-f81a2d935d59</po:ID>       
        <po:element01 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns11:creditNote">documents/b6f066ea-1d48-566c-967a-f81a2d935d59</po:element01>
    </po:Document>
</po:DocumentList>

I created a main XSD file (Main.xsd) for the namespace http://www.example.com/PO1 :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="XMLSchema-versioning" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:po="http://www.example.com/PO1" xmlns:ns11="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" targetNamespace="http://www.example.com/PO1" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
    <xs:import namespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" schemaLocation="Main_Sub_1.xsd"/>
    <xs:element name="DocumentList">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Document">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="DocumentId" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="ID" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="element01" type="ns11:creditNote"/>
                        </xs:sequence>
                        <xs:anyAttribute processContents="skip"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:anyAttribute processContents="skip"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

And I created another file Main_Sub_1.xsd for the namespace urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" attributeFormDefault="qualified" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns11="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
    <xs:simpleType name="creditNote">
        <xs:restriction base="xs:string">
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

However when I try to validate the main XSD file I get this error :

"'ns11:creditNote' must refer to an existing simple or complex type."

I made some experiments, and tried changing the namespace urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 to "test01" and it works, but as soon as I use this specific urn, it throws an error.

I saw that the urn points to an xsd schema that can be viewed here : https://schemas.liquid-technologies.com/oasis/ubl/2.0/?page=ubl-invoice-2_0_xsd.html

So I'm guessing the validator, when seeing this namespace, instead of importing my file with the type creditNote I created, looks at a file in a distant server which does not have the type creditNote defined. I don't have a lot of experience with XML and XSD but since replacing the urn with anything else makes my validation work, that's how I understood it.

How can I fix this error ?

1
Two possibilities: 1. Remove xsi:type="ns11:creditNote" from the XML or 2. Define the type ns11:creditNote somewhere.zx485
@zx485 : How can I define the type ns11:creditnote ? I tried creating another xsd and importing it but I didn't manage to make it work.Nabil B.
Tried some things, too. But also couldn't provide a solution. Sorry.zx485

1 Answers

0
votes

I want to validate 2 specific elements in my XML and allow any other attributes and elements that could be added.

You need to be very specific about where these additional tags/attributes need to appear. The best way to do that is to edit your question and add some example messages that show the extra content.

when I try to validate the XML I have an error : "Value 'ns11:creditNote' is not allowed for attribute 'xsi:type'."

You are asserting that the tag 'po:DocumentList' matches a type definition with name creditNote in namespace urn:oasis:names:specification:ubl:schema:xsd:Invoice-2. But you have not defined that type. In fact you have not even imported an XSD which has that targetNamespace.

Were you really trying to override the type of po:DocumentList? Or were you trying to bring the 'creditNote' type into play for some other purpose?

In summary, your question suggests a certain amount of confusion about how to define and use XML Schema extension mechanisms. Happy to help if you clarify the question a bit.