xsl-list
[Top] [All Lists]

Re: [xsl] check for whitespace value between nodes

2010-08-20 11:00:33
On Fri, Aug 20, 2010 at 8:15 AM, Wendell Piez 
<wapiez(_at_)mulberrytech(_dot_)com> wrote:
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.)

Hi Wendell,

In fact the expression I proposed also covers your "first definition".

Do note that there is only a single definition for whitespace in the
XML Recommendation (http://www.w3.org/TR/REC-xml/#sec-common-syn) :

[3]    S    ::=    (#x20 | #x9 | #xD | #xA)+

and this is exactly what the normalize-space() function handles
(http://www.w3.org/TR/xpath-functions/#func-normalize-space):

"Summary: Returns the value of $arg with whitespace normalized by
stripping leading and trailing whitespace and replacing sequences of
one or more than one whitespace character with a single space, #x20.

The whitespace characters are defined in the metasymbol S (Production
3) of [Extensible Markup Language (XML) 1.0 Recommendation (Third
Edition)]."



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play







Cheers,
Wendell



======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
======================================================================


--~------------------------------------------------------------------
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>
--~--

<Prev in Thread] Current Thread [Next in Thread>