XML schema for element whose simple type is driven by an attribute - xsd

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.

Related

s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation' [duplicate]

I have this xml-Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Fussballmannschaft">
<xs:complexType>
<xs:attribute name="Name" type="xs:string" />
<xs:attribute name="Stadt" type="xs:string" />
<xs:attribute name="Tabellenplatz" type="xs:string" />
<xs:element name="Spieler">
<xs:complexType>
<xs:attribute name="SpielerID" type="xs:string" />
<xs:keyref name="I_D" refer="Name" />
<xs:attribute name="Name" type="xs:string" />
<xs:element name="Torwart">
<xs:attribute name="GehealteneElfmeter" type="xs:integer" />
<xs:keyref name="ID_Torwart" refer="SpielerID" />
</xs:element>
<xs:element name="Verteidiger">
<xs:attribute name="GewonneneZweikaempfe" type="xs:integer" />
<xs:keyref name="ID_Verteidiger" refer="SpielerID" />
</xs:element>
<xs:element name="Stuermer">
<xs:attribute name="GeschosseneTore" type="xs:integer" />
<xs:keyref name="ID_Stuermer" refer="SpielerID" />
</xs:element>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>
and this sample:
<Fussballmannschaft Name="BVB">
<Stadt>Dortmund</Stadt>
<Tabellenplatz>3</Tabellenplatz>
<Spieler SpielerID="1">
<I_D>BVB</I_D>
<Name>Oliver</Name>
<Torwart>
<GehealteneElfmeter>20</GehealteneElfmeter>
<ID_Torwart>1</ID_Torwart>
</Torwart>
</Spieler>
<Spieler SpielerID="2">
<I_D>BVB</I_D>
<Name>Peter</Name>
<Torwart>
<GewonneneZweikaempfe>20</GewonneneZweikaempfe>
<ID_Verteidiger>2</ID_Verteidiger>
</Torwart>
</Spieler>
<Spieler SpielerID="3">
<I_D>BVB</I_D>
<Name>Paul</Name>
<Torwart>
<GeschosseneTore>20</GeschosseneTore>
<ID_Stuermer>3</ID_Stuermer>
</Torwart>
</Spieler>
</Fussballmannschaft>
But the parser says:
S4s-elt-character: Non-whitespace Characters Are Not Allowed In Schema Elements Other Than 'xs:appinfo' And 'xs:documentation'. Saw 'Dortmund'.
Do you know where the problem is?
The real reason you're getting this error,
S4s-elt-character: Non-whitespace Characters Are Not Allowed In Schema Elements Other Than 'xs:appinfo' And 'xs:documentation
is that you're attempting to validate your XML file as an XSD file.
So, fix the way you're invoking your validating parser so that you're validating your XML file against your XSD. See How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?
Additionally, there are a slew of other problems with your XSD itself. See collapsar's answer for help there.
We also had this problem. The reason was the address of the schema http:://xyz.xsd was replaced with https:://xyz.xsd. Our schema library was not able to forward to the https address via http one. So we simply changed the http adresses to https ones in the xml file.
Stadt and Tabellenplatz mustn't be xml elements but need to be attributes of the element Fussballmannschaft. This pattern repeats with the inner elements.
The schema also lacks structures to express repetition of elements (namely the Spieler element) and choices between player roles (Torwart,Verteidiger,Stuermer).
The use of the xs:keyref in the schema definition appears to be incomplete - the referenced keys are not specified as xs:key elements. In order to demonstrate this use, a new root element Fussball is defined, which should reflect the intent of the schema to formalize the notion of soccer teams. This new root element harbors the key and keyref definitions for the club name attribute and will be needed anyway as soon as multiple teams are to be represented in in a file (there must be a single root element in an xml file ).
The following pair of schema and sample passes the validation.
Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Fussball">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="Fussballmannschaft">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="Spieler" maxOccurs="unbounded">
<xs:complexType mixed="true">
<xs:choice>
<xs:element name="Torwart">
<xs:complexType>
<xs:attribute name="GehalteneElfmeter" type="xs:integer" />
<xs:attribute name="ID_Torwart" type="xs:integer" />
</xs:complexType>
</xs:element>
<xs:element name="Verteidiger">
<xs:complexType>
<xs:attribute name="GewonneneZweikaempfe" type="xs:integer" />
<xs:attribute name="ID_Verteidiger" type="xs:integer" />
</xs:complexType>
</xs:element>
<xs:element name="Stuermer">
<xs:complexType>
<xs:attribute name="GeschosseneTore" type="xs:integer" />
<xs:attribute name="ID_Stuermer" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:choice>
<xs:attribute name="I_D" type="xs:string" />
<xs:attribute name="SpielerID" type="xs:integer" />
<xs:attribute name="Spielername" type="xs:string" />
</xs:complexType>
</xs:element><!-- Spieler -->
</xs:sequence>
<xs:attribute name="Name" type="xs:string" />
<xs:attribute name="Stadt" type="xs:string" />
<xs:attribute name="Tabellenplatz" type="xs:string" />
</xs:complexType>
<xs:key name="k-Spieler">
<xs:selector xpath="./Spieler"/>
<xs:field xpath="#SpielerID"/>
</xs:key>
<xs:keyref name="kref-Spieler" refer="k-Spieler">
<xs:selector xpath="./Spieler/Stuermer|./Spieler/Torwart|./Spieler/Verteidiger"/>
<xs:field xpath="#ID_Stuermer|#ID_Torwart|#ID_Verteidiger"/>
</xs:keyref>
</xs:element><!-- Fussballmannschaft -->
</xs:sequence>
</xs:complexType>
<!--
A 'key' tells you how to uniquely reference an element instance - eg. one among several soccer teams.
-->
<xs:key name="k-Verein">
<xs:selector xpath="./Fussballmannschaft"/>
<xs:field xpath="#Name"/>
</xs:key>
<!--
A 'keyref' specifies that some attribute value(s) are not only constrained by their datatype but must also uniquely identify a certain element in the file - a semantic relationship is established.
-->
<xs:keyref name="kref-Verein" refer="k-Verein">
<xs:selector xpath="./Fussballmannschaft/Spieler"/>
<xs:field xpath="#I_D"/>
</xs:keyref>
</xs:element><!-- Fussball -->
</xs:schema>
XML:
<Fussball>
<Fussballmannschaft Name="BVB" Stadt="Dortmund" Tabellenplatz="3">
<Spieler SpielerID="1" I_D="BVB" Spielername="Oliver">
<Torwart GehalteneElfmeter="20" ID_Torwart="1"/>
</Spieler>
<Spieler SpielerID="2" I_D="BVB" Spielername="Peter">
<Verteidiger GewonneneZweikaempfe="20" ID_Verteidiger="2"/>
</Spieler>
<Spieler SpielerID="3" I_D="BVB" Spielername="Paul">
<Stuermer GeschosseneTore="20" ID_Stuermer="3"/>
</Spieler>
</Fussballmannschaft>
</Fussball>
References
W3C XML Schema Definition
W3C XML Schema Definition / identity constraints
SO answer on the use of key and keyref

Add any to complexType

I have this XML schema:
<xs:element name="lineinfo">
<xs:complexType>
<xs:all>
<xs:element name="done" type="xs:unsignedInt" />
</xs:all>
<xs:attribute name="id" type="xs:long" />
<xs:anyAttribute processContents="skip" />
</xs:complexType>
</xs:element>
but I want to allow any other extra element in the lineinfo tag:
<lineinfo state="assigned" id="175">
<done>4</done>
<todo>6</todo>
</lineinfo>
I tried to add <xs:any /> inside the <xs:all>, but it doesn't seem to be allowed.
I couldn't find a way to do what I wanted, so I ended up adding all "unwanted" tags in my list, with minOccurs set to 0:
<xs:element name="lineinfo">
<xs:complexType>
<xs:all>
<xs:element name="done" type="xs:unsignedInt" />
<xs:element name="todo" minOccurs="0" />
<xs:element name="error" minOccurs="0" />
</xs:all>
<xs:attribute name="id" type="xs:long" />
<xs:anyAttribute processContents="skip" />
</xs:complexType>
</xs:element>
Parent tag of <xs:any> are only choice, sequence. w3cschools #el_any
To use <xs:any> put <xs:sequence> instead of <xs:all>. w3cschools #any
else you could use xs:anyType
xs:anyType is a type, like xs:integer (though xs:anyType is special
in that it can act as a simple or complex type, and it places
essentially no restrictions on the tree that it validates -- think of
it loosely as the Schema language's analog of java.lang.Object).
A sample use would be:
<xsd:element name="value" type="xs:anyType"/>
Anyway if you want use below an example taken from w3cschools #anyattribute
SCHEMA
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
<xs:attribute name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
XML
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>

XSD: Different Restriction by Attribute Values

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.

XSD Restriction on Attribute

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>

How to define a class within a class for 1 to 1 relationship in xsd

If I define my xsd schema like below , the xs:element is giving an error. I want to have a 1:1 relationship between BitStatType and ValidationCountMessage? How can I define a class within a class -- This is for JAXB java.
<xs:complexType name="BitStatType">
<xs:element name="BitAgeViolation" type="ValidationCountMessage" />
<xs:attribute name="WiId" type="xs:int" use="required"/>
<xs:attribute name="Title" type="xs:string" use="required"/>
<xs:attribute name="TriggerRulesCount" type="xs:int" use="required"/>
<xs:attribute name="TriggerEventsCount" type="xs:int" use="required"/>
<xs:attribute name="LastPublishedDate" type="xs:date" use="required"/>
</xs:complexType>
Firstly, you're missing an xs:sequence (just in case, not sure if it got lost in posting or what).
<xs:complexType name="BitStatType">
<xs:sequence>
<xs:element name="BitAgeViolation" type="ValidationCountMessage"/>
</xs:sequence>
<xs:attribute name="WiId" type="xs:int" use="required"/>
<xs:attribute name="Title" type="xs:string" use="required"/>
<xs:attribute name="TriggerRulesCount" type="xs:int" use="required"/>
<xs:attribute name="TriggerEventsCount" type="xs:int" use="required"/>
<xs:attribute name="LastPublishedDate" type="xs:date" use="required"/>
</xs:complexType>
If your ValidationCountMessage is a complexType then JAXB should create two classes, one for BitStatType and one for ValidationCountMessage; the BitStatType class will have a field bitAgeViolation, of type ValidationCountMessage. So, you'll have an association between these two classes.

Resources