Search code examples
javaxmlxsdxsd-validation

Multiple XSD implementing the same targetNamespace - is this correct?


I implemented an xsd scanner, which creates an targetNamespace=<file.xsd> catalog. Includes are filtered, so the catalog has only the root files of the targetNamespace. With this catalog I'm resolving the required files (using a LSResourceResolver) to validate incoming xml files.

Map

namespace1=path/xsdForNameSpace1
namespace2=path/xsdForNameSpace2
:

But now I got multiple XSD, containing different content, but implementing the same targetNamespace. Imho this is not correct, one namespace one root xsd - done

Example

schema1.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://www.xxxxxxx.com/texxxxxxx"
        targetNamespace="http://www.xxxxxxx.com/texxxxxxx"
        elementFormDefault="qualified">
<xsd:include schemaLocation="xxxxxx_xxxxxx_xxxxx_xxxxx.xsd"/>
<xsd:element name="ab120">
    <xsd:complexType>
:

schema2.xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://www.xxxxxxx.com/texxxxxxx"
        targetNamespace="http://www.xxxxxxx.com/texxxxxxx"
        elementFormDefault="qualified">
<xsd:include schemaLocation="xxxxxx_xxxxxx_xxxxx_xxxxx.xsd"/>
<xsd:element name="ab122">
    <xsd:complexType>
:

I have two xml files are implementing the identical namespace http://www.xxxxxxx.com/texxxxxxx one with a root element ab120 the other with a root element ab122.

In this case my map contains only one of the implementing xsd files and I've no idea how to resolve the correct xsd for the incoming xml.

The incoming xml files look like this.

file1.xml:

<ab120 xmlns="http://www.xxxxxxx.com/texxxxxxx" ...>
 : 
</ab120>

file2.xml

<ab122 xmlns="http://www.xxxxxxx.com/texxxxxxx" ...>
 :
</ab122>

The LSResourceResolver interface does not give me access to the xml, so I can't decide according the root node, which xsd I should use.

My temporary solution:

I added a second index with (namespace,xsd_file_name) that resolves correctly when the xml provides the implementing file (systemID)

targenNamespace="namespace myfile.xsd" 

My question is, is it correct to specifiy multiple XSD file implementing the same namespace with different xsd structures ?

Edit: It seemed to be not clear enough. Added two examples


Solution

  • Ok after asking w3c there is nothing in the specs that precludes this.

    Reusing a targetNamespace with different content is allowed. However, how to handle this, if you have to validate XMLs, is on your own and depends on the situation.

    Possible solutions could be adding a version tag to the xml header or combine schemas if possible.

    In my context nothing of the above would help, the resolver interface does not allow additional information, and the xsds can not be combined by a choice.

    The only way to solve the issue is creating different index, resolver combinations. When creating the validator I have to use the correct resolver according to the origin where the xml came from.