empty namespace

Questions about XML that are not covered by the other forums should go here.
eHannes
Posts: 31
Joined: Sat Apr 16, 2005 11:29 am

empty namespace

Post by eHannes »

Hi,

Hope you can help me this...
I have the following XML:

Code: Select all

<?xml version="1.0"?>
<result>
<A ID="oai:ARNO:148242" DT="2005-03-02"/>
<A ID="oai:ARNO:148374" DT="2005-03-04"/>
</result>
and the following XSL:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<feed xmlns="http://www.w3.org/2005/Atom">
<xsl:apply-templates/>
</feed>
</xsl:template>
<!-- -->
<xsl:template match="result">
<xsl:apply-templates/>
</xsl:template>
<!-- -->
<xsl:template match="A">
<entry>
<xsl:apply-templates/>
</entry>
</xsl:template>
<!-- -->
<xsl:template match="* | @*"/>
<!-- -->
</xsl:stylesheet>

the output looks like:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<entry xmlns=""/>
<entry xmlns=""/>
</feed>
my questions:
why do i have the empty xmlns attribute?
and
how can i prevent this?

Thanks, Hans Scholte
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Hans,

You generate as output a feed element in the http://www.w3.org/2005/Atom namespace and inside thie element you generate entry elements from no namespace. In order to represent that as XML, if a default namesapce declaration is used to represent feed then the default namespace must be reset to no namepsace to represent entry correctly. If you want your entry elements also in the http://www.w3.org/2005/Atom namespace then place them in that namespace:

<entry xmlns="http://www.w3.org/2005/Atom">
...

Best Regards,
George
eHannes
Posts: 31
Joined: Sat Apr 16, 2005 11:29 am

Post by eHannes »

Hi George

I can follow you there but, when a output a title elements in the xslt (one in <feed> and one in <entry> then these do not have a namespace attribute. Like:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>this doesn't have the namespace attribute</title>
<entry xmlns="">
<title>this one neither</title>
</entry>
</feed>
Why is that?

Thank, Hans
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Hans,

The namespaces are inherited from parent and inside entry no prefix means no namespace and if you output a title element from no namespace there is no need to declare again the default namespace as no namespace as the default namespace is already no namespace being delcared so in entry with the xmlns="" namespace declaration.

Best Regards,
George
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung »

An element automatically falls under the same namespace as it's parent unless stated otherwise by declaring a different namespace.
Additionally, if you wanted all elements in your output to have the same namespace you could move the xmlns attribute to your xsl:stylesheet element, eg.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/2005/Atom">
Post Reply