By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

default namespace and xmlns=""

P: n/a
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:output 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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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:output 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:output 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

"Mike Dickens" <mi**@progress-partnership.co.uk> wrote in message
news:72*************************@posting.google.co m... 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:output 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

Jul 20 '05 #2

P: n/a


<?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
Jul 20 '05 #3

P: n/a
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...

"Marrow" <ma****@somewhere.so.fu> wrote in message news:<8S*************@newsfe2-gui.ntli.net>...
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:output 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:output 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

"Mike Dickens" <mi**@progress-partnership.co.uk> wrote in message
news:72*************************@posting.google.co m...
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:output 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

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.