I'm new in XSD and have the following question, thanks a lot for your help!
Suppose I have the xml:
<DataGroup>
<Data name="name">Jane</Data>
<Data name="age">50</Data>
<Data name="state>MA</Data>
<Data name="zipcode">01000</Data>
</DataGroup>
I'd like to put restriction on:
when the attribute name equals to "age", the value of should be an integer and >20,
when the attribute name equals to "state", the value of should be two letters.
when the attribute name equals to "zip code", the value of should be \d{5}.
Can not modify the xml, any idea?
Thanks!
Using XSD 1.1 you can declare alternative types for each situation. Additionally you can restrict the number of <Data> elements to exactly four, and add an assertion to guarantee that each different attribute occurs exactly once:
<xs:element name="DataGroup">
<xs:complexType>
<xs:sequence>
<xs:element name="Data" maxOccurs="4" minOccurs="4">
<xs:alternative type="NameData" test="#name='name'" />
<xs:alternative type="AgeData" test="#name='age'" />
<xs:alternative type="ZipData" test="#name='zipcode'"/>
<xs:alternative type="StateData" test="#name='state'"/>
</xs:element>
</xs:sequence>
<xs:assert test="Data/#name='name' and Data/#name='age' and Data/#name='zipcode' and Data/#name='state'"></xs:assert>
</xs:complexType>
</xs:element>
Since <Data> is a simple type, you need to declare the attribute as an extension of each type:
<xs:complexType name="NameData">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="AgeData">
<xs:simpleContent>
<xs:extension base="AgeType">
<xs:attribute name="name" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ZipData">
<xs:simpleContent>
<xs:extension base="ZipType">
<xs:attribute name="name" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="StateData">
<xs:simpleContent>
<xs:extension base="StateType">
<xs:attribute name="name" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
The restrictions you declare in the types which are base for the simple types:
<xs:simpleType name="AgeType">
<xs:restriction base="xs:integer">
<xs:minExclusive value="20"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="StateType">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z]{2}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ZipType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{5}"/>
</xs:restriction>
</xs:simpleType>
This will validate your file with the restrictions you require.
Related
I want to write an XML schema (xsd 1.1) for a document containing options. Each option has a name and a type (like boolean, integer, string, etc ...) and a datum matching that type. The list of types is fixed, but quiet long. (Only 3 are listed in listing 3 for simplicity.)
How do I do this without a ridiculous amount of repetition?
Use case 1
Here is a valid document for this schema..
Listing 1:
<abc:options>
<abc:option name="is-enabled" type="boolean">false</abc:option>
<abc:option name="wing-span" type="float">1.2</abc:option>
</abc:options>
Use case 2
This document is not valid for this schema because the simple type bit is wrong for the #type attribute.
<abc:options>
<abc:option name="is-enabled" type="boolean">24</abc:option>
<abc:option name="wing-span" type="float">this-is-not-a-number!</abc:option>
</abc:options>
What I have tried so far ...
Listing 3 is my attempt so far. But it is bad because I have to re-declare the #name attribute for each datum type. Is there a better solution? That is to say, one where I don't have to re-declare the #name attribute for each possible datum type.
Listing 3:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:abc="http://www.example.com"
targetNamespace="http://www.example.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="options">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="abc:option" type="option-Type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="option-Datum-Type">
<xs:restriction base="xs:string">
<xs:enumeration value="boolean"/>
<xs:enumeration value="integer"/>
<xs:enumeration value="float"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="option-Type-boolean">
<xs:simpleContent>
<xs:extension base="xs:boolean">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="abc:option-Datum-Type" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="option-Type-string">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="abc:option-Datum-Type" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="option-Type-float">
<xs:simpleContent>
<xs:extension base="xs:double">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="abc:option-Datum-Type" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="option-Type">
<xs:alternative test="#type='boolean'" type="abc:option-Type-boolean"/>
<xs:alternative test="#type='string'" type="abc:option-Type-string" />
<xs:alternative test="#type='float'" type="abc:option-Type-float" />
<xs:alternative type="xs:error"/>
</xs:complexType>
</xs:schema>
If the type can only be one of the atomic types you can use xs:assert like this:
<xs:complexType name="option-Type">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
<xs:assert
test="if (#type='boolean') then . castable as xs:boolean
else if (#type='float') then . castable as xs:float
else if (#type='int') then . castable as xs:int
else if (#type='string') then . castable as xs:string
else false()"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
Notes:
You don't need to declare any new type. If you want you can even also skip the declaration of the enumeration.
Using this approach you need a new line for every new possible type (you don't really need a new line but it is easy to read with every type in a different line).
You can use text() instead of . if you found it to be more clear
Notice how simply this approach would be if XPath 2.0 had an eval function similar to javascript and other languages eval function:
<xs:assert test="eval(text() || ' castable as xs:' || #type)"/>
I though an eval/parse function was going to be added to XPath 3.0 spec but I think it finally has not been added.
Unlike instance of you cannot use lists (*,+) other than ? with castable as operator. You can only use atomic types using this approach.
Cast to string string should always succed as the type is declared as xs:string.
I would like to write a XSD element that permit something like that :
<CustomFields>
<CustomField id="1">some text data</CustomField>
<CustomField id="2">some text data</CustomField>
</CustomFields>
But I have some restrictions : I have to restrict the text (maxLenght = 36). And I would like to be impossible to have 2 CustomField with the same id.
So far I wrote this, but it not what I want :
<xs:element name="CustomFields" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomField" minOccurs="0" maxOccurs="20">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:integer" use="required"></xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Thanks for any help.
Regards.
You could use following attempt
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="restrictedString">
<!-- Make a new type to be a "descendant" of string-->
<xs:restriction base="xs:string">
<xs:maxLength value="36"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="CustomFields">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomField" minOccurs="0" maxOccurs="20">
<xs:complexType>
<xs:simpleContent>
<!-- reference new type you declared above -->
<xs:extension base="restrictedString">
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!-- Restrict #id to be unique-->
<xs:unique name="id_uq">
<xs:selector xpath="CustomField"/>
<xs:field xpath="#id"/>
</xs:unique>
</xs:element>
</xs:schema>
Right now oxygen is telling me to terminate complex type despite it already being terminated. Why is that happening? I tried removing the simpleType. I tried taking out the complex type and it still won't accept it. Here is the code.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.engr.iupui.edu/~efernand/account" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:account="http://www.engr.iupui.edu/~efernand/account">
<xs:element name="account">
<xs:complexType>
<xs:sequence>
<xs:element ref="account:owner"/>
<xs:choice maxOccurs="unbounded">
<xs:element ref="account:deposit"/>
<xs:element ref="account:payment"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="number" use="required" type="xs:NCName"/>
<xs:attribute name="type" use="required" />
<xs:simpleType>
<xs:restriction base="xs:NCName">
<xs:enumeration value="direct"/>
<xs:enumeration value="check"/>
<xs:enumeration value="cash"/>
<xs:enumeration value="transfer"/>
<xs:enumeration value="atm" />
</xs:restriction>
</xs:simpleType>
</xs:attribute> <!--ERROR HERE-->
</xs:complexType>
</xs:element>
<xs:element name="owner">
<xs:complexType>
<xs:sequence>
<xs:element ref="account:name"/>
<xs:element ref="account:address"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="name">
<xs:complexType>
<xs:sequence>
<xs:element ref="account:first"/>
<xs:element ref="account:last"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="first" type="xs:NCName"/>
<xs:element name="last" type="xs:NCName"/>
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element ref="account:street"/>
<xs:element ref="account:city"/>
<xs:element ref="account:state"/>
<xs:element ref="account:zip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:NCName"/>
<xs:element name="state">
<xs:simpleType>
<xs:restriction base="xs:NCName">
<xs:pattern value="[a-zA-Z]{2}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="zip" type="xs:integer"/>
<xs:element name="deposit">
<xs:complexType>
<xs:sequence>
<xs:element ref="account:from"/>
<xs:element ref="account:amount"/>
<xs:element ref="account:date"/>
<xs:element minOccurs="0" ref="account:description"/>
</xs:sequence>
<xs:attribute name="type" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="from">
<xs:complexType mixed="true">
<xs:attribute name="category" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="payment">
<xs:complexType>
<xs:sequence>
<xs:element ref="account:to"/>
<xs:element ref="account:amount"/>
<xs:element ref="account:date"/>
<xs:element minOccurs="0" ref="account:description"/>
</xs:sequence>
<xs:attribute name="checknum">
<xs:simpleType>
<xs:restriction base="xs:NCName">
<xs:pattern value="C[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="type" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="to">
<xs:complexType mixed="true">
<xs:attribute name="category" use="required">
<xs:simpleType>
<xs:restriction base="xs:NCName">
<xs:enumeration value="income" />
<xs:enumeration value="other"/>
<xs:enumeration value="cash"/>
<xs:enumeration value="food"/>
<xs:enumeration value="utilites"/>
<xs:enumeration value="clothing"/>
<xs:enumeration value="savings"/>
<xs:enumeration value="entertainment"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="amount" type="xs:decimal"/>
<xs:simpleType name="amount">
<xs:restriction base="xs:decimal">
<xs:minExclusive value="0"/>
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="date" type="xs:date"/>
<xs:simpleType name="date">
<xs:restriction base="xs:date">
<!--No month number begins with 2-->
<xs:pattern value="[0-9]{4}-[0-1][0-9]-[0-9]{2}"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="description" type="xs:string"/>
</xs:schema>
<xs:attribute name="type" use="required" />
I don't think you want the / there.
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.
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>
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>
to make xsd for element with same names that only identifyed by attribute value example :-
<a>
<b n="structure one">
<c n="inner element 1"/>
<c n="inner element 2"/>
<c n="inner element 3"/>
</b>
<b n="structure two">
<c n="inner element 1 for structure two"/>
<c n="inner element 2 for structure two"/>
<c n="inner element 3 for structure two"/>
</b>
</a>
notice that from the XML i have to mention specific value that belong to the inner element same for structure
Not sure what your specific requirements are, but the following schema validates your document. It says that the root element must be named a, and it can contain any number of b elements, which themselves contain any number of c elements. The b and c elements must contain the attribute with the name n.
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="b">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="c">
<xs:complexType>
<xs:attribute name="n" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="n" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If you want to constrain the attributes to a specific set of values, you can use a restriction. This schema enumerates the possible values of the n attributes on the b and c elements:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="b" type="b"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="b">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="c">
<xs:complexType>
<xs:attribute name="n" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="inner element 1"/>
<xs:enumeration value="inner element 2"/>
<xs:enumeration value="inner element 3"/>
<xs:enumeration value="inner element 1 for structure two"/>
<xs:enumeration value="inner element 2 for structure two"/>
<xs:enumeration value="inner element 3 for structure two"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="n" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="structure one"/>
<xs:enumeration value="structure two"/>
<xs:enumeration value="structure three"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:schema>
You can also constrain the values of the attributes with a regex pattern, like this:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="b" type="b"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="b">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="c">
<xs:complexType>
<xs:attribute name="n" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="^inner element [0-9]+.*$"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="n" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="structure (one|two|three)"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:schema>