3

In my Java (Maven) project I'm using JAXB 2.2.11 to rebuild instances of a class from an XML payload received by our servers. I have an .xsd schema defining the class, which works great in conjunction with JAXB to rebuild an instance of the type I want. The issue I'm having is that those payloads can (without any notice or warning) have extra elements which I don't really care about.

One of the places where those extra elements can appear is within an xs:all tag. I do like having the functionality of said xs:all tag:

The all element specifies that the child elements can appear in any order and that each child element can occur zero or one time.

However, I don't want to get a parsing error while processing an XML payload that contains extra attributes. An xs:any tag inside the xs:all would work great, but it's not permitted in XSD 1.0 (according to w3schools and this other SO answer) and apparently, JAXB doesn't support XSD 1.1. Also, the way JAXB treats the any or the anyAttribute is very interesting, because it puts all the unknown nodes into a map, so I can log it saying "Hey! We are receiving an attribute that we don't really care about as of now, but maybe you'll find it somehow useful in the future?"

I've read about Xsom, that supports XSD 1.1, but apparently, it doesn't return an instance of the class you want, but more generic set of hash-maps and lists, therefore losing my type checking, which is something I don't want to.

So... Is there any way of pretending to have an xs:any within an xs:all?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Savir
  • 17,568
  • 15
  • 82
  • 136

1 Answers1

1

Correct. XSD 1.0 does not allow xsd:any within xsd:all; XSD 1.1 does.

Workarounds for XSD 1.0 not allowing xsd:any in xsd:all

  1. Confine xsd:any to a fixed wrapper element such as expansion with xsd:all:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="root">
        <xsd:complexType>
          <xsd:all>
            <xsd:element name="a"/>
            <xsd:element name="b"/>
            <xsd:element name="expansion">
              <xsd:complexType>
                <xsd:sequence>
                  <xsd:any maxOccurs="unbounded"/>
                </xsd:sequence>
              </xsd:complexType>
            </xsd:element>
          </xsd:all>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    
  2. Abandon the unordered allowance of xsd:all and let the xsd:any elements follow in sequence:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="root">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="a"/>
            <xsd:element name="b"/>
            <xsd:any maxOccurs="unbounded"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    
kjhughes
  • 106,133
  • 27
  • 181
  • 240