Hello,
I am new in web technologies.
Please, help!
I've created xsd file, using Visual Studio to do validation for xml data
xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"

xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="stations">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="station-entry">
<xs:complexType>
<xs:sequence>
<xs:element name="station-id" type="xs:unsignedByte" />
<xs:sequence>
<xs:element name="entry" type="xs:string" />
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

xsd file has beed created by Visual Studio, using the xml:
<?xml version="1.0" encoding="utf-8" ?>
<stations>
<station-entry>
<station-id>1</station-id>
<entry>"1 AST"</entry>
<entry>"7 ART"</entry>
</station-entry>
<station-entry>
<station-id>2</station-id>
<entry>"1 AMR Plt"</entry>
<entry>"2 ASR"</entry>
</station-entry>
</stations>

Validation return error:

../data/please.xsd:2: element schema: Schemas validity error : Element '{http://www.w3.org/2001/XMLSchema}schema': No matching global declaration available for the validation root.


Any idea what is wrong with xsd? xml?

Thanks,
Angela.

Dani AI

Generated

Good catch by on the cardinality. One more note on the error you saw: "No matching global declaration available for the validation root" often means the validator is trying to validate the XSD itself as if it were the XML instance, or your XML is not associated with the schema. Make sure you validate the XML document, not the .xsd. Easiest is to reference the schema from the XML root like this (no namespaces in your schema, so use noNamespaceSchemaLocation):

<stations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="please.xsd">
  ...
</stations>

A couple of modeling tweaks that help new XML/XSD projects stay tidy, . The extra nested xs:sequence Visual Studio generated is not harmful, but you can simplify by keeping a single sequence and putting the occurrence constraints directly on entry. Also, xs:unsignedByte caps station-id at 255; if IDs may grow, switch to xs:unsignedShort or xs:nonNegativeInteger. Finally, those quotes inside <entry> are literal characters; if you do not want them stored, remove them from the XML.

If you want stronger validation, add identity constraints so each station has a unique ID and entries are not duplicated within a station. You can drop these into your schema without changing the existing structure:

<!-- under the global 'stations' element -->
<xs:key name="uniqueStationId">
  <xs:selector xpath="station-entry"/>
  <xs:field xpath="station-id"/>
</xs:key>
<!-- under the local 'station-entry' element declaration -->
<xs:unique name="uniqueEntryWithinStation">
  <xs:selector xpath="entry"/>
  <xs:field xpath="."/>
</xs:unique>

These catch common data mistakes early and keep your XML clean as it grows.

Recommended Answers

All 3 Replies

Your document has multiple "entry" elements, but you current schema only allows for 1 and only 1 of them. You have to change the cardinality on the "entry" definition in order to allow for multiples.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="stations">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="station-entry" maxOccurs="unbounded">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="station-id" type="xs:unsignedByte"/>
							<xs:sequence>
								<xs:element minOccurs="1" maxOccurs="unbounded" name="entry" type="xs:string"/>
							</xs:sequence>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Your document validated against this schema for me. Using both Stylus Studio and Altova XML Spy.

Thanks a lot!!!
I am very appreciated!!!!!

Welcome! Problem Solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.