0

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

3 Answers3

0

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

Yes, that is a valid use of XML schema. A schema does not have to be represented by a single XSD file. Please see https://www.w3.org/TR/xmlschema-0/#SchemaInMultDocs and https://www.w3.org/TR/xmlschema-0/#import

You may also find this thread helpful: What's the difference between xsd:include and xsd:import?

kimbert
  • 2,376
  • 1
  • 10
  • 20
  • My question seemed to be not clear enough. The import and includes are not the problem. The problem is to have multiple root xsds implementing the same namespace. I added a sample to the question. – Stefan Lechner Jun 10 '21 at 04:50
0

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.

0

How about including the two existing XSDs in a third one and use this for validation?

schema3.xsd:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://www.xxxxxxx.com/texxxxxxx"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:include schemaLocation="schema1.xsd"/>
    <xs:include schemaLocation="schema2.xsd"/>
</xs:schema>