Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
2 replies Latest Post - ‏2012-09-15T19:31:39Z by catskills
catskills
catskills
2 Posts
ACCEPTED ANSWER

Pinned topic combining namespaces

‏2012-09-15T02:19:04Z |
Ok so the question is on combining xml namespaces in a single document.

I have the following four files::

A.xml

<?xml version="1.0"?>
<a:Rock xmlns:a="http://www.any.com/spec/A" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="A.xsd ">
RockDataForTypeA
</a:Rock>

A.xsd

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:a="http://www.any.com/spec/A" targetNamespace="http://www.any.com/spec/A">
<element name="Rock" type="string"/>
</schema>

and likewise I have

B.xml

<?xml version="1.0"?>
<b:Rock xmlns:b="http://www.any.com/spec/B" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="B.xsd ">
RockDataForTypeB
</b:Rock>

B.xsd

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:a="http://www.any.com/spec/B" targetNamespace="http://www.any.com/spec/B">
<element name="Rock" type="string"/>
</schema>

Now what I want is this::

<?xml version="1.0"?>
<both
xmlns="http://www.any.com/spec/both"
xmlns:a="http://www.any.com/spec/A"
xmlns:b="http://www.any.com/spec/B"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="both.xsd" >
<a:Rock>RockDataForTypeA</a:Rock>
<b:Rock>RockDataForTypeB</b:Rock>
</both>

What should this xml document looklike to be able to do this type of thing (what xmlns directives, qualified/unqualified etc would be required) and then what would the xsd schema look like that would validate this document?

I read through a few hundred pages of the the O'Reilly book on XSD schema and still can't answer this question--it seems like it should be rather trivial, but to me alas it is not. Thanks any help or pointers to documentation for this would be much appreciated.

Bradley
Updated on 2012-09-15T19:31:39Z at 2012-09-15T19:31:39Z by catskills
  • SystemAdmin
    SystemAdmin
    1391 Posts
    ACCEPTED ANSWER

    Re: combining namespaces

    ‏2012-09-15T14:56:48Z  in response to catskills
    [0] First, if you want to use xsi to suggest a location for the xsd, the xsi:schemaLocation value is of the wrong format for each A.xml or B.xml or both.xml. Its format is of the form of namespace urn followed by the schema "physical" location separated by a whitespace(s). Like this for A.xml for instance.
    
    xsi:schemaLocation=
    "http://www.any.com/spec/A A.xsd"
    


    [1] Now the question proper. It can be done like this.
    
    <xs:schema xmlns:xs=
    "http://www.w3.org/2001/XMLSchema" xmlns:a=
    "http://www.any.com/spec/A" xmlns:b=
    "http://www.any.com/spec/B" targetNamespace=
    "http://www.any.com/spec/both" elementFormDefault=
    "qualified" attributeFormDefault=
    "unqualified" > <xs:
    
    import namespace=
    "http://www.any.com/spec/A" schemaLocation=
    "A.xsd" /> <xs:
    
    import namespace=
    "http://www.any.com/spec/B" schemaLocation=
    "B.xsd" /> <xs:element name=
    "both"> <xs:complexType> <xs:sequence> <xs:element ref=
    "a:Rock" /> <xs:element ref=
    "b:Rock" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
    

    [1.1] Due to the simplicity of the structure, elementFormDefault etc may not have a chance to show its proper meaning in action. Also, I don't see a need to declare a prefix for the targetNamespace just yet; but, if the structure grows in complexity, you may have to declare a prefix for the targetNamespace as well...
  • catskills
    catskills
    2 Posts
    ACCEPTED ANSWER

    Re: combining namespaces

    ‏2012-09-15T19:31:39Z  in response to catskills
    Thank you that was a very fine answer!