Re: matching on namespaces, whitespace handling

Subject: Re: matching on namespaces, whitespace handling
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 14 Jun 1999 13:48:10 -0300
At 99/06/11 17:35 -0500, Wilson, James.W wrote:
>after banging on XSL (using the latest XT) quite a bit, I have some
>questions for the gurus.
>
>1. I don't understand the relationship between the names in the template
>'match' attributes, the namespaces defined in the input document, and the
>namespaces defined in the stylesheet. Perhaps it's somewhere in the spec,
>but I can't seem to find it.
>
>suppose I have a document
>
><myns:mystuff
>	xmlns:html='http://www.w3.org/TR/REC-html40'
>	xmlns:mystuff='http://www.murky.com/mystuff'
>	>
>	<myns:foo>
>		<html:a href='bar'>See bar</html:a>
>	</myns:foo>
></myns:mystuff>

First of all, I'll assume you had a typo in your namespace declaration that
should rather have read "xmlns:myns='http://www.murky.com/mystuff'".

>how should I match elements in a given namespace? 

By matching the URI of the namespace prefix used in the stylesheet with the
URI of the namespace prefix used in the source document.

>I understand that one can
>do <xsl:template match='html:*'>, but do I have to define the 'html:*' and
>'myns:*' namespaces in my XSL stylesheet if I want to match elements in
>those namespaces? 

You have to define the namespaces, but you need not use the same, or any
specific, prefix ... it is the matching URI that is important.

>Do I have to make sure that I use the same prefix as in
>the input document? 

No.

>What if someone uses 'myhtml:*' as the html prefix? 

They would do the following (note that due to the problem reported by James
I am unable to run this to demonstrate its functionality with XT):


D:\temp\wilson>type test.xml
<?xml version="1.0"?>
<myns:mystuff
	xmlns:html='http://www.w3.org/TR/REC-html40'
	xmlns:myns='http://www.murky.com/mystuff'
	>
	<myns:foo>
		<html:a href='bar'>See bar</html:a>
	</myns:foo>
</myns:mystuff>
D:\temp\wilson>type test.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";
                xmlns:hisstuff='http://www.murky.com/mystuff'
                xmlns:myhtml="http://www.w3.org/TR/REC-html40";
                result-ns="myhtml">

<xsl:template match="hisstuff:foo">
  <myhtml:p>Found a foo!</myhtml:p>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="hisstuff:mystuff">
  <myhtml:html>
    <xsl:apply-templates/>
  </myhtml:html>
</xsl:template>

<xsl:template match="myhtml:*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>


>It would be very helpful to find a document, ideally a big bold portion of
>the spec, which explains how the XSL processor should match input elements
>with namespace prefixes. Does the processor match 'html:*' using the URI of
>the namespace 'html' defined *in the stylesheet*? 

The URI, yes .... not the prefix.

>does it match using the
>qname in the original input? 

No, because qname() involves the prefix.

It matches namespace() and local-part(), neither of which involves the prefix.

>2. How do I find all the namespaces that are defined on a node? With the
>namespace() function I can find *the* namespace of a node, but I can't
>figure out how to get *all* defined namespaces.


I'm not sure where it is important for you ... it is the XSL engine's
responsibility to instantiate enough namespace nodes to ensure there are
enough namespace declarations in the output to be correct.

>3. When producing html output, I often want to control exactly how much
>space is produced between different parts of the result tree. 

Remember the objective of XSL is (to quote the working draft) "XSLT is also
designed to be used independently of XSL. However, XSLT is not intended as
a completely general-purpose XML transformation language. Rather it is
designed primarily for the kinds of transformation that are needed when
XSLT is used as part of XSL.".  Since the distribution of insignificant
whitespace is not material to XSL, there are no constraints or controls
available to a stylesheet writer regarding the placement of whitespace
inside markup (though you can add whitespace in element content using
various techniques described in XSLT).

You may want to ask XSLT vendors what their interpretation of the technical
term "nicely" in section 5 of XSLT that reads "the XSLT processor may add
whitespace to the result tree (possibly based on whitespace stripped from
either the source document or the stylesheet) in order to indent the result
nicely".  You may find product differentation from vendors will give you
some kinds of control of insignificant whitespace from one vendor not
available from another vendor.

I hope this helps.

......... Ken


--
G. Ken Holman                  mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.           http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0  +1(613)489-0999  (Fax:-0995)
Website: XSL/XML/DSSSL/SGML services outline,  XSL/DSSSL shareware,
         stylesheet resource library, conference training schedule,
         commercial stylesheet training materials, on-line XSL CBT.


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


Current Thread