Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define XSD to allow any element

Tags:

xml

xsd

I want to define an xsd for a parameter element which will allow me to define the parameter in the following manners

<parameter name="save.type" value="attribute" />

or

<parameter name="payload">
        <p:AdderProcessRequest xmlns:p="http://wso2.org/bps/sample">
            <!--Exactly 1 occurrence -->
            <x xmlns="http://wso2.org/bps/sample">{@xvalue}</x>
            <!--Exactly 1 occurrence -->
            <y xmlns="http://wso2.org/bps/sample">{@yvalue}</y>
        </p:AdderProcessRequest>
</parameter>

In the second approach the xml content within the parameter element is not known beforehand so it can be anything.

The following is the xsd i created but it doesn't seem to work.

<xs:element name="parameter" maxOccurs="unbounded" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:any minOccurs="0"/>
        </xs:sequence>
        <xs:attribute type="xs:string" name="name" use="optional"/>
        <xs:attribute type="xs:string" name="value" use="optional"/>
    </xs:complexType>
</xs:element>

Any help with this will be much appreciated. Thanks in advance

like image 473
pulasthi Avatar asked Jun 04 '13 12:06

pulasthi


People also ask

How do you make an element required in XSD?

The "use" property in the XSD definition is used to specify if the attribute is optional or mandatory. To specify that an attribute must be present, use="required" (Note: use may also be set to "prohibited", but we'll come to that later).

How can you extend the document with elements not specified by the schema?

The <any> element enables us to extend the XML document with elements not specified by the schema.

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

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.


1 Answers

I was able to figure it out after going through the specs posting it here so someone else might need it :). You have to add processContents="skip" so the content won't be processed.

<xs:element name="parameter" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
    <xs:sequence>
        <xs:any processContents="skip" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="name" use="optional"/>
    <xs:attribute type="xs:string" name="value" use="optional"/>
</xs:complexType>

like image 134
pulasthi Avatar answered Oct 17 '22 13:10

pulasthi