I'm having trouble validating a schema I created.
"cvc-elt.1: Cannot find the declaration of element 'category'."
xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="list">
<xs:complexType>
<xs:sequence>
<xs:element name="category" type="categoryType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="categoryType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="desc" type="xs:string"/>
<xs:element name="icon" type="xs:base64Binary"/>
<xs:element name="poi" type="poiType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="poiType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="desc" type="xs:string"/>
<xs:element name="longitude" type="xs:long"/>
<xs:element name="latitude" type="xs:long"/>
<xs:element name="url" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="image" type="xs:base64Binary" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
xml
<?xml version="1.0" encoding="UTF-8"?>
<list SchemaLocation="sem.xsd">
<category>
<name>Sehenswürdigkeiten</name>
<desc>sehenswerte und berühmte Orte, die man gesehen haben muss</desc>
<icon>...</icon>
<poi>
<name>Linzer Landhaus</name>
<desc>Sitz des Oberösterreichsichen Landtags</desc>
<url>http://www.linz.at/tourismus/7569.asp</url>
<longitude>48.304107</longitude>
<latitude>14.286025</latitude>
<image>...</image>
</poi>
<poi>
<name>Ars Electronica</name>
<desc>Museum der digitalen Künste</desc>
<url>http://www.aec.at</url>
<longitude>48.309788</longitude>
<latitude>14.284179</latitude>
<image>...</image>
<image>...</image>
</poi>
</category>
<category>...</category>
</list>
any idea whats wrong?
cheers hoax
The problem is probably with the line:
<list SchemaLocation="sem.xsd">
You haven't indicated that SchemaLocation is anything special, it just looks like another attribute.
It should be:
<list xsi:schemaLocation="sem.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
See here for a more detailed explanation.
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sem.xsd">
does the trick =)
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 do the validations like:
<A>
<B> or <C>
<D>
</A>
In the , the first element should be one of the B and C. The second element is D. And at the same time, the first element B or C and the second element do not in sequence. It could become and . Anybody know how to do it?
To match your example, please try:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="B" type="xs:string"/>
<xs:element name="C" type="xs:string"/>
</xs:choice>
<xs:element name="D" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You have to use two things here, the first one is <xs:choice> which allows only one elements to be present in xml.
In order to make an element optional (D) you have to specify minOccurs="0".
Edited (after feedback): All valid cases are covered with this XSD.
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="B" type="xs:string"/>
<xs:element name="C" type="xs:string"/>
<xs:element name="D" type="xs:string"/>
<xs:complexType name="OurType">
<xs:choice>
<xs:sequence>
<xs:element ref="D"/>
<xs:choice>
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:choice>
</xs:sequence>
<xs:sequence>
<xs:choice>
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:choice>
<xs:element ref="D"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:element name="A" type="OurType"/>
</xs:schema>
I get the best practice. It should be as below:
<xs:element name="A">
<xs:complexType>
<xs:all>
<xs:element ref="Choice" minOccurs="1" maxOccurs="1"/>
<xs:element ref="D" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="D" type="xs:string"/>
<xs:element name="Choice" abstract="true"/>
<xs:element name="B" substitutionGroup="Choice">
</xs:element>
<xs:element name="C" substitutionGroup="Choice">
</xs:element>
i want to create a XSD.
One optional element in the XSD is <PrintoutSettings OrderSource="NameOfOrder_X" .../>
Another substructure of the XSD is
<Order Name="NameOfOrder">...</Order>
<Order Name="NameOfOrder2">...</Order>
...
My aim is it, that the attribute NameOfOrder_X of <PrintoutSettings .../> must be a string that was defined in one of the <Order>...</Order> - elements.
How can i realize it in a XSD?
Kind regards
sb
---
---
Here some of my XSD:
<xs:complexType name="Order">
<xs:sequence>
<xs:element form="qualified" minOccurs="0" name="Documents" type="Documents"/>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="Translator" type="xs:string" use="required"/>
<xs:attribute name="Proofreader" type="xs:string" use="required"/>
<xs:attribute name="LockedBy" type="xs:string" use="optional"/>
</xs:complexType>
<xs:complexType name="Orders">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Order" type="Order"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="QQDBData">
<xs:sequence>
<xs:element minOccurs="0" name="Orders" type="Orders"/>
<xs:element minOccurs="0" name="LocalSettings" type="Settings"/>
</xs:sequence>
<xs:attribute default="2000-01-01" name="CreationDate" type="xs:date" use="optional"/>
</xs:complexType>
<xs:element name="QQDBData" type="QQDBData">
<xs:key name="PK-Orders">
<xs:selector xpath="Order"/>
<xs:field xpath="#Name"/>
</xs:key>
<xs:keyref name="FK-PrintoutSettings" refer="PK-Orders">
<xs:selector xpath="PrintoutSettings"/>
<xs:field xpath="#OrderSource"/>
</xs:keyref>
</xs:element>
The answer you've got from #DevNull is technically correct (+1); it is, however, not so much XSD; have a look at this section from the schema primer to understand the limitations you have with ID/IDREF.
I'll take the schema put forward by DevNull and modify it to show you the recommended XSD approach that relies on key/keyref.
UPDATE: I took your schema and updated to make sure it is sufficient for my tests:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:complexType name="Order">
<xs:sequence>
<xs:element form="qualified" minOccurs="0" name="Documents" type="Documents"/>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="Translator" type="xs:string" use="required"/>
<xs:attribute name="Proofreader" type="xs:string" use="required"/>
<xs:attribute name="LockedBy" type="xs:string" use="optional"/>
</xs:complexType>
<xs:complexType name="Documents"/>
<xs:complexType name="Orders">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Order" type="Order"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="QQDBData">
<xs:sequence>
<xs:element minOccurs="0" name="Orders" type="Orders"/>
<xs:element minOccurs="0" name="LocalSettings" type="Settings"/>
</xs:sequence>
<xs:attribute default="2000-01-01" name="CreationDate" type="xs:date" use="optional"/>
</xs:complexType>
<xs:complexType name="Settings">
<xs:attribute name="OrderSource" use="required" type="xs:string"/>
</xs:complexType>
<xs:element name="QQDBData" type="QQDBData">
<xs:key name="PK-Orders">
<xs:selector xpath="Orders/Order"/>
<xs:field xpath="#Name"/>
</xs:key>
<xs:keyref name="FK-PrintoutSettings" refer="PK-Orders">
<xs:selector xpath="LocalSettings"/>
<xs:field xpath="#OrderSource"/>
</xs:keyref>
</xs:element>
</xs:schema>
What I had to do is to update the selectors, since by introducing additional tags, the XPath has changed. I always try to visualize the constraints, to ensure that the XPaths as described still make sense.
An updated sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<QQDBData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" CreationDate="2000-01-01">
<Orders>
<Order Name="Name1" Translator="Translator1" Proofreader="Proofreader1" LockedBy="LockedBy1">
<Documents/>
</Order>
<Order Name="Name1" Translator="Translator1" Proofreader="Proofreader1" LockedBy="LockedBy1">
<Documents/>
</Order>
</Orders>
<LocalSettings OrderSource="OrderSource1"/>
</QQDBData>
An error message, for the same invalid XML may look like (I am using QTAssistant to validate):
Error occurred while loading [], line 12 position 3
The key sequence 'OrderSource1' in Keyref fails to refer to some key.
You can use an attribute type of xs:ID for <Order> and an attribute type of xs:IDREF for <PrintoutSettings>.
Example XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="doc">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Order"/>
<xs:element ref="PrintoutSettings"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Order">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:NMTOKEN">
<xs:attribute name="Name" use="required" type="xs:ID"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="PrintoutSettings">
<xs:complexType>
<xs:attribute name="OrderSource" use="required" type="xs:IDREF"/>
</xs:complexType>
</xs:element>
</xs:schema>
Example of INVALID XML: (Gives the error "There is no ID/IDREF binding for IDREF 'NameOfOrder_X'." in Xerces.)
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book.xsd">
<Order Name="NameOfOrder">...</Order>
<Order Name="NameOfOrder2">...</Order>
<PrintoutSettings OrderSource="NameOfOrder_X"/>
</doc>
Example of VALID XML:
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book.xsd">
<Order Name="NameOfOrder">...</Order>
<Order Name="NameOfOrder2">...</Order>
<PrintoutSettings OrderSource="NameOfOrder2"/>
</doc>
Below is the sample xml which has multiple <rulex> that starts with sequence 1 and it can end up to many rule like <rule1> , <rule2>, <rule3> etc....
<?xml version="1.0" encoding="UTF-8"?>
<AddressChange_181>
<rules>
<rule1>
<conditions>xya</conditions>
<response_path>abc</response_path>
</rule1>
<rule2>
<conditions>xxxx</conditions>
<response_path>aaaa</response_path>
</rule2>
<rule3>
<conditions>yyyyy</conditions>
<response_path>ffff</response_path>
</rule3>
<rule4>
<conditions>zzzz</conditions>
<response_path>yyyy</response_path>
</rule4>
<default>
<response_path>uuuuu</response_path>
</default>
</rules>
</AddressChange_181>
Below is the schema where i tried creating dynamic <rulex> element name for the above xml.
when i generate xml from this schema i do not get the same xml format as above xml.
can you please let me know how to create schema with multiple element name that starts with a sequence number.
My requirement is to add more than one rule (<rule1>,<rule2>,<rule3> etc...) in xml file and this xml file should be validated against schema.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="default" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rule">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:anyType">
<xs:pattern value="rule/d{1,3}"></xs:pattern>
</xs:restriction>
</xs:simpleContent>
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
</xs:schema>
Thanks,
Madhu
There is no way to define such a structure using XSD if the number of ruleX tags is arbitrarily defined. If you can constrain the upper bound to a maximum, and you really have to stick with ruleX naming convention, than you can define a complex type such as ruleType, then a bunch of rule1, rule2,... , ruleN elements of that type - I would call this messy... I wouldn't recommend this.
XSD (with a maximum of 6):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule1" minOccurs="0"/>
<xs:element ref="rule2" minOccurs="0"/>
<xs:element ref="rule3" minOccurs="0"/>
<xs:element ref="rule4" minOccurs="0"/>
<xs:element ref="rule5" minOccurs="0"/>
<xs:element ref="rule6" minOccurs="0"/>
<xs:element ref="default"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="ruleType">
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
<xs:element name="rule1" type="ruleType"/>
<xs:element name="rule2" type="ruleType"/>
<xs:element name="rule3" type="ruleType"/>
<xs:element name="rule4" type="ruleType"/>
<xs:element name="rule5" type="ruleType"/>
<xs:element name="rule6" type="ruleType"/>
</xs:schema>
Alternatively, you could have a tag named "rule" with a #sequence attribute.
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="default"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="SequenceKey">
<xs:selector xpath="rule"/>
<xs:field xpath="#sequence"/>
</xs:unique>
</xs:element>
<xs:complexType name="ruleType">
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="sequence" type="xs:int" use="required"/>
</xs:complexType>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
<xs:element name="rule" type="ruleType"/>
</xs:schema>
Sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<rules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rule sequence="1">
<conditions>conditions1</conditions>
<response_path>response_path1</response_path>
</rule>
<rule sequence="2">
<conditions>conditions2</conditions>
<response_path>response_path2</response_path>
</rule>
<default>
<response_path>response_path1</response_path>
</default>
</rules>
Or it could also be that one could simply use the index of the <rule/> within the parent collection .
I would like to validate this XML:
<meta>
<house>
<big ... />
<little ... />
<big ... />
</house>
<flat>
<red ... />
<red ... />
<yellow ... />
</flat>
</meta>
I wrote that.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="meta">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="house">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='big' />
<xs:element name='little' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="flat">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='red'/>
<xs:element name='yellow'/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But that does not validate my example.
Without 'house' or 'flat', and only meta, that worked.
Where could be my problem ?
Found !
The solution: add a "xs:choice" for each "xs:sequence", like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="meta">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name="house">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name='big' />
<xs:element name='little' />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="flat">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name='red'/>
<xs:element name='yellow'/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>