Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML schema; multiple from a list of valid attribute values

I'm reasonably new to working with XML schemas, so excuse my incompetence if this is more trivial than I myself believe it must be.

I'm trying to create a required attribute that must contain 1 or more white-space-separated string values from a list. The list is the 4 typical HTTP request methods; get, post, put, and delete.

So valid elements would include:

<rule methods="get" />
<rule methods="get post" />
<rule methods="post put delete" />

Whereas invalid elements would include:

<rule methods="get get" />
<rule methods="foobar post" />
<rule methods="get;post;put" />

I've tried fooling with enumerations and length, but I don't believe I'm understanding what I need to do (or for that matter if it is in fact possible, though it seems as though it should be)


This is where I'm at now, thanks to @tdrury:

<xs:attribute name="methods" use="required">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:whiteSpace value="collapse" />
            <xs:pattern value="(?:(?:get|post|put|delete)\s?){1,4}" />
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

Which works, except for repetition (such as get get or post post post) and absent whitespace (such as getpost or postputdelete)


Edit:

After playing around with this a bit, I came up with an idea: an enumeration of all possible sequences. Thankfully, this list is (for the time being) fixed to the four usual transport methods, get, post, put, and delete, so I figured:

<xs:restriction base="xs:string">
    <xs:whiteSpace value="collapse" />
    <xs:enumeration value="delete" />
    <xs:enumeration value="put" />
    <xs:enumeration value="put delete" />
    <xs:enumeration value="post" />
    <xs:enumeration value="post delete" />
    <xs:enumeration value="post put" />
    <xs:enumeration value="post put delete" />
    <xs:enumeration value="get" />
    <xs:enumeration value="get delete" />
    <xs:enumeration value="get put" />
    <xs:enumeration value="get put delete" />
    <xs:enumeration value="get post" />
    <xs:enumeration value="get post delete" />
    <xs:enumeration value="get post put" />
    <xs:enumeration value="get post put delete" />
</xs:restriction>

Can anyone see a reason that this would not be a good idea?

like image 590
Dan Lugg Avatar asked Dec 31 '11 15:12

Dan Lugg


People also ask

How can we provide multiple values to attributes in XML?

attributes cannot contain multiple values (elements can) attributes cannot contain tree structures (elements can) attributes are not easily expandable (for future changes)

Can XML have multiple schemas?

Schemas can be composed of one or more XML documents. These schema documents can be explicitly joined together using the include and import elements.

Can XML elements have multiple attributes?

An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.

How do I store multiple values in XML?

use a “primitive attribute” and append, with a separator character, the multiple values into one string, or. use the FME attribute list, or. retain one attribute value out of the multiple values.


2 Answers

The basic problem can be addressed with enumerations as well:

<xs:attribute name="methods" use="required">
    <xs:simpleType>
        <xs:restriction>
            <xs:simpleType>
                <xs:list>
                    <xs:simpleType>
                        <xs:restriction base="xs:token">
                            <xs:enumeration value="get"/>
                            <xs:enumeration value="post"/>
                            <xs:enumeration value="put"/>
                            <xs:enumeration value="delete"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:list>
            </xs:simpleType>
            <xs:minLength value="1"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

This unfortunately has the same limitation as the <xs:pattern> solution and cannot validate that each token in the list is unique. It does however address the whitespace issue (getpost would be rejected).

like image 125
DRH Avatar answered Sep 28 '22 08:09

DRH


You can use regular expressions as a restriction on a simpleType: http://www.w3.org/TR/xmlschema-2/#dt-pattern

I'm not a regex expert but it would be something like this:

<xs:attribute name="methods" use="required">
   <xs:simpleType>
      <xs:restriction base="xs:string">
         <xs:pattern value='((get|post|put|delete)[/s]*){4}'/>
      </xs:restriction>
   </xs:simpleType>
</xs:attribute>
like image 31
tdrury Avatar answered Sep 28 '22 08:09

tdrury