Re: [xsl] check for whitespace value between nodes

Subject: Re: [xsl] check for whitespace value between nodes
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 20 Aug 2010 11:15:18 -0400
Hi,

At 04:07 PM 8/19/2010, Liam wrote:
On Thu, 2010-08-19 at 12:58 -0500, a kusa wrote:
> Hi
>
> Is there any way in XSLT to check if the value between two nodes is a
> whitespace?
>
> I tried normalize-space(product/text()) !=' ' but that is not working.
> I want tocheck if the actual value between <product> </product> is a
> white space.

Whitespace is a tricky thing to get right. It's impossible to give you a
good answer without knowing more about what you are trying to do and
why, and the context, and without seeing your XSLT.

Problems relating to "whitespace" are more often underspecified than not. XML has a definition of "white space" (any space, carriage return, line feed or tab character or run of such characters), but whether your definition in your problem aligns exactly with XML's is a different question. And even if it does, there are often other questions.


For example, in the problem statement here, what does "a white space" mean? By one reasonable reading it matches "^\s$", while another has it match "^\s+$" as Liam suggests. (Other readings allow strings that match neither.) Does a run of whitespace characters count as "a white space"?

Dimitre's XPath 1.0 solution, "product/text() and not(normalize-space(product/text())", works with the second of these definitions, since a run of whitespace is collapsed by normalize-space() into an empty string (and then coerced to Boolean false). That is, the test is refactored to (a) the text node exists (the 'product' element is not empty), but (b) it has no value other than whitespace characters.

For the first definition, one might want

translate(product/text(),'&#xA;&#x9;',' ')=' '

(in XPath 1.0). This turns any LF or TAB into a space, then sees if the resulting string is a single space. (Ordinarily one doesn't have to worry about CR since it has already been normalized by a parser. But even this isn't always so.)

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread