RE: [xsl] Getting rid of xmlns="" attributes

Subject: RE: [xsl] Getting rid of xmlns="" attributes
From: "Allen, Erik" <eallen@xxxxxxxxxxx>
Date: Wed, 31 Dec 2003 11:11:54 -0700
> -----Original Message-----
> From: Lars Huttar [mailto:lars_huttar@xxxxxxx]
> Sent: December 30, 2003 7:34 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] Getting rid of xmlns="" attributes
> If I understand you right, the source XML elements have no
> namespace, and you want the result XML elements to be in no
> namespace. However,

The original XML looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<template version="3" readVersion="3" xml:space="preserve">
  <templateInfo name="" id="1139410602" revision="1">

	So, as you can see, there is no namespace.
> > I need
> > to add two namespaces to the XML.
> I take this to mean you want to add two namespace
> declarations to the output,
> "";
> and
> "";
> Clearly the desired prefix for the latter is "xsi",
> and you've got that covered.
> But what prefix do you want for the former? And what
> elements do you want to be in that namespace?
> In your stylesheet you're using the null prefix,
> i.e. you're making "" the default namespace.
	That is correct. The ""; is
supposed to be the default namespace so I don't have a prefix for it.

> As a result, your output <template> element is in
> this "" namespace (because result tree
> elements with no namespace prefix go in the stylesheet's
> default namespace). Is that what you wanted?
> Did you want the other elements to be in that namespace too?

	I want it all in that namespace.


> > 	I've found that if I change the namespace in the
> > <xsl:stylesheet> from xmlns to xmlns:y, then I don't get the rogue
> > attribute,
> That makes sense. You no longer have a non-null default namespace,
> so there is no need to override the default namespace to produce
> elements that are in no namespace.
> > but that is also giving me the incorrect namespace.
> Here is where I would need more specifics to understand what you want.
> Which elements have the incorrect namespace and what namespace
> did you want them to have?
> (Terminology precision check ... do you mean the namespace is
> or a namespace declaration, or a namespace prefix?)
> Do you want all of the output XML to be in no namespace? all
> in the tempuri namespace? some of each?
> If the latter, what namespace prefix do you want used for each?
> You can't use "" as a prefix for two different namespaces,
> unless you put xmlns="..." in multiple places.

	I will confess that I'm not totally comfortable with namespace
terminology so I may have been using incorrect terms. The original XML
file has no namespace, even though everyone agrees it should have.
However, due to legacy reasons, we can't add one in. (XPaths would then
require the namespace prefix within every element name, which would
require changing a lot of code which would then no longer work with
older XML files.)
	For the "Terminology precision check", I believe I mean that the
namespace prefix is incorrect. The URL within the namespace looks
exactly how I want it, but I also want it to be the default namespace,
not an explicit one.
	I want the output XML to be within the tempuri namespace, same
as the root element is.
	From the comments I've been getting back, I believe I have
solved my problem. I changed the stylesheet to look like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl=''
		version = '1.0'>

<xsl:output method="xml"/>

<xsl:template match="/template">
    <xsl:attribute name="version">
      <xsl:value-of select="@version"/>
    <xsl:attribute name="readVersion">
      <xsl:value-of select="@readVersion"/>
    <xsl:apply-templates select="*"/>

<xsl:template match="node()">
  <xsl:element name="{local-name()}"
    <xsl:apply-templates select="@*|node()|text()"/>

<xsl:template match="@*|text()" priority="3">
  <xsl:copy-of select="."/>


	This seems to have solved all the problems. The xsl:copy-of was
preserving the namespace, so I have to explicitly write out the elements
with the correct namespace.
	I apologize for not stating the XSL processor I'm using. I'm
personally using Saxon 6.2.2 for my testing. The person who will be
actually running this stylesheet is using the Microsoft .NET version.
Since both were showing the same problem, I assumed it was common to all
XSL processors.
	Thanks for all the help!

	Erik Allen

 XSL-List info and archive:

Current Thread