XSD Restriction on Attribute - attributes

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>

Related

XSD and xmllint : replacing xs:sequence with xs:all gives an error

I want to validate the following XML, without imposing the order of points :
<?xml version='1.0' encoding='ISO-8859-1'?>
<root>
<name>Map corners</name>
<point>NW</point>
<point>SW</point>
<point>NE</point>
<point>SE</point>
</root>
My XSD is :
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="point" type="xs:string" fixed="NW"/>
<xs:element name="point" type="xs:string" fixed="SW"/>
<xs:element name="point" type="xs:string" fixed="NE"/>
<xs:element name="point" type="xs:string" fixed="SE"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xmllint validates my XML.
But if I replace xs:sequence with xs:all (to release the order constraint : SW may be before NW), it doesn't validate, and I have the following message :
my6.xsd:4: element complexType: Schemas parser error : local complex type: The content model is not determinist.
Did I miss something ?
In XSD 1.0, you can't use xsd:any with a repeating element particle. You can do something like this:
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="point" minOccurs="4" maxOccurs="4">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="NW"/>
<xs:enumeration value="SW"/>
<xs:enumeration value="NE"/>
<xs:enumeration value="SE"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="pk_points">
<xs:selector xpath="point"/>
<xs:field xpath="."/>
</xs:key>
</xs:element>
</xs:schema>

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>

How to fully define an XML Schema for same element name but differing subtype

Hi again SO community,
got another request regarding how to define an XSD schema correctly.
Given I have an XML file like:
<?xml version="1.0" encoding="UTF-8" ?>
<transformation xmlns="http://www.denktmit.de/pdi/ktr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.w3.org/2001/XMLSchema-instance
./test.xsd">
<info>
<name>TrafoName</name>
<description>TrafoDescription</description>
</info>
<changedby>LastUser</changedby>
<step>
<type>FileInput</type>
<description>FileInput Step description</description>
<FileInputParameterOne>FileInputParam</FileInputParameterOne>
<another>1</another>
</step>
<step>
<type>Select</type>
<description>Select Step description</description>
<SelectParameterOne>SelectParameterOne</SelectParameterOne>
<SelectParameterTwo>SelectParameterTwo</SelectParameterTwo>
<another>2</another>
</step>
</transformation>
This resembles the style of the transformation files as produced by http://community.pentaho.com/projects/data-integration/
As you can see, each step contains several sequence nodes on top
<type>Select</type>
<description>Select Step description</description>
and on bottom:
<another>2</another>
In between, arbitrary complex xml subtrees may occur. But what exactely max occur is determined by the value in "type". The use case is to store specific configurations for concrete Java classes, that all implement an interface, defining getters/setter for "type", "description" and "another" and defining addtional object structure themself. It would also be sufficient to neglect the order, as long as all the elements, needed for a specific class are somehow present.
I already read:
How to group types & subtypes in XSD
xsd: How to extend a type with an unordered list of elements
Use <xs:all> in XML schema's complexType?
xsd: How to extend a type with an unordered list of elements
And I tried something like
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.denktmit.de/pdi/ktr" elementFormDefault="qualified"
attributeFormDefault="unqualified" xmlns="http://www.denktmit.de/pdi/ktr">
<xs:element name="transformation">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="step" type="fileInputStep"></xs:element>
<xs:element name="step" type="selectStep"></xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="stepComplexType" abstract="true"></xs:complexType>
<xs:complexType name="fileInputStep">
<xs:complexContent>
<xs:extension base="stepComplexType">
<xs:sequence>
<xs:element name="type" minOccurs="1"
maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="description" type="xs:string"
minOccurs="1" maxOccurs="1"></xs:element>
<xs:element name="FileInputParameterOne" type="xs:string">
</xs:element>
<xs:element name="another" type="xs:int" minOccurs="1"
maxOccurs="1"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="selectStep">
<xs:complexContent>
<xs:extension base="stepComplexType">
<xs:sequence>
<xs:element name="type" minOccurs="1"
maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="Select"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="description" type="xs:string"
minOccurs="1" maxOccurs="1"></xs:element>
<xs:element minOccurs="1" name="SelectParameterOne"
type="xs:string">
</xs:element>
<xs:element minOccurs="1" name="SelectParameterTwo"
type="xs:string">
</xs:element>
<xs:element name="another" type="xs:int" minOccurs="1"
maxOccurs="1"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
But it is not working, telling me:
Multiple annotations found at this line: validation against this
schema, ambiguity would be created for those two particles.
- Start tag of element
- cos-element-consistent: Error for type '#AnonType_transformation'. Multiple elements with name 'step', with different types, appear in
the model group.
Any ideas?
You may use xsd Group. Here's an sample for your reference
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="transformation">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="step" type="stepType"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="stepComplexType" abstract="true"></xs:complexType>
<xs:group name="selectParamGroup">
<xs:sequence>
<xs:element name="SelectParameterOne" type="xs:string" minOccurs="1" maxOccurs="1">
</xs:element>
<xs:element name="SelectParameterTwo" type="xs:string" minOccurs="1" maxOccurs="1">
</xs:element>
</xs:sequence>
</xs:group>
<xs:group name="paramGroup">
<xs:choice>
<xs:element name="FileInputParameterOne" type="xs:string">
</xs:element>
<xs:group ref="selectParamGroup">
</xs:group>
</xs:choice>
</xs:group>
<xs:complexType name="stepType">
<xs:complexContent>
<xs:extension base="stepComplexType">
<xs:sequence>
<xs:element name="type" minOccurs="1"
maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="description" type="xs:string"
minOccurs="1" maxOccurs="1"></xs:element>
<xs:group ref="paramGroup"></xs:group>
<xs:element name="another" type="xs:int" minOccurs="1"
maxOccurs="1"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<transformation>
<step>
<type>FileInput</type>
<description>FileInput Step description</description>
<FileInputParameterOne>FileInputParam</FileInputParameterOne>
<another>1</another>
</step>
<step>
<type>Select</type>
<description>Select Step description</description>
<SelectParameterOne>SelectParameterOne</SelectParameterOne>
<SelectParameterTwo>SelectParameterTwo</SelectParameterTwo>
<another>2</another>
</step>
</transformation>

XSD for elements string with attribute id

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>

xsd element with the same name

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>

Resources