Jump to content
MrAdam

Stuck with XSLT schema validation

Recommended Posts

Hi guys,Managed to get some-what comfortable with XSLT now, but looking to validate my XML against my schema. I'm working with the following, pretty-simple XML:

<products>	<product id="1">		<title>Product A</title>		<stock>			<available>123</available>			<ordered>123</ordered>			<delivery>01/01/1970</delivery>		</stock>		<stats>			<total_sold>123</total_sold>			<per_order_average>123.45</per_order_average>			<all_order_average>123.45</all_order_average>		</stats>	</product></products>

My schema is:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="products">	<xs:complexType>		<xs:element name="product">			<xs:attribute name="id" type="xs:integer" />			<xs:complexType>				<xs:sequence>					<xs:element name="title" type="xs:string" />					<xs:element name="stock" />						<xs:complexType>							<xs:sequence>								<xs:element name="available" type="xs:integer" />								<xs:element name="ordered" type="xs:integer" />								<xs:element name="delivery" type="xs:date" />							</xs:sequence>						</xs:complexType>					</xs:element>					<xs:element name="stats" />						<xs:complexType>							<xs:sequence>								<xs:element name="total_sold" type="xs:integer" />								<xs:element name="per_order_average" type="xs:decimal" />								<xs:element name="all_order_average" type="xs:decimal" />							</xs:sequence>						</xs:complexType>					</xs:element>				</xs:sequence>			</xs:complexType>		</xs:element>	</xs:complexType></xs:element>

I'm getting a lot of errors though. Am I on the right track? One area I can't seem to find a clear explanation of is attributes, with a complex type within them.In my XML above I have:

<product id="1">	[...]</product>

Which I'm using the following schema to validate:

		<xs:element name="product">			<xs:attribute name="id" type="xs:integer" />			<xs:complexType>				[...]			</xs:complexType>		</xs:element>

But I don't think that's correct. Any pointers or advise would be greatly appreciated!ThanksAdam

Share this post


Link to post
Share on other sites

Sorry I posted a little prematurely there. I have managed to fix some of the issues, but I still get one:

lement '{http://www.w3.org/2001/XMLSchema}complexType': The content is not valid. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))

That's using PHP's DOM schema validator. I used an on-line validator and the error is:

4s-elt-invalid-content.1: The content of '#AnonType_products' is invalid. Element 'element' is invalid, misplaced, or occurs too often.

Which refers to the line:

			<xs:element name="product" minOccurs="0" maxOccurs="unbounded">

The XSD in-full is now:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">	<xs:element name="products">		<xs:complexType>			<xs:element name="product" minOccurs="0" maxOccurs="unbounded">				<xs:complexType>					<xs:attribute name="id" type="xs:integer" />					<xs:sequence>						<xs:element name="title" type="xs:string" />						<xs:element name="stock">							<xs:complexType>								<xs:sequence>									<xs:element name="available" type="xs:integer" />									<xs:element name="ordered" type="xs:integer" />									<xs:element name="delivery" type="xs:date" />								</xs:sequence>							</xs:complexType>						</xs:element>						<xs:element name="stats">							<xs:complexType>								<xs:sequence>									<xs:element name="total_sold" type="xs:integer" />									<xs:element name="per_order_average" type="xs:decimal" minOccurs="0" />									<xs:element name="all_order_average" type="xs:decimal" minOccurs="0" />								</xs:sequence>							</xs:complexType>						</xs:element>					</xs:sequence>				</xs:complexType>			</xs:element>		</xs:complexType>	</xs:element></xs:schema>

Again.. very grateful for any help. Just need a push in the right direction..Thanks

Share this post


Link to post
Share on other sites

Can you try this. I have placed the attribute "id" inside the xs:complexType/xs:sequence. What is the error message you got? <xs:element name="product"> <xs:complexType> <xs:sequence> <xs:attribute name="id" type="xs:integer" /> <xs:element name="title" type="xs:string" /> <xs:element name="stock" /> <xs:complexType> <xs:sequence> <xs:element name="available" type="xs:integer" /> <xs:element name="ordered" type="xs:integer" /> <xs:element name="delivery" type="xs:date" /> </xs:sequence> </xs:complexType>Regards,Arunawww.technologyandleadership.com"The intersection of Technology and Leadership"

Hi guys,Managed to get some-what comfortable with XSLT now, but looking to validate my XML against my schema. I'm working with the following, pretty-simple XML:
<products>	<product id="1">		<title>Product A</title>		<stock>			<available>123</available>			<ordered>123</ordered>			<delivery>01/01/1970</delivery>		</stock>		<stats>			<total_sold>123</total_sold>			<per_order_average>123.45</per_order_average>			<all_order_average>123.45</all_order_average>		</stats>	</product></products>

My schema is:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="products">	<xs:complexType>		<xs:element name="product">			<xs:attribute name="id" type="xs:integer" />			<xs:complexType>				<xs:sequence>					<xs:element name="title" type="xs:string" />					<xs:element name="stock" />						<xs:complexType>							<xs:sequence>								<xs:element name="available" type="xs:integer" />								<xs:element name="ordered" type="xs:integer" />								<xs:element name="delivery" type="xs:date" />							</xs:sequence>						</xs:complexType>					</xs:element>					<xs:element name="stats" />						<xs:complexType>							<xs:sequence>								<xs:element name="total_sold" type="xs:integer" />								<xs:element name="per_order_average" type="xs:decimal" />								<xs:element name="all_order_average" type="xs:decimal" />							</xs:sequence>						</xs:complexType>					</xs:element>				</xs:sequence>			</xs:complexType>		</xs:element>	</xs:complexType></xs:element>

I'm getting a lot of errors though. Am I on the right track? One area I can't seem to find a clear explanation of is attributes, with a complex type within them.In my XML above I have:

<product id="1">	[...]</product>

Which I'm using the following schema to validate:

		<xs:element name="product">			<xs:attribute name="id" type="xs:integer" />			<xs:complexType>				[...]			</xs:complexType>		</xs:element>

But I don't think that's correct. Any pointers or advise would be greatly appreciated!ThanksAdam

Share this post


Link to post
Share on other sites

I decided to take things back to basics.With this XML:

<?xml version="1.0"?><products>	<product>		<title>Product A</title>	</product></products>

And this XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">	<xs:element name="products">		<xs:complexType>			<xs:element name="title" type="xs:string" />		</xs:complexType>	</xs:element></xs:schema>

I receive the following error:

s4s-elt-invalid-content.1: The content of '#AnonType_products' is invalid. Element 'element' is invalid, misplaced, or occurs too often
Referring to this line:
			<xs:element name="title" type="xs:string" />

I feel like I'm missing something fundamental out? :sThanks

Share this post


Link to post
Share on other sites

Realised a few obvious flaws with the previous XSD. Changed it to:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">	<xs:element name="products">		<xs:sequence>			<xs:element name="product">				<xs:sequence>					<xs:complexType>						<xs:element name="title" type="xs:string" />					</xs:complexType>				</xs:sequence>			</xs:element>		</xs:sequence>	</xs:element></xs:schema>

But get the error:

4s-elt-must-match.1: The content of 'products' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: sequence.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×