XML Schema for document with two namespaces

Question

I want to prepare .xsd to validate XML document I was given.

In bespoken document there are defined two namespaces:

<?xml version="1.0" encoding="UTF-8"?>
    <komunikat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="www.example.com/xml"
        xmlns:nfz="www.example.com/xml/nfz" 

        nfz:version="1.7"
        nfz:info-aplik-nad="KS-PPS 2016.00.3.1">

        type="someType"
        version="4"
    </komunikat>

In my schema I tried:

   targetNamespace="www.example.com/xml"
   xmlns:nfz="www.example.com/xml/nfz"
   ...
   <xs:attribute name="nfz:info-aplik-nad" use="optional" >
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:length value="40" />
        </xs:restriction>
    </xs:simpleType> ...

But in my validator I receive an error :

.XMLSchemaParseError: Element '{http://www.w3.org/2001/XMLSchema}attribute', attribute 'name': 'nfz:info-aplik-nad' is not a valid value of the atomic type 'xs:NCName'., line 25

What I understand : char is not allowed in attribute name property.

Is it possible to do without changing xml file ?


Show source
| xml   | xsd   2017-01-04 16:01 1 Answers

Answers to XML Schema for document with two namespaces ( 1 )

  1. 2017-01-04 16:01

    Only attributes in namespaces can have colons in their names (to specify the namespace prefix).

    There are a number of extraneous problems with your XML and XSD. Here is a complete, more minimal, example that shows an XML instance that is valid against an XSD where an attribute is in a namespace:

    XML

    <?xml version="1.0" encoding="UTF-8"?>
    <komunikat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns="http://myNameSpace.com" 
               xmlns:nfz="http://myNameSpace.com" 
               nfz:info-aplik-nad="KS-PPS 2016.00.3.1"/>
    

    XSD

    <xs:schema targetNamespace="http://myNameSpace.com" 
               xmlns:m="http://myNameSpace.com"
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               attributeFormDefault="qualified" >
      <xs:element name="komunikat">
        <xs:complexType>
          <xs:sequence/>
          <xs:attribute name="info-aplik-nad" use="optional" >
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:length value="18" />
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    Note in particular the use of attributeFormDefault="qualified" in the XSD.

    In order to use an additional namespace, you must have an additional XSD, which then would imported (using xs:import) into the main XSD.

Leave a reply to - XML Schema for document with two namespaces

◀ Go back