default namespace and xmlns=""

M

Mike Dickens

hi,
i'm sure this has come up before but havn't managed to find an answer.
if i have the following xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet method="xml" version="1.0" xmlns:ns1="abc"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput indent="yes" media-type="text/xml" standalone="yes"
version="1.0"/>
<xsl:template match="/">
<x>
<xsl:element name="y" namespace="abc">
<xsl:apply-templates select="/a"/>
</xsl:element>
</x>
</xsl:template>
<xsl:template match="/a">
<z>
<xsl:value-of select="b"/>
</z>
</xsl:template>
</xsl:stylesheet>

and apply it to the following xml

<a>
<b>hello</b>
</a>

i get

<?xml version="1.0" standalone="yes"?>
<x xmlns:ns1="abc">
<y xmlns="abc">
<z xmlns="">hello</z>
</y>
</x>

my question is this. is it possible to remove the xmlns="" bit without
having to use <ns1:z> in the xslt?

thanks,
mike
 
M

Marrow

Hi Mike,
my question is this. is it possible to remove the xmlns="" bit without
having to use <ns1:z> in the xslt?

It is not possible to 'remove' namespace declarations placed in the output
by the transformation engine. The transformation engine decides, to some
degree under your instruction, what namespace declarations are needed.
In this case your <z> element is bound to the null namespace (i.e. the
namespace URI is "") - the only way to 'removing' that namespace declaration
would be to bind the <z> element to a different namespace... which I
suspect, but am not sure, is what you are asking... how to bind <z> to the
"abc" namespace?

One way to start taking control of your output namespace is to define the
default output namespace in your stylesheet, e.g.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="abc"
xmlns="abc">

The default namespace for the stylesheet is used as the default namespace
for the output.

Having done that, then you only need to specify namespace prefixes (or
@namespace-uri on <xsl:element>/<xsl:attribute>) where you want
elements/attributes to be bound to a different namespace to the default
output namespace.

Another thing that can help you 'control' (to a degree) the output namespace
declarations is to use the <xsl:namespace-alias> instruction, for example to
declare your prefix 'ns1' (e.g. xmlns:ns1="abc") in the stylesheet and then
have that transposed to the default output namespace (e.g. xmlns="abc")
would be to specify that...

<xsl:namespace-alias stylesheet-prefix="ns1" result-prefix="#default"/>

So putting those together, your stylesheet might look like...

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="abc"
xmlns="abc">

<xsl:namespace-alias stylesheet-prefix="ns1" result-prefix="#default"/>

<xsl:eek:utput indent="yes" media-type="text/xml" standalone="yes"
version="1.0"/>
<xsl:template match="/">
<x>
<ns1:y>
<xsl:apply-templates select="/a"/>
</ns1:y>
</x>
</xsl:template>

<xsl:template match="/a">
<z>
<xsl:value-of select="b"/>
</z>
</xsl:template>
</xsl:stylesheet>

which would give an output of...

<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<x xmlns="abc">
<y>
<z>hello</z>
</y>
</x>

Although then there would be no particular reason to declare the 'ns1'
namespace prefix at all nor specify the @namespace-uri attribute on
<xsl:element> (come to that, there is no particular reason to use
<xsl:element> in this case at all). So your stylesheet really just needs to
be...

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="abc">

<xsl:eek:utput method="xml" indent="yes"/>
<xsl:template match="/">
<x>
<y>
<xsl:apply-templates select="/a"/>
</y>
</x>
</xsl:template>

<xsl:template match="/a">
<z>
<xsl:value-of select="b"/>
</z>
</xsl:template>
</xsl:stylesheet>

NB. Really, the only times when you need to use <xsl:element> is where the
element name or namespace URI is being defined dynamically, i.e. where
either the @name or @namespace-uri attributes contain an AVT (attribute
value template).

HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
 
D

David Carlisle

<?xml version="1.0" standalone="yes"?>
<x xmlns:ns1="abc">
<y xmlns="abc">
<z xmlns="">hello</z>
</y>
</x>

my question is this. is it possible to remove the xmlns="" bit without
having to use <ns1:z> in the xslt?


You probbaly want to generate z in the namespace abc which you could do
in the xsl by using
<z xmlns="abc">

Usuallly you put such a result namespace declaration on your
xsl:stylesheet element then it's in scope for the whole stylesheet and
would mean you could replace
<xsl:element name="y" namespace="abc">
by
<y>

and just use <z> to generate z.

If you really want x to be in no-namespace you could do
<xsl:element name="x" namespace="">

David
 
M

Mike Dickens

yes, that's what i meant, though possible put as have <z> inherit the
namespace of it's parent rather bind to "abc" specifically. this is
because there are potentially lots of z's...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top