RE: [xsl] overriding namespaces

Subject: RE: [xsl] overriding namespaces
From: "Dion Houston" <dionh@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 29 May 2003 19:33:33 -0700
Hi Mike:

You have found what some (like me) consider to be a feature of XPath,
that is namespace awareness.  Basically elements in a different domain
are considered distinct, regardless of how similar they may be in domain
name.

In your particular situation, I would advise against attempting to have
one generic stylesheet that handled both versions.  My preferred
solution would be to archive your existing stylesheets, and recreate
them with the new namespace.

Among the reasons:

- Archiving your existing stylesheets would guarantee results if you
needed to do a v4.0 only run in the future.  Should you wish to change
your stylesheets for 5.0 while maintaining the behavior for 4.0, you can
easily change them if they're separate.

- Should you wish to do new behavior with your stylesheets in some
cases, but not in others, having two stylesheets would allow you to
import the old one, and fall back whenever you desire.

- This would lead to the simplest, cleanest implementation of templates
on two separate sets of objects.

Should you really desire to have one stylesheet, then you have a few
options (in order of ugliness):

1. Make each of your template matches match on both element names (i.e.
match="v4:foo | v5:foo")

2. Match on the local name (match="*[local-name()='root']")

3. Massage out the namespaces and apply on the null names (i.e.
(pseudo-XSLT))

<xsl:variable name="massaged">
   <xsl:apply-templates mode="removeNamespaces"/>
</xsl:variable>
<xsl:variable name="massaged.ns" select="vendor:node-set($massaged)"/>

<xsl:template match="*" mode="removeNamespaces">
   <xsl:element name="local-name()">
      ...
   </xsl:element>
</xsl:template>

...
<xsl:apply-templates select="$massaged.ns"/>
...
<xsl:template match="root">

HTH!

Dion Houston
SDE/T UDDI (http://uddi.microsoft.com)


-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of michael shea
Sent: Thursday, May 29, 2003 3:14 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] overriding namespaces

Hi All,

We currently have a problem whereby we need to change the namespace
declared 
for a certain entity depending on the namespace that will be in the XML 
input.  As an example, here is the a sample of the stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:test="http://www.test.com/v4.0/test";>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <test>
      <xsl:value-of select="/test:root/test:name"/>
    </test>
  </xsl:template>
</xsl:stylesheet>

In this example, we are trying to output the /root/name text value of
the 
input document, which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.test.com/v4.0/test";>
  <name>my_name</name>
</root>

As you can see the namespace is http://www.test.com/v4.0/test.  This
will 
work correctly when the namespaces match, but we also have the case
whereby 
the input document could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.test.com/v5.0/test";>
  <name>my_name</name>
</root>

Notice the only difference is the v5.0 instead of v4.0 in the namespace.

Now running the stylesheet again, there is no output for the name
because 
the namespaces differ. So my question is, does anyone know of a way to
have 
different namespaces handled by the same stylesheet?

I know one solution would be to double up the code inside the stylesheet
to 
include a check for both versions, but this is not an ideal solution.

One method I tried was to import a dummy stylesheet with a namespace of 
xmlns:test that the user could change to suit whatever version they were

currently working on, but this didn't work.

Another method suggested was to pass in the version as a parameter to
the 
stylesheet, however I don't know how to get this to work especially 
considering the namespace declarations are declared in the header of the

stylesheet (which might lead to the next response, "why don't you
generate 
your stylesheet with another stylesheet with the correct version in the 
header?" , well due to the intricacies of the way our stylesheets are
used, 
we can't do this).

Thanks in advance for any help.
Mike.

_________________________________________________________________
ninemsn Extra Storage is now available. No account expiration - no need
to 
worry about losing your Hotmail account. Go to  
http://join.msn.com/?page=dept/home&pgmarket=en-au


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread