xsl-list
[Top] [All Lists]

RE: JAXP - Ignoring whitespaces from a Node object passed as parameter for XSLT transformation

2005-10-15 13:35:05
The XSLT 1.0 spec is a little unclear on this point: it says that the
xsl:strip-space declaration applies to "source documents" but doesn't say
exactly what it means by the phrase. Documents loaded using the document()
function are described as "source documents", but the specification never
refers to values of global parameters in those terms.

XSLT 2.0 is more explicit: section 4.4 on whitespace stripping says "For the
purposes of this section, the term source tree means the document containing
the initial context node, and any document returned by the functions
document, doc, or collection. It does not include documents passed as the
values of stylesheet parameters or returned from extension functions." I
suspect that this is the way most XSLT 1.0 processors interpreted the 1.0
spec.

One workaround is therefore to load the document using the document()
function (perhaps via a URIResolver) rather than supplying it directly as a
parameter. If your application is building the tree only to pass it to the
stylesheet then this is almost certainly a better approach anyway, since
it's usually best to let the XSLT processor build the tree in the format it
thinks best rather than in a form that you have chosen. In particular, this
will enable it to strip whitespace in the course of tree construction rather
than doing it as a separate operation following tree construction.

The other workaround, of course, is to forget space-stripping. If you want
to ignore whitespace text nodes, then just ignore them. Using select="*"
rather than select="node()" will usually have the desired effect.

Michael Kay
http://www.saxonica.com/


-----Original Message-----
From: Blue Gecko [mailto:bluegecko(_at_)libero(_dot_)it] 
Sent: 15 October 2005 20:55
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] JAXP - Ignoring whitespaces from a Node object 
passed as parameter for XSLT transformation

Hi all,

I'm facing with a problem concerning ignorable whitespace text nodes.

I use an XSLT stylesheet with the <xsl:strip-space elements="*"/> 
instruction in order to avoid any redundancy.
I pass a Node object (say: myNode -- see the attached code), obtained 
from an XPath evaluation, to the XSLT transformer as a 
parameter (named 
"siteMap"), but the resulting transformation preserves *just* 
and *only* 
the whitespaces coming from *that* parameter nodeset: it 
seems that the 
XSLT parser is unable to deal properly with pre-constructed nodesets.
I want to stress that the rest of the transformed content 
(from the main 
XML source) works perfectly (all whitespaces are stripped as 
expected).

Is this a standard-compliant behavior?
How can I force XPath resulting Node to ignore redundant 
whitespace text 
nodes?

Thanks in advance!

---------------------
My code snippet:

import javax.xml.transform.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.*;

{

[snip]

// Define the XPath evaluator!
XPath xPath = XPathFactory.newInstance().newXPath();

// Get the DOM representation of the root node via XPath evaluation!
Node myNode = (Node) xPath.evaluate(
  "/rootTag",
  new InputSource(sourcePath + "siteMap.xml"),
  XPathConstants.NODE
  );

[snip]

// Get the page xslt transformer!
Transformer pageTransformer = 
TransformerFactory.newInstance().newTransformer(pageStyle);
pageTransformer.setParameter("siteMap", myNode);

[snip]

}

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--