RE: [xsl] schema reference in output

Subject: RE: [xsl] schema reference in output
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 20 Apr 2009 10:11:03 +0100
In the XPath data model, namespaces and attributes are completely different
animals, despite the fact that they use similar syntax in lexical XML. So
you can't create a namespace using the xsl:attribute instruction. You can do
it using the xsl:namespace instruction - but in this case you don't need to.
If you create an attribute whose name is in the xsi namespace then the xsi
namespace node will be created automatically.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Arjen F. de Vries (Gmail) [mailto:afdevries@xxxxxxxxx] 
> Sent: 20 April 2009 09:58
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] schema reference in output
> 
> Dear Listees,
> 
> Im trying to transform XML to XML. In this transformation in 
> trying to extend the output by inserting a schema reference.
> 
> I'm using the SAXONb9-1-0-6n processor and Altova processor 
> to test the XSLT processing. Both give me the following error:
> 
> Error at xsl:attribute on line 23 column 19 of 400_o_pew_company.xslt:
>   XTDE0850: Invalid attribute name: {xmlns:xsi} Error at 
> xsl:attribute on line 24 column 19 of 400_o_pew_company.xslt:
>   XTSE0280: Undeclared namespace prefix {xsi} Failed to 
> compile stylesheet. 2 errors detected.
> 
> I have found the following site but Im not sure how to 
> implement the template option :
> http://stackoverflow.com/questions/424148/xsl-create-xmlnsxsi-
> namespace-and-attribute/424362
> 
> Thanks for your time!
> 
> The source XML file looks like :
> 
> <?xml version = "1.0" encoding="UTF-8"?> <main 
> datecreated="2009-04-17T17:00:02"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:noNamespaceSchemaLocation="400_o_emp_company.xsd">
> <DATA_RECORD>
>   <EMPLOYEENUMBER>012345</EMPLOYEENUMBER>
>   <LASTNAME>Small</LASTNAME>
>   <INITIALS>T</INITIALS>
>   <FIRSTNAME>Toby</FIRSTNAME>
>   <GENDER>M</GENDER>
>   <DATEOFBIRTH>1960-04-22</DATEOFBIRTH>
>  </DATA_RECORD>
>  <DATA_RECORD>
>   <EMPLOYEENUMBER>012346</EMPLOYEENUMBER>
>   <LASTNAME>Bakker</LASTNAME>
>   <INITIALS>H</INITIALS>
>   <FIRSTNAME/>
>   <GENDER>M</GENDER>
>   <DATEOFBIRTH>1951-02-13</DATEOFBIRTH>
>  </DATA_RECORD>
> </main>
> 
> My transformation looks like :
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:xs="http://www.w3.org/2001/XMLSchema";
> xmlns:fn="http://www.w3.org/2005/xpath-functions";>
> 	<xsl:output method="xml" version="1.0" encoding="UTF-8" 
> indent="yes"/>
> 	<xsl:variable name="now" select="current-dateTime()"/>
> 	<xsl:variable name="timestamp" 
> select="format-dateTime($now, '[Y0001][M01][D01][H01][m01]')"/>
> 	<xsl:template match="main">
> 		<xsl:result-document href="400_o_emp_{$timestamp}.xml">
> 			<xsl:element name="Employees">
> 
> 			<!-- schema verwijzingsattributen in main -->
>                         <!-- THIS IS WHERE IM HAVING TROUBLE -->
> 			<xsl:attribute 
> name="xmlns:xsi"><xsl:value-of select 
> ="'http://www.w3.org/2001/XMLSchema-instance'"/></xsl:attribute>
> 			<xsl:attribute 
> name="xsi:noNamespaceSchemaLocation"><xsl:value-of
> select ="'400_o_emp.xsd'"/></xsl:attribute>
> 
> 			<!-- begin van de loop voor alle records -->
> 				<xsl:for-each select="DATA_RECORD">
> 					<xsl:element name="Employee">
> 						<xsl:attribute 
> name="employeeNumber">
> 							
> <xsl:value-of select="EMPLOYEENUMBER"/>
> 						</xsl:attribute>
> 						<xsl:attribute 
> name="lastName">
> 							
> <xsl:value-of select="LASTNAME"/>
> 						</xsl:attribute>
> 						<xsl:attribute 
> name="Initials">
> 							
> <xsl:value-of select="INITIALS"/>
> 						</xsl:attribute>
> 						<xsl:attribute 
> name="firstName">
> 							
> <xsl:value-of select="FIRSTNAME"/>
> 						</xsl:attribute>
> 						<xsl:attribute 
> name="gender">
> 							
> <xsl:value-of select="GENDER"/>
> 						</xsl:attribute>
> 						<xsl:attribute 
> name="dateOfBirth">
> 							<xsl:if 
> test="DATEOFBIRTH">
> 								
> <xsl:if test="DATEOFBIRTH!=''">
> 								
> 	<xsl:call-template name="FormatDate">
> 								
> 		<xsl:with-param name="DateTime" select="DATEOFBIRTH" />
> 								
> 	</xsl:call-template>
> 								
> </xsl:if>
> 							</xsl:if>
> 						</xsl:attribute>
> 					</xsl:element>
> 				</xsl:for-each>
> 			</xsl:element>
> 		</xsl:result-document>
> 	</xsl:template>
> 	
> 	<xsl:template name="FormatDate">
> 		<xsl:param name="DateTime" />
> 		<xsl:variable name="year">
> 			<xsl:value-of 
> select="substring($DateTime,1,4)" />
> 		</xsl:variable>
> 		<xsl:variable name="month">
> 			<xsl:value-of 
> select="substring($DateTime,6,2)" />
> 		</xsl:variable>
> 		<xsl:variable name="day">
> 			<xsl:value-of 
> select="substring($DateTime,9,2)" />
> 		</xsl:variable>
> 		<xsl:value-of select="$day" />
> 		<xsl:text>-</xsl:text>
> 		<xsl:value-of select="$month" />
> 		<xsl:text>-</xsl:text>
> 		<xsl:value-of select="$year" />
> 	</xsl:template>
> </xsl:stylesheet>
> 
> XSD of the output file :
> 
> <?xml version="1.0"?>
> <xs:schema xmlns=""
> xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"; id="main">
> 	<xs:element name="Employees"/>
> 	<xs:complexType name="Employee">
> 		<xs:sequence>
> 			<xs:element name="DATA_RECORD" 
> type="EmployeeType" maxOccurs="unbounded"/>
> 		</xs:sequence>
> 		<xs:attribute name="dateCreated" type="xs:string"/>
> 	</xs:complexType>
> 	<xs:complexType name="EmployeeType">
> 		<xs:attribute name="employeeNumber" type="xs:string"/>
> 		<xs:attribute name="lastName" type="xs:string"/>
> 		<xs:attribute name="Initials" type="xs:string"/>
> 		<xs:attribute name="firstName" type="xs:string"/>
> 		<xs:attribute name="gender" type="xs:string"/>
> 		<xs:attribute name="dateOfBirth" type="xs:string"/>
> 	</xs:complexType>
> </xs:schema>

Current Thread