Subject: Re: [xsl] Default namespace in result document From: andrew welch <andrew.j.welch@xxxxxxxxx> Date: Wed, 7 Dec 2005 19:40:08 +0000 |
> I have been using XSLT successfully for a few years now but have never > tried to make namespaces work, mainly because at least one component in > the system has not supported them properly. I've now tried using them > for the first time and although I get a result document that is correct > it's not quite what I had expected. Here's an example: > > [XML input:] > > <?xml version="1.0"?> > <foo xmlns="http://example.com/foo"> > <blah/> > </foo> > > > [XSLT:] > > <?xml version="1.0" encoding="utf-8"?> > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > version="1.0" > xmlns:foo="http://example.com/foo"> > > <xsl:output method="xml" indent="yes"/> > > <xsl:template match="/"> > <foo:root> > <xsl:apply-templates mode="copy"/> > </foo:root> > </xsl:template> > > <xsl:template match="*" mode="copy"> > <xsl:copy> > <xsl:apply-templates mode="copy"/> > </xsl:copy> > </xsl:template> > > </xsl:stylesheet> > > > So the stylesheet contains literal result elements in namespace foo, and > also copies elements from the input XML that are in namespace foo. I > was hoping to get something like this: > > <?xml version="1.0"?> > <root xmlns="http://example.com/foo"> > <foo> > <blah/> > </foo> > </root> > > i.e. the default namespace is defined on the root element, and no > namespace things are used anywhere else. But instead, using libxslt > version 1.1.15, I get this: > > <?xml version="1.0"?> > <foo:root xmlns:foo="http://example.com/foo"> > <foo xmlns="http://example.com/foo"> > <blah/> > </foo> > </foo:root> > > This is not ideal because it makes files much larger than they need to > be. I guessed that there would be some xsl:output attribute to select a > default namespace but I can't see anything that does exactly this. I > can't help feeling that I've missed something obvious. Can someone > suggest what I need to do? > > (I assume that this is also an issue for people trying to generate > "HTML-compatible XSLT" - not something I have tried to do yet...) Use a default namespace in the stylesheet, eg: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://example.com/foo"> (note the foo prefix has been removed) This puts all literal result elements in that namespace, so you would need to remove the prefixes. This creates the result: <?xml version="1.0" encoding="utf-8"?> <root xmlns="http://example.com/foo"> <foo> <blah/> </foo> </root> ...which I think is what you are after. cheers andrew
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Default namespace in result d, Phil Endecott | Thread | Re: [xsl] Default namespace in resu, Geert Josten |
Re: [xsl] Variable assign, Dimitre Novatchev | Date | [xsl] Output Before DOCTYPE?, Michael B Allen |
Month |