XML Validation and Transformation problems

Associate
Joined
11 Mar 2003
Posts
1,328
Location
North Shields
Hello all,

I'm using .Net 2.0 to validate an XML document using a .XSD file, which works fine, but afterwards when I try to transform the same xml document to something else, it doesn't work, unless I remove the schema validation information from the document node in the XML file. But obviously I need this schema information in there so it will validate correctly - so what can I do?

The funny thing is, during transformation the output document (in this case an html file) is created and all the html elements rendered, but none of the xsl functions work and none of the actual data is printed.

Is there something I've missed?

The documents i'm using are below:

test.xml:
Code:
<?xml version="1.0" encoding="iso-8859-1" ?>
<racing_series 
	xmlns="http://www.elkdanger.co.uk"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.elkdanger.co.uk test.xsd">

	<drivers>
		<driver retired="true">
			<name>Example Driver</name>
			<age>25</age>
			<team>McLaren</team>
		</driver>
	</drivers>

	<teams>
		<team>
			<name>McLaren</name>
			<engine>Mercedes</engine>
			<tyres>Bridgestone</tyres>
			<titleSponser>Vodafone</titleSponser>
		</team>
	</teams>

</racing_series>

test.xsd (not sure it's relevant but here for completeness):
Code:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.elkdanger.co.uk" xmlns:NS="http://www.elkdanger.co.uk" targetNamespace="http://www.elkdanger.co.uk" elementFormDefault="qualified">
	<xs:element name="racing_series">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="drivers" minOccurs="1" maxOccurs="1">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="driver" minOccurs="0" maxOccurs="unbounded">
								<xs:complexType>
									<xs:sequence>
										<xs:element name="name" type="xs:string" />
										<xs:element name="age" type="xs:integer" />
										<xs:element name="team" type="xs:string" />
									</xs:sequence>
									<xs:attribute name="retired" default="false" type="xs:boolean" />
								</xs:complexType>
							</xs:element>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:element name="teams" minOccurs="1" maxOccurs="1">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="team" minOccurs="0" maxOccurs="unbounded">
								<xs:complexType>
									<xs:sequence>
										<xs:element name="name" type="xs:string" />
										<xs:element name="engine" type="xs:string" />
										<xs:element name="tyres" type="xs:string" />
										<xs:element name="titleSponser" type="xs:string" />
									</xs:sequence>
								</xs:complexType>
							</xs:element>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

test.xsl
Code:
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema">

	<xsl:template match="/">
		<html>
			<head/>
			<body>
				<h2>Drivers:</h2>
				<xsl:for-each select="racing_series/drivers/driver">
					<p>Hello!</p>					
				</xsl:for-each>				
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Obviously I don't expect you to trawl through the lot, it's just there for reference. The idea is that with one 'driver' element in the xml file, the resulting html file after transformation should print 'Hello', but it doesn't. However, if I remove the schema properties in the 'racing_series' element in the xml file, the document doesn't validate but the transformation works.

There must be a way to validate AND transform the same xml document, surely?

Can anyone help? Thanks in advance.
 
No-one?

Never mind, I've found my own answer. In case anyone is comes across the same problem, it was to do with default namespaces. I've declared a default namespace in my xml document but I hadn't declared it in my XSLT file.

This article helps explain it, linked from this ASP.Net post.
 
Back
Top Bottom