Hi I have this kind of XML
I would like to write an XML that validates this data:
<?xml version="1.0" encoding="UTF-8"?>
<Entry attribute1="value1" attribute2="Value2">
<subEntry tagX="xValue1" tagy="yValue"/>
</Entry>
but doesn't validate
<?xml version="1.0" encoding="UTF-8"?>
<Entry attribute1="value1" attribute2="Value2">
<subEntry tagX="xValue1" tagy="yValue"/>
<subEntry tagX="xValue1" tagy="yValue"/>
</Entry>
I would like to restrict the number of subEntries:
The maxOccurs of sequence don't check the number of subEntries.
The XSD is :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Entry">
<xs:complexType>
<xs:sequence>
<xs:element ref="subEntry"/>
</xs:sequence>
<xs:attribute1 name="dateEmission" type="xs:dateTime" use="required"/>
<xs:attribute2 name="emetteur" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="Entry">
<xs:complexType>
<xs:attribute name="tagX" type="xs:string" use="required"/>
<xs:attribute name="tagy" type="xs:decimal" use="optional"/>
</xs:complexType>
</xs:element>
</xs:schema>
You need to add maxOccurs on the element declaration. Change it to below
<xs:sequence>
<xs:element ref="subEntry" maxOccurs="1" />
</xs:sequence>
Related
This is my sample XML code:
<Address>
<StreetAddress></StreetAddress>
<OtherDestination />
<City>TORONTO</City>
</Address>
That is currently using this XSD:
<xs:element name="Address" nillable="true">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element ref="StreetAddress" minOccurs="0"/>
<xs:element ref="OtherDestination" minOccurs="0"/>
<xs:element ref="City" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
I want to add an attribute id to Address element like this..
<Address id="first">
<StreetAddress></StreetAddress>
<OtherDestination />
<City>TORONTO</City>
</Address>
How should I change the existing XSD to fulfill my requirement?
An attribute declaration can be added within xs:complexType after the xs:sequence:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="StreetAddress" minOccurs="0" type="xs:string"/>
<xs:element name="OtherDestination" minOccurs="0" type="xs:string"/>
<xs:element name="City" minOccurs="0" type="xs:string"/>
</xs:sequence>
<!------------------------------------------>
<!-- This is where to declare attributes: -->
<xs:attribute name="id" type="xs:string"/>
<!------------------------------------------>
</xs:complexType>
</xs:element>
</xs:schema>
The above XSD will validate your XML successfully.
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>
I can't validate my xml schema here .
I have the following error :
Cannot resolve the name 'familyType' to a(n) 'type definition' component.
Here is my schema:
<?xml version="1.0"?>
<xs:schema version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="families">
<xs:complexType>
<xs:sequence>
<xs:element name="family" type="familyType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="familyType">
<xs:complexType>
<xs:sequence>
<xs:element name="father" type="xs:string"/>
<xs:element name="mother" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
When you specify that the family element is of type familyType, you have to declare a type, not an element, named familyType:
<?xml version="1.0"?>
<xs:schema version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="families">
<xs:complexType>
<xs:sequence>
<xs:element name="family" type="familyType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="familyType">
<xs:sequence>
<xs:element name="father" type="xs:string"/>
<xs:element name="mother" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
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>
I have just made my first attempt at XSD.
When I try and validate my XML against my XSD, I get the error:
Cannot find the declaration of element 'linkage'.
Below I give my XSD and a cut down version of my XML. I've tried adding a namespace qualifier to the top element in my XML and also to every element (changing the XSD to qualified) and it did not help. I'm obviously making a basic mistake. As I'm new to XSD, if you could include what I need to change in my XML and/or XSD I would be very grateful.
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.stephenwaring.me.uk/android/nestedsettings"
xmlns="http://www.stephenwaring.me.uk/android/nestedsettings"
elementFormDefault="unqualified">
<xs:element name="linkage">
<xs:complexType>
<xs:sequence>
<xs:element name="preference-screen" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="parent" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="child" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="key" type="xs:token" use="required" />
<xs:attribute name="reformat" type="xs:boolean" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attributeGroup ref="keyed" />
<xs:attribute name="preference-screen" type="xs:token" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attributeGroup ref="keyed"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attributeGroup ref="defaults"/>
</xs:complexType>
</xs:element>
<xs:attributeGroup name="defaults">
<xs:attribute name="html" type="xs:boolean" />
<xs:attribute name="prefix" type="xs:string" />
<xs:attribute name="suffix" type="xs:string" />
<xs:attribute name="separator" type="xs:string"/>
<xs:attribute name="reformat" type="xs:boolean" />
<xs:attribute name="shaddow" type="xs:boolean" />
<xs:attribute name="child-summary" type="xs:boolean" />
<xs:attribute name="shadow-separator" type="xs:string"/>
</xs:attributeGroup>
<xs:attributeGroup name="keyed">
<xs:attributeGroup ref="defaults" />
<xs:attribute name="key" type="xs:token" use="required" />
</xs:attributeGroup>
</xs:schema>
Cut down XML:
<?xml version="1.0" encoding="UTF-8"?>
<linkage
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://stevewaring.me.uk/android/nestedsettings nestedsettings.xsd"
xmlns="http://www.stevewaring.me.uk">
<preference-screen
key="preferences1">
<parent
key="prefFruit"
preference-screen="preferences2">
<child key="prefFruit1"/>
<child key="prefFruit2"/>
</parent>
</preference-screen>
</linkage>
Your XSD defines elements in http://www.stephenwaring.me.uk/android/nestedsettings whereas your document element is in http://www.stevewaring.me.uk. Make them to agree, one way or the other, and it should take care of the error you're having.
I've added a fixed XML, there's another issue related to use of unqualified elements.
<?xml version="1.0" encoding="UTF-8"?>
<linkage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.stephenwaring.me.uk/android/nestedsettings nestedsettings.xsd" xmlns="http://www.stephenwaring.me.uk/android/nestedsettings">
<preference-screen xmlns="" key="preferences1">
<parent key="prefFruit" preference-screen="preferences2">
<child key="prefFruit1"/>
<child key="prefFruit2"/>
</parent>
</preference-screen>
</linkage>