How to get the white space in xsd? - xsd

Following is the code snippet :
<xs:element name="A_Record">
<xs:complexType>
<xs:sequence>
<xs:element name="A8_Filler" type="xs:string" />
<xs:element name="A10_ReferenceNumber" type="xs:string" />
<xs:element name="A11b_EffectiveDate" type="xs:string" />
<xs:element name="A11c_Filler" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
I need to have whitespaces in A8_Filler, A11b_EffectiveDate and A11c_Filler. How can I do this?

Turning krio's comment into an answer, try:
<xs:element name="A_Record">
<xs:complexType>
<xs:sequence>
<xs:element name="A8_Filler">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="A10_ReferenceNumber" type="xs:string" />
<xs:element name="A11b_EffectiveDate">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="A11c_Filler">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
... or even create a reusable type:
<xs:element name="A_Record">
<xs:complexType>
<xs:sequence>
<xs:element name="A8_Filler" type="MyWhitespaceStringType" />
<xs:element name="A10_ReferenceNumber" type="xs:string" />
<xs:element name="A11b_EffectiveDate" type="MyWhitespaceStringType" />
<xs:element name="A11c_Filler" type="MyWhitespaceStringType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="MyWhitespaceStringType">
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve" />
</xs:restriction>
</xs:simpleType>

Related

Simple XSD Schema

I have problem with my XSD schema. Task is simple, a have this xml:
<?xml version="1.0" encoding="UTF-8"?>
<Pisemnost nazevSW="EPO MF ČR" verzeSW="40.23.1">
<DPPDP8 verzePis="04.01">
<VetaP>
<VetaO>
<VetaU> All these elements can be repeated and have any order.
<VetaE> They can have any attributes, but they have no content.
<VetaF>
<VetaS>
<VetaUA>
<VetaUB>
<VetaUD>
<Prilohy>
-- Any content ---
</Prilohy>
</DPPDP8>
-- Any content ---
</Pisemnost>
I make this XSD schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Pisemnost">
<xs:complexType>
<xs:sequence maxOccurs="1">
<xs:element name="DPPDP8">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:choice>
<xs:element name="VetaD" />
<xs:element name="VetaE" />
<xs:element name="VetaF" />
<xs:element name="VetaO" />
<xs:element name="VetaP" />
<xs:element name="VetaS" />
<xs:element name="VetaU" />
<xs:element name="VetaUA" />
<xs:element name="VetaUB" />
<xs:element name="VetaUD" />
</xs:choice>
</xs:sequence>
<xs:attribute name="verzePis" />
</xs:complexType>
</xs:element>
<xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="nazevSW" />
<xs:attribute name="verzeSW" />
</xs:complexType>
</xs:element>
</xs:schema>
I would like to add "Prilohy" element with any content to XSD, but all my ideas ends with an invalid XSD. Please, can anybody help me? I ask for help, because I cant XSD and it is complicated for me. Thanks for any ideas :)
According to the reference DPPDP8 schema from here It should look like this:
<xs:element maxOccurs="1" minOccurs="0" name="Prilohy">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="ObecnaPriloha">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:base64Binary">
<xs:attribute name="cislo" use="required">
<xs:annotation>
<xs:documentation>Pořadové číslo přílohy</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="4"/>
<xs:fractionDigits value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="nazev" use="optional">
<xs:annotation>
<xs:documentation>Popis přiloženého souboru</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="jm_souboru" use="optional">
<xs:annotation>
<xs:documentation>
<strong>Název přiloženého souboru.</strong><br/>povolené typy souborů jsou: DOC, DOCX, RTF, XLS, XLSX, PDF, JPG, TXT a TXT/CSV. Dále je možné přiložit podepsané (formát PKCS#7) a komprimované (formát ZIP) soubory, vždy však jde o jeden podepsaný nebo jeden komprimovaný soubor některého z podporovaných formátů. Součet velikostí všech souborů přiložených v elektronické podobě (tzv. e-příloh) může být nejvýše 4 000 kilobajtů.
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="kodovani" use="optional">
<xs:annotation>
<xs:documentation>Kódování přiloženého souboru</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="base64"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element maxOccurs="1" minOccurs="0" name="PredepsanaPriloha">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:base64Binary">
<xs:attribute name="cislo" use="required">
<xs:annotation>
<xs:documentation>Pořadové číslo přílohy</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="4"/>
<xs:fractionDigits value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="nazev" use="optional">
<xs:annotation>
<xs:documentation>Popis přiloženého souboru</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="jm_souboru" use="optional">
<xs:annotation>
<xs:documentation>
<strong>Název přiloženého souboru.</strong><br/>povolené typy souborů jsou: DOC, DOCX, RTF, XLS, XLSX, PDF, JPG, TXT a TXT/CSV. Dále je možné přiložit podepsané (formát PKCS#7) a komprimované (formát ZIP) soubory, vždy však jde o jeden podepsaný nebo jeden komprimovaný soubor některého z podporovaných formátů. Součet velikostí všech souborů přiložených v elektronické podobě (tzv. e-příloh) může být nejvýše 4 000 kilobajtů.
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="kodovani" use="optional">
<xs:annotation>
<xs:documentation>Kódování přiloženého souboru</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="base64"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="kod" use="required">
<xs:annotation>
<xs:documentation>Kód přiloženého souboru</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(PP_OPISPUV){1}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

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 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>

Oxygen is telling me to terminate complexType despite being terminated

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.

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