RE: Matching namespaces on source documents

Subject: RE: Matching namespaces on source documents
From: "Richman, Jeremy" <jrichman@xxxxxxxxxxxx>
Date: Tue, 9 Nov 1999 12:29:44 -0500
Thanks Mike Brown and Michael Kay, those are very comprehensive

The stylesheet I was trying to create precluded putting the namespace
declaration into the stylesheet element, because my transformation was
from an old version of an XSL stylesheet to a new one.  The 'source
declared the xsl prefix in its document element (xsl:stylesheet) and
gave it a different namespace than my transformation sheet did.

For example, one of the required changes was to change the old xsl:number
level="multi" to level="multiple".

The sample case for this is:

Source document stylesheet (needs to be transformed to newer version):

<xsl:stylesheet xmlns:xsl="";>
  <xsl:template match="Section">
    <xsl:number count="Chapter|Section" level="multi" format="1.1"/>

transformation stylesheet:

<xsl:stylesheet xmlns:xsl="";>
<xsl:output method="xml" xml-declaration="yes" encoding="UTF-8"

<!-- Convert @level=multi to @level=multiple -->
<xsl:template match="@level[local-name(..)='number' and
                        .='multi' and
   <xsl:attribute name="level">multiple</xsl:attribute>

This works with LotusXSL, but XT gives the following error:
file:in.xsl:1: no such prefix "xmlns".

I'm not sure whether this is a problem with XT or whether it is
being correctly rigorous where LotusXSL is not.

Note: of course my example is deliberately simplified and does not create
the correct xsl:stylesheet element on the output, nor copy the rest of the
source documents.  The basic templates I used for those tasks are:

<!-- ************************************************************ -->
<!-- *         Rule for stylesheet element                        -->
<!-- *           High priority to assure this rule is used        -->
<!-- ************************************************************ -->

<xsl:template match="/*" priority="10">
   <xsl:element name="xsl:stylesheet">
      <xsl:apply-templates select="node()"/>

<!-- ************************************************************ -->
<!-- *   DEFAULT COPY RULES FOR elements, attributes, comments,   -->
<!-- *      processing-instructions and text                      -->
<!-- *   These all have low priority to ensure they are picked    -->
<!-- *      as a last resort.                                     -->
<!-- *                                                            -->
<!-- *   The default rules for xsl: and fo: elements have         -->
<!-- *     a higher priority than the other fall-back default.    -->
<!-- ************************************************************ -->

<!-- Default copy for attributes, with a low priority -->
<xsl:template match="@*" priority="-1">

<!-- Default copy for fo attributes, with a slightly higher priority -->
<xsl:template match="@*[namespace-uri(..)='']"
   <xsl:attribute name="{local-name(.)}">
      <xsl:call-template name="inches-to-in">
         <xsl:with-param name="content"><xsl:value-of

<!-- Default copy rules for fo:elements; low priority -->
<xsl:template match="*[namespace-uri(.)='']"
   <xsl:element name="fo:{local-name(.)}"
      ><xsl:apply-templates select="@*|node()"

<!-- Default copy rules for xsl:elements; low priority -->
<xsl:template match="*[namespace-uri(.)='']"
   <xsl:element name="xsl:{local-name(.)}"
      ><xsl:apply-templates select="@*|node()"

<!-- Default copy rules for all other elements; lowest priority -->
<xsl:template match="*" priority="-2">
   <xsl:element name="{local-name(.)}"
      ><xsl:apply-templates select="@*|node()"

<!-- Default copy rule for comments, processing instructions and text -->
<xsl:template match="comment()|processing-instruction()|text()"
      <xsl:apply-templates select="node()"/>


Mike Brown wrote:
To expand on Michael Kay's explanation...

> <xsl:template match="test:foo">

This says "this template matches elements with the local name
'foo' that have associated with them a namespace URI that has
been assigned to the 'test' prefix in this stylesheet" ... 
it does *not* say "this template matches elements designated
'test:foo' in the source document".

It's kind of confusing because you do have access to the
'test' prefix from the source document, but not in this way.

You're matching the namespace URI that 'foo' is assigned to,
not 'foo'. It might help if you consider what happens when
you don't use prefixes:

  <?xml version="1.0"?>
     <foo xmlns="";>hello world</foo>

How would you make a template match this foo? match="foo"
wouldn't work because the namespace URI is not null.
match="foo[namespace-uri(.)='']" would
probably work, but is inconvenient. So, you can declare
a prefix-to-namespace-URI assignment in your stylesheet:


and then use <xsl template match="mytest:foo">. The URIs have
to match between the stylesheet and source document. Whether
the prefixes do or don't is irrelevant.

One of the consequences of this is that there's apparently
no way of knowing whether the prefix assignment was done in
the stylesheet to aid in processing or if you're intending
to have the assignment appear in the result tree. Your
XSL processor might be inclined to put the assignment in the
output, even if the prefix is not used in the result tree.
The result tree would probably have to be reparsed after it
is built in order to determine whether the prefix is
actually needed. Then you're still left wondering what to do
if you wanted the prefix to be declared anyway...

Someone else may want to comment/correct me on this. I know
it has come up before.

- -Mike

 XSL-List info and archive:

Current Thread