Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define an XSD file that allows unknown (wildcard) elements?

Tags:

xml

xsd

I'm receiving an XML message with unknown variable name elements... that is, they are not predefined...

I only know there can be 0 or more of those elements, along with some other that are mandatory...

For example

<root>
    <service>my service</service>
    <resource>my resource</resource>
    <action>update</action>
    <parameters>
      <field1>value1</field1>
      <field2>value2</field2>
      <field3>value3</field3>
    </parameters>
</root>

that is, I don't know what will be passed as "parameters", I only know there will be 0 or more elements with a value, no deeper tag nesting allowed....

I was thinking about something like

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="root">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="service" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
      <xs:element name="resource" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
      <xs:element name="action" type="xs:string" minOccurs="1" maxOccurs="1" nillable="false"/>
      <xs:element name="parameters">
        <xs:complexType>
          <xs:element name="*" maxOccurs="unbounded">
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

of course, the hard part is

<xs:element name="*" maxOccurs="unbounded">

Is it possible to do such a thing?
How can I define an XSD file that validates such a message?

--

I checked the w3c reference at

http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html#NCName

and it says:

The ·lexical space· of NCName is the set of all strings which ·match· the NCName production of [Namespaces in XML].

So what does it mean?
besides... could you recommend me some easy way to test compliance with an XSD definition?

like image 586
opensas Avatar asked Jun 03 '09 08:06

opensas


People also ask

How do you define an element attribute in XSD?

An attribute provides extra information within an element. Attributes are defined within an XSD as follows, having name and type properties. An Attribute can appear 0 or 1 times within a given element in the XML document. Attributes are either optional or mandatory (by default the are optional).

Which element is used to indicate that elements defined in the XSD must appear?

The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

What is simpleType and complexType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is Substitutiongroup in XSD?

A substitution group is a construct in XML Schema (XSD) that allows data architects to create a set of elements that can be substituted for a head element. Any top-level element can be defined as the head element of a substitution group.


1 Answers

What you want is a wildcard particle, for details see http://www.w3.org/TR/xmlschema-1/#Wildcards

To do it you can use xs:any. Note that xs:element and xs:any cannot be placed directly inside an xs:complexType. You need a container like a xs:sequence or xs:choice.

A valid schema that handles wildcards is below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="service" type="xs:string"/>
        <xs:element name="resource" type="xs:string"/>
        <xs:element name="action" type="xs:string"/>
        <xs:element name="parameters">
          <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
              <xs:any processContents="lax"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
</xs:schema>
like image 51
George Bina Avatar answered Sep 22 '22 08:09

George Bina