Project

Profile

Help

Default null namespace (xmlns="") disappeared

Added by Anonymous about 16 years ago

Legacy ID: #5089774 Legacy Poster: Willem van Heerde (wheerde)

I was writing an xslt stylesheet to scan an xml schema and I noticed that during some steps the null namespace disappeared. I have the following simplified stylesheet: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns='http://www.test.com' targetNamespace='http://www.test.com'> <xs:import schemaLocation="other.xsd"/> <xs:element name="top"> <xs:complexType> <xs:sequence> <xs:element ref="name" minOccurs="0"/> <xs:element ref="other" minOccurs="0" xmlns=""/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Note that the xmlns="" on the other element is necessary because the ref attribute contains a qname. If the value does not have a prefix the default namespace is used to find the object it references. When I run the following stylesheet: <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:template mode="copy" match="|@"> <xsl:copy> <xsl:apply-templates mode="copy" select="|@"> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="/:schema/:element"> <original> <xsl:copy-of select="."/> </original> <xsl:variable name="temp"> <xsl:apply-templates mode="copy"/> </xsl:variable> <copy> <xsl:copy-of select="$temp"/> </copy> </xsl:template> </xsl:stylesheet> The second time the null namepsace has gone, see output: <original> <xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com" name="top"> <xs:complexType> <xs:sequence> <xs:element ref="name" minOccurs="0"/> <xs:element xmlns="" ref="other" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </original> <copy> <xs:complexType xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com"> <xs:sequence> <xs:element ref="name" minOccurs="0"/> <xs:element ref="other" minOccurs="0"/> </xs:sequence> </xs:complexType> </copy> If I add a value to the default namepsace, for example xmlns="a" it is not deleted. If I add a subelement with NO namespace the xmlns="" declaration is moved to this element. I think its a bug in the namespace fix up. Greetings Willem.


Replies (3)

Please register to reply

RE: Default null namespace (xmlns=&quot;&quot;) disappe - Added by Anonymous about 16 years ago

Legacy ID: #5089860 Legacy Poster: Michael Kay (mhkay)

Certainly looks like a bug (and namespaces probably have more test cases than everything else put together...) Seems to fail in 9.0 and 9.1, but 8.9 is OK. Michael Kay

RE: Default null namespace (xmlns=&quot;&quot;) disappe - Added by Anonymous about 16 years ago

Legacy ID: #5089917 Legacy Poster: Willem van Heerde (wheerde)

Hello Michael, Thanx, I will use the 8.9 release for the time being. Are there any other (major) namespace problems between the 8.9 release and the 9.1 release? In which release will this problem be solved? Regards, Willem van Heerde

RE: Default null namespace (xmlns=&quot;&quot;) disappe - Added by Anonymous about 16 years ago

Legacy ID: #5089958 Legacy Poster: Michael Kay (mhkay)

Sorry, but I think this is a problem with the language spec, and not with Saxon at all. In the language of the spec, when <xsl:copy> is applied to <xs:element ref="other" xmlns=""/> it creates an element with copies of all the namespace nodes from the source document. There are two such namespace nodes: xmlns:xs="..." and xmlns:xml="...". Then, ignoring the xs:sequence level, when <xsl:copy> is applied to <xs:complexType> it creates an xs:complexType element with three namespace nodes: xmlns:xs="...", xmlns:xml="...", and xmlns="http://www.test.com/". It then attaches a copy of the <xs:element> element, following the rules in 5.7.1 Constructing Complex Content. Rule 11 invokes namespace fixup, which does nothing, because all the prefixes used in element and attribute names are already declared. Rule 12 then invokes namespace inheritance: and this is the nasty bit. This causes the namespace xmlns="http://www.test.com/" to be propagated to the <xs:element> child. So when the time comes to serialize the result tree, the <xs:element> element does indeed have a namespace node for xmlns="http://www.test.com/" and therefore no xmlns="" undeclaration is generated. I thought I understood namespaces rather well, but this comes as a surprise to me! It means that the classic identity transform is not in fact an identity transform at all. I think it might be necessary to modify rule 12 so it does not apply to an unnamed namespace node (that is, xmlns="....."). I will raise the question with the Working Group. Meanwhile, I think you can achieve the desired effect using <xsl:copy inherit-namespaces="no">.

    (1-3/3)

    Please register to reply