Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD Restriction on Attribute

Tags:

I think I have searched a lot about this but still no go.

Will appreciate any help.

I am trying to restrict an attribute for an element with empty content. "color" should have a restriction to only hold 3 digit or minLength=3 and maxLength=3. It should not have any content.

<?xml version="1.0" encoding="utf-8"?>   <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="">   <product id="" name="">     <article id="1001">       <umbrella color="100"/>       <umbrella color="101"/>     </article>     <article id="1002">       <umbrella color="110"/>     </article>   </product> </items> 

EDIT: I know how to do a XSD Restriction on a simpleType. But I don't how to combine it to one entity with a ComplexType.

If you could provide a more detailed (or full) solution I would be happy.

Btw, "color" is not limited to xs:integer. It is actually a xs:string.

like image 403
ZiggyStardust Avatar asked Jan 11 '13 00:01

ZiggyStardust


People also ask

What is restriction in XSD?

restriction. restriction is normally a range of conditions to be applied on the element's value. In this example, we've set a restriction on marks that marks should be in range of 0 to 100 with both values are included. <xs:minInclusive value = "0"/> <xs:maxInclusive value = "100"/>

What is the difference between attribute and element in XSD?

An element is an XML node - and it can contain other nodes, or attributes. It can be a simple type or a complex type. It is an XML entity. An attribute is a descriptor.


2 Answers

You can define your attribute similar to the following. This example uses a pattern to restrict the value, but you could also use min and max if that's more appropriate.

<xs:attribute name="color">     <xs:simpleType>         <xs:restriction base="xs:integer">             <xs:pattern value="[0-9][0-9][0-9]"/>         </xs:restriction>     </xs:simpleType> </xs:attribute> 

Then in your element definition, you just use a ref to reference the defined attribute:

<xs:attribute ref="color"/> 

UPDATE (in response to comment from OP):

Here's what the entire schema might look like:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">      <xs:attribute name="color">         <xs:simpleType>             <xs:restriction base="xs:integer">                 <xs:pattern value="[0-9][0-9][0-9]"/>             </xs:restriction>         </xs:simpleType>     </xs:attribute>      <xs:attribute name="id">         <xs:simpleType>             <xs:restriction base="xs:integer">                 <xs:pattern value="[0-9][0-9][0-9][0-9]"/>             </xs:restriction>         </xs:simpleType>     </xs:attribute>      <xs:attribute name="name" type="xs:string"/>      <xs:complexType name="article_type">         <xs:attribute ref="color" use="required"/>     </xs:complexType>      <xs:element name="article">         <xs:complexType>             <xs:choice maxOccurs="unbounded" minOccurs="0">                 <xs:element name="umbrella" type="article_type"/>             </xs:choice>             <xs:attribute ref="id" use="required"/>         </xs:complexType>     </xs:element>      <xs:element name="product">         <xs:complexType>             <xs:choice maxOccurs="unbounded" minOccurs="0">                 <xs:element ref="article"/>             </xs:choice>             <xs:attribute ref="id" use="required"/>             <xs:attribute ref="name"/>         </xs:complexType>     </xs:element>      <xs:element name="items">         <xs:complexType>             <xs:choice maxOccurs="unbounded" minOccurs="0">                 <xs:element ref="product"/>             </xs:choice>         </xs:complexType>     </xs:element>  </xs:schema> 
like image 115
David Avatar answered Sep 24 '22 05:09

David


The following should work

 <element name="umbrella" nillable="true" type="umbrellaType"> 

<complexType name="umbrellaType">    <attribute name="color">      <simpleType>        <restriction base="int">         <minExclusive value="99"></minExclusive>         <maxInclusive value="999"></maxInclusive>        </restriction>      </simpleType>    </attribute> </complexType> 
like image 44
Baski Avatar answered Sep 22 '22 05:09

Baski