Flaw? - <?xml-model?> and multiple namespaces

This should cover W3C XML Schema, Relax NG and DTD related problems.
Carla Huls
Posts: 2

Flaw? - <?xml-model?> and multiple namespaces

Mon Sep 02, 2013 10:51 pm

Hi,

I have two RelaxNG schemas called Address.rng and Customers.rng, with target namespaces "http://www.myserver.com/xmlns/address" and "http://www.myserver.com/xmlns/customers".

Schema Customers.rng has an <anyName> instruction, like this:

Code: Select all

    <element name="CustomerInformation">
        <anyName>
            <except>
            <nsName ns="blah"/>
         </except>
      </anyName>
   </element>


This means i can put elements from any namespace except "blah" within element "CustomerInformation", like this:

Code: Select all

   <cust:CustomerInformation xmlns:cust="http://www.myserver.com/xmlns/customers">
      <addr:Address xmlns:addr="http://www.myserver.com/xmlns/address">
      </addr:Address>
   </cust:CustomerInformation>


To validate this document i use an xml-model instruction:

<?xml-model href="Customers.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>

But, this doesn't work, because namespace "http://www.myserver.com/xmlns/address" is not defined. So, i add a second xml-model instruction:

<?xml-model href="Address.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>

But, this also doesn't work, because these xml-model instructions are used one-by-one, not together.


Any ideas?

Thank you,
- Carla


Full Example:

Address.rng:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<grammar
   datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
   ns="http://www.myserver.com/xmlns/address"
   xmlns="http://relaxng.org/ns/structure/1.0"
   xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0">
   <start>
      <ref name="start"/>
   </start>
   <define name="start">
      <element name="Address">
         <element name="Street">
            <data type="string"/>
         </element>
         <element name="City">
            <data type="string"/>
         </element>
         <element name="State">
            <data type="string"/>
         </element>
         <element name="ZipCode">
            <data type="string"/>
         </element>
      </element>
   </define>
</grammar>


Customers:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<grammar
   datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
   ns="http://www.myserver.com/xmlns/customers"
   xmlns="http://relaxng.org/ns/structure/1.0"
   xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0">
   <start>
      <ref name="start"/>
   </start>
   <define name="start">
      <element name="Customers">
         <element name="CustomerInformation">
            <element name="CustomerNumber">
               <data type="integer"/>
            </element>
            <element name="blabla">
               <data type="integer"/>
            </element>
            <element>
               <anyName>
                  <except>
                     <nsName ns="blah"/>
                  </except>
               </anyName>
               <text/>
            </element>
         </element>
      </element>
   </define>
</grammar>


Example.xml:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<?xml-model href="Address.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="Customers.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<cust:Customers
   xmlns:addr="http://www.myserver.com/xmlns/address"
   xmlns:cust="http://www.myserver.com/xmlns/customers">
   <cust:CustomerInformation>
      <cust:CustomerNumber error1="">990023</cust:CustomerNumber>
      <addr:Address>
         <addr:Street>9902 Broadway</addr:Street>
         <addr:City>Chicago</addr:City>
         <addr:error2/>
         <addr:State>IL</addr:State>
         <addr:ZipCode error3="">60612</addr:ZipCode>
      </addr:Address>
   </cust:CustomerInformation>
</cust:Customers>
adrian
Posts: 2451

Re: Flaw? - <?xml-model?> and multiple namespaces

Wed Sep 04, 2013 5:54 pm

Hi,

xml-model instructions are meant to associate one or more schemas with the XML document. They are not supposed to be complementary within the same schema model, they are supposed to provide multiple validation steps. This means each xml-model specified schema will be evaluated independently from the others.
http://www.w3.org/TR/xml-model/#the-xml ... nstruction

BTW, is there any reason why you don't refer Address.rng in the Customers.rng schema?
Instead of:

Code: Select all

                <element>
                    <anyName>
                        <except>
                            <nsName ns="blah"/>
                        </except>
                    </anyName>
                    <text/>
                </element>

You can use:

Code: Select all

<externalRef href="Address.rng"/>

In the XML you'll only need the Customers.rng xml-model instruction.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Carla Huls
Posts: 2

Re: Flaw? - <?xml-model?> and multiple namespaces

Thu Sep 05, 2013 5:14 pm

Thank you for your response.

My question was not of a practical nature. I find it strange that it's not possible to decide in the XML document which namespaces to combine when using the xml-model instruction. In XMLSchema I can do this:

<?xml version="1.0" encoding="utf-8"?>
<cust:Customers
xmlns:addr="http://www.myserver.com/xmlns/address"
xmlns:cust="http://www.myserver.com/xmlns/customers"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.myserver.com/xmlns/address Address.xsd
http://www.myserver.com/xmlns/customers Customiers.xsd">
<cust:CustomerInformation>
<cust:CustomerNumber error1="">990023</cust:CustomerNumber>
<addr:Address>
<addr:Street>9902 Broadway</addr:Street>
<addr:City>Chicago</addr:City>
....

When this XML Document is validated, both namespaces (address & customers) are checked.
There is no alternative using xml-model:

<?xml version="1.0" encoding="utf-8"?>
<?xml-model href="Address.rng" type="application/xml" schematypens="http://www.w3.org/2001/XMLSchema"?>
<?xml-model href="Customers.rng" type="application/xml" schematypens="http://www.w3.org/2001/XMLSchema"?>
<cust:Customers
xmlns:addr="http://www.myserver.com/xmlns/address"
xmlns:cust="http://www.myserver.com/xmlns/customers">
<cust:CustomerInformation>
<cust:CustomerNumber error1="">990023</cust:CustomerNumber>
<addr:Address>
<addr:Street>9902 Broadway</addr:Street>
<addr:City>Chicago</addr:City>
....

This is not the same as the previous example: here the XML document is validated twice against both namespaces, one by one.

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 0 guests