3

I am trying to validate a XML document that uses multiple namespaces. I want to embed tags of the secondary namespace within a document of the primary namespace. The main/primary namespace does not "know" of the extension/secondary namespace.

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<book
    xmlns    ="MyMain_FPI"
    xmlns:ns2="MyExtension_FPI"
    >
    <ns2:playmusic/>
    <chapter/>
    <chapter/>
</book>

The Java code for validation is the following:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new Source[] {
    new StreamSource(new FileInputStream("main.xsd")),
    new StreamSource(new FileInputStream("extension.xsd")),
});
factory.setSchema(schema);

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(new File("test.xml"));

As you can see, I directly add the XSD files to the DocumentBuilderFactory. Those files are:

main.xsd

<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns="MyMain_FPI"
  targetNamespace="MyMain_FPI"
    elementFormDefault="qualified">
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="chapter"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="chapter">
    <xs:complexType/>    
  </xs:element>
</xs:schema>

extension.xsd

<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns="MyExtension_FPI"
  targetNamespace="MyExtension_FPI"
    elementFormDefault="qualified">
  <xs:element name="playmusic">
  </xs:element>
</xs:schema>

The error I get when executing the code above is

[Error] test.xml:6:18: cvc-complex-type.2.4.a: Ungültiger Content wurde beginnend mit Element 'ns2:playmusic' gefunden. '{"MyMain_FPI":chapter}' wird erwartet.

Meaning, that is unexpected within the main namespace - which is understandable, since the element itself is unknown in that namespace. I expected that elements of the extension namespace are ignored when validating the document again the main namespace and vice versa - meaning that I can embed documents following the extension namespace within the main namespace. But that is obviously not working.

What did I miss?

jrbedard
  • 3,662
  • 5
  • 30
  • 34
taranion
  • 631
  • 1
  • 6
  • 17
  • 2
    Your XSD says that your "book" element has a list of "chapter" elements. You tried to put in a different element. I'm not sure what you're question is. Do you want to know how to rewrite `main.xsd` to allow playmusic? – Daniel Moses Sep 21 '16 at 20:15
  • I thought it would be possible to validate the document without rewriting the `main.xsd`. Both XSDs are not really related. I assume I could write an XSD that combines both XSDs, but that would be annoying, since my real application might have several extension XSDs that might be present or not - and the list may be continued. (Think of it as embedded subdocuments for a kind of plugin). My question is if there is any way to validate this, so that the validator won't complain about elements from the extension.xsd – taranion Sep 22 '16 at 06:00

1 Answers1

2

Your main.xsd is explicit in what is allowed in the book element. If you want to allow any additional element, you'll need to state that in the main.xsd. After which you can add any extension XSDs you want. see https://www.w3.org/TR/xmlschema-0/#any.

new main.xsd

<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns="MyMain_FPI"
targetNamespace="MyMain_FPI"
  elementFormDefault="qualified">
<xs:element name="book">
  <xs:complexType>
    <xs:sequence>
      <xs:any minOccurs="0" maxOccurs="unbounded" />
      <xs:element maxOccurs="unbounded" ref="chapter"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="chapter">
  <xs:complexType/>    
</xs:element>
</xs:schema> 
Daniel Moses
  • 5,872
  • 26
  • 39
  • Thank you. The ANY element is a good compromise between keeping the main XSD to have no clue of possible extensions and still being valid although unknown (within `main.xsd`) elements appear in the document tree. This way I just need to define the possible extension points in the `main.xsd`. – taranion Sep 23 '16 at 21:07