views:

483

answers:

2

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, allong 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"&gt;

<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 sucha 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 recomend me some easy way to test compliance with an xsd definition?

+1  A: 

Try this:

<xs:element name="parameters">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:any processContents="lax"></xs:any>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
Rashmi Pandit
+3  A: 

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 sequence or 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"&gt;

  <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>
George Bina