Re: Styling xml values into xml namespace tags

Subject: Re: Styling xml values into xml namespace tags
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 8 Nov 2000 13:48:49 +0000

> I'd like to know if, when styling an xml into another xml format, it is
> possible to use a value from the first to generate a node in the second
> in the format of a valid namespace (ie. defined at the top of the xml).
> For example if I want to use the type attribute as part of the namespace
> in the output xml (assuming it was referenced in the second xml as a
> valid namespace ie xmlns:Book="";>):
> If the first xml looked similar to.
>  <entry Id="111766" type="Book">
>   <title>The Hobbit</title>
>   <description>Fantasy</description>
>   </entry>
> Could it be output as:
> <label:Book>
>     <name>The Hobbit</name>
>     <description>Fantasy</description>
> </label:Book>

I'm a bit confused by your question because in the text you're talking
about creating an element in a particular *namespace* while in the
example you show creating an element called 'Book' in the 'label'
namespace (the bit before the ':' is the namespace prefix, so you need
a xmlns:label declaration somewhere for it to make sense; the bit
after the ':' is the name of the element).

In fact, you can control both the name and the namespace of the
generated dynamically with the xsl:element.  Setting the namespace
depends on whether you know the prefix for the namespace (and have
declared it within your stylesheet) or the URI for the namespace.  If
the former, then you need something like:

<xsl:element name="{$prefix}:{$name}" />

If the latter, then you need something like:

<xsl:element name="{$name}" namespace="{$uri}" />

The latter is better because XSLT processors are not obliged to use
the prefix that you specify within the element name to determine the
namespace of the generated element.

To get the output that you've given from the example above, for

<xsl:template match="entry">
  <xsl:element name="label:{@type}">
    <name><xsl:value-of select="title" /></name>
    <description><xsl:value-of select="description" /></description>

If you meant that the 'Book' (the @type) should be the namespace
prefix used, then just swap the 'label' and the '{@type}' in the

  <xsl:element name="{@type}:label">

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread