RE: [xsl] Namespace match error. XSL to convert attribute-centric to element-centric.

Subject: RE: [xsl] Namespace match error. XSL to convert attribute-centric to element-centric.
From: "Kerry, Richard" <richard.kerry@xxxxxxxxxxx>
Date: Fri, 16 Nov 2007 17:41:21 -0000
Thanks first to Andrew and Michael for responding.

In the interest of completion, here's the stylesheet I ended up using.
The root node's attributes are copied as attributes and not converted to
elements.

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    exclude-result-prefixes="xs"
    version="2.0">

<xsl:template match="/*">
  <xsl:copy>
	<xsl:copy-of select="@*" />
	<xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*" >
  <xsl:element name="{name()}">
	<xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

<xsl:template match="*" >
  <xsl:element name="{name()}">
	<xsl:apply-templates select="@*" />
	<xsl:apply-templates/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

Yes it seems obvious now that converting from attributes to elements
will stop the document from complying with the original schema, so
copying that to the result is not really sensible.  However my
stylesheet does copy the root node's attributes as attributes and I
remove the schema manually later.

Regards,
Richard.

PS
The result is as follows (with indentation added manually afterwards):

<?xml version="1.0" encoding="UTF-8"?>
<slots xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="generic_snmp_slots.xsd">
  <slot>
    <name>RevMajor</name>
    <use>yes</use>
    <description>The Major Revision level.</description>
  </slot>
</slots>


> -----Original Message-----
> From: Kerry, Richard
> Sent: 15 November 2007 09:59
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Namespace match error. XSL to convert
> attribute-centric to element-centric.
>
>
>
> I'm trying to convert an xml document from attribute-centric to
> element-centric using the following stylesheet.
> (This is ConvertToElements2.xsl referred to below)
>
> <xsl:stylesheet
> 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> 	xmlns:xs="http://www.w3.org/2001/XMLSchema";
> 	exclude-result-prefixes="xs"
> 	version="2.0">
>
> <xsl:template match="/">
> 	<xsl:apply-templates/>
> </xsl:template>
>
> <xsl:template match="@*" >
> 	<xsl:element name="{name()}">
> 		<xsl:value-of select="."/>
> 	</xsl:element>
> </xsl:template>
>
> <xsl:template match="*" >
> 	<xsl:element name="{name()}">
> 		<xsl:apply-templates select="@*" />
> 		<xsl:apply-templates/>
> 	</xsl:element>
> </xsl:template>
>
> </xsl:stylesheet>
>
>
> If I apply it to the following data :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <slots xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>  xsi:noNamespaceSchemaLocation="generic_snmp_slots.xsd">
> 	<slot name="RevMajor" use="yes" description="The Major Revision
> level."/>
> </slots>
>
> I get an error as follows (this is using Saxon, on Windows XP):
>
> (some lines of batchfile removed)
>
> C:\Projects\Drivers\Mib>set
> CLASSPATH=3DC:/Downloads/Saxon/saxonb8-9j/saxon8.jar
> C:\Projects\Drivers\Mib>java net.sf.saxon.Transform Slots4.xml
> C:/Projects/Tools_RK/Xsl/ConvertToElements2.xsl
> outfile=3DSlots4_Elements.xml
>
> Error at xsl:element on line 12 of
> file:/C:/Projects/Tools_RK/Xsl/ConvertToElements2.xsl:
> 	XTDE0830: Undeclared prefix in element name: xsi Transformation
> failed: Run-time errors were reported
>
>
>
>
>
> I take it this error is due to the "xsi:" namespace of the
> "xsi:noNamespaceSchemaLocation" attribute of my source data.
> How can I handle this ?
> I thought matching "*" would match everything. Do I need to tell it to
> ignore namespaces ? Or match namespaces somehow ?
> Or is this an XML error (wrong name for the schema attribute perhaps)
> that is thus nothing to do with XSL ?
>
>
>
> Uncertainly,
> Richard.
>
>
>
>
>
> Richard Kerry
> Colledia Control Engineer
> Siemens IT Solutions and Services Ltd
> Room 457 Design Building, BBC Television Centre, Wood Lane,
> London, W12
> 7RJ
> T: +44 (0)20 82259063 F: +44 (0)20 8576 8182 M: +44 (0)7921 244993
> Email: richard.kerry@xxxxxxxxxxx
> <blocked::mailto:richard.kerry@xxxxxxxxxxx>
> Website:  www.siemens.co.uk/it-solutions
> <blocked::outbind://47/www.siemens.co.uk/it-solutions>
>
> This e-mail contains confidential information and is for the exclusive
> use of the addressee/s.  If you are not the addressee, then any
> distribution, copying or use of this e-mail is prohibited. If received
> in error, please advise the sender and delete it immediately.
>  We accept
> no liability for any loss or damage suffered by any person
> arising from
> use of this e-mail.
>
> Siemens IT Solutions and Services Limited
> Registered No: 1203466 England
> Registered Office: Faraday House, Sir William Siemens Square, Frimley,
> Camberley, GU16 8QD

Current Thread