Home > Blockchain >  Validation issues with polymorphic xml
Validation issues with polymorphic xml

Time:07-26

We have some abstract elements defined in our root schema e.g.:

<xs:complexType name="Abstract1" abstract="true">
   <xs:sequence minOccurs="0">
      <xs:element name="absName1" type="xs:string" minOccurs="0"/>
   </xs:sequence>
</xs:complexType>

... which are then extended as follows:

<xs:complexType name="Extension1">
   <xs:complexContent>
      <xs:extension base="root:Abstract1">
         <xs:sequence>
            <xs:element name="extName1" type="xs:string" minOccurs="0"/>
         </xs:sequence>
      </xs:extension>
   </xs:complexContent>
</xs:complexType>

These elements are used inside our xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://example.com"
    xmlns:ext="https://ext.example.com"
    xsi:schemaLocation="https://example.com ../xsd/root.xsd
      https://ext.example.com ../xsd/ext1.xsd">
    <test1
        testTC="Test Case 1.1 - tag injection with same names using type casting, abstract type">
        <extension1 xsi:type="ext:Extension1">
            <absName1>Test Case 1.1 - absName1 from Abstract1 Type inhereted by Extension1</absName1>
            <ext:extName1>Test Case 1.1 - extName1 from Extension1</ext:extName1>
        </extension1>
        <extension1 xsi:type="ext:Extension11">
            <absName1>Test Case 1.1 - absName1 from Abstract1 Type inhereted by Extension11</absName1>
            <ext:extName11>Test Case 1.1 - extName11 from Extension11</ext:extName11>
        </extension1>
    </test1>
</root>

However, when I try to validate the xml using Saxon command line, I get the error message below:

$ java com.saxonica.Validate --multipleSchemaImports:on -s:tc1.xml -xsd:"../xsd/ext1.xsd;../xsd/root.xsd"

Validation error on line 7 column 92 of tc1.xml:
  FORG0001: In content of element <root>: The content model does not allow element <test1>
  to appear as the first child. The element is in namespace https://example.com but it
  should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 9 column 14 of tc1.xml:
  FORG0001: In content of element <extension1>: The content model does not allow element
  <absName1> to appear as the first child. The element is in namespace https://example.com
  but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 13 column 14 of tc1.xml:
  FORG0001: In content of element <extension1>: The content model does not allow element
  <absName1> to appear as the first child. The element is in namespace https://example.com
  but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 20 column 17 of tc1.xml:
  FORG0001: In content of element <extension2>: The content model does not allow element
  <nonAbsName2> to appear as the first child. The element is in namespace
  https://example.com but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 24 column 17 of tc1.xml:
  FORG0001: In content of element <extension2>: The content model does not allow element
  <nonAbsName2> to appear as the first child. The element is in namespace
  https://example.com but it should be in no namespace. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Error on line 28 column 8 of tc1.xml:
  XQDY0027  Five validation errors were reported. First error: In content of element <root>:
  The content model does not allow element <test1> to appear as the first child. The element
  is in namespace https://example.com but it should be in no namespace. 
Validation of file tc1.xml completed: errors found

Here is a link to the actual sample files for reference: xml / root schema - ext schema

Any help would be greatly appreciated :)

CodePudding user response:

Your immediate problem can be solved by adding

elementFormDefault="qualified"

to the xs:schema elements of your XSDs. For an in-depth explanation, see What does elementFormDefault do in XSD?

There remain additional validation issues. If their resolution is not obvious to you, post a new question, preferably with a complete MCVE that's independent of off-site resources.

  • Related