p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

xslt thread: xmlns question?

Message #1 by Tero Ahola <TAH@s...> on Thu, 19 Sep 2002 15:51:18 +0200
The solution isn't always easy, but in your case there are two easy

(1) Literal result elements are your friend. Instead of <xsl:element
name="schultz">, write

<schultz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

When a literal result element is processed, all its namespace nodes are
copied to the result tree.

(2) When you write an attribute whose name is in a particular namespace,
the namespace node for that namespace is automatically added to the
result tree. So just write:

<xsl:attribute name="xsi:schemaLocation" 

You are right to observe that you can't create namespace declarations
using <xsl:attribute>. That's because in the XPath data model,
attributes nodes and namespace nodes are quite different things, even
though they are both serialized as XML attributes.

Michael Kay
Software AG
home: Michael.H.Kay@n...
work: Michael.Kay@s... 

> -----Original Message-----
> From: Tero Ahola [mailto:TAH@s...] 
> Sent: 19 September 2002 14:51
> To: P2P_XSLT
> Subject: [xslt] xmlns question?
> Hi!
> If I would like to create a namespace in a xml output 
> document.  I tried to create an attribute called "xmlns:xsi " 
> but this wasn't allowed with the xslt parser I used. Is there 
> a way which I can create an namespace in a output-file. My 
> result I would like to have as the following little example. 
> I have also included the code snippet of the stylesheet I 
> tried to use. OUTPUT DOKUMENT <?xml version="1.0" 
> encoding="ISO-8859-1"?> <schultz 
> xmlns="http://xmlns.schultz.dk/dokument"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://xmlns.schultz.dk/dokument
> http://schultzora.schultz.dk/schemas/schultz.xsd">
> ......
> </schultz>
> Regards Tero
> <?xml version="1.0" encoding="iso-8859-1" ?> 
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:msxsl="urn:schemas-microsoft-com:xslt"
> xmlns:user="http://mycompany.com/mynamespace" version="1.0">  
> <xsl:output method="xml" encoding="iso-8859-1" 
> standalone="no" doctype-public="-//Schultz Information//DTD 
> SCHULTZ//EN" omit-xml-declaration="no" /> 
>   <xsl:template match="/">
>   <xsl:apply-templates /> 
>   </xsl:template>
> <xsl:template match="schultzdoc">
>   <xsl:element name="schultz">
>    <xsl:attribute name="xmlns">
>     <xsl:text>http://xmlns.schultz.dk/dokument</xsl:text> 
>    </xsl:attribute>
>    <xsl:attribute name="xmlns:xsi">
>     <xsl:text>http://www.w3.org/2001/XMLSchema-instance</xsl:text> 
>   </xsl:attribute>
>   <xsl:attribute name="xsi:schemaLocation">
>     <xsl:text>http://xmlns.schultz.dk/dokument
> http://schultzora.schultz.dk/schemas/schultz.xsd</xsl:text> 
>   </xsl:attribute>
> </xsl:element>
> <xsl:apply-templates/>>  
> </xsl:template>
> ...........
> ---
> In developing the .NET Framework Microsoft has 
> taken XML to heart, and with ASP.NET representing 
> the cutting edge of Windows based web development, 
> Professional ASP.NET XML with C# has been published 
> to help web developers to appreciate how these 
> technologies can work together to provide 
> functional, powerful applications. 
> http://www.wrox.com/acon11.asp?ISBN=1861007345

  Return to Index