RE: [xsl] Testing for Missing or Empty Tags

Subject: RE: [xsl] Testing for Missing or Empty Tags
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 08 Jan 2004 15:05:14 -0500
Hi Rechell,

At 01:42 PM 1/8/2004, you wrote:
Thanks, Wendell, Vasu, Michael, and David for your helpfule responses.

You are very welcome.


Am I correct that:
1)not(Tag[normalize-space()]) will treat all blanks as if it were empty
text, whereas not(string(Tag)) will not?

Basically, but...


There's an important difference between

[a] Tag[normalize-space()] or Tag[string()]
    (short for child::Tag[normalize-space(self::node())] and
               child::Tag[string(self::node())] )

and

[b] string(Tag) or normalize-space(Tag)
    (short for string(child::Tag) or normalize-space(child::Tag) ) ...

The [a] expressions test true if there exists a Tag child with a non-null string value (string()) or a non-null normalized-string value (normalize-space()).

The [b] expressions perform the same tests, except only on the first Tag child of the context node: since the string() and normalize-space() functions require a single node as operand, the first node in the set in document order is used.

So, the answer is basically "yes", with the caveat that the two expressions are also not alike in that crucial other respect.

normalize-space() collapses white space and trims leading and trailing white space, so boolean(normalize-space(' ')) is false since normalize-space(' ') = ''.

2)If I had multiple sets of a given element, not(Tag[normalize-space()])
will return true for the following xml document because of the second
Tag element, while not(string(Tag)) will return false since the first
tag element has text?

<Xml>
<Tag>somevalue</Tag>
<Tag></Tag>
<Tag>Another value</Tag>
</Xml>

Not quite. Tag[normalize-space()] will test true by virtue of the existence of the first and third Tag elements (which have text content even when trimmed), so not(Tag[normalize-space()]) tests false.


You are correct, however, that not(string(Tag)) will test false since the first Tag child has text content besides whitespace.

If you wanted to test whether there exists a Tag child without text content, that would be test="Tag[not(normalize-space())]", which would test true in the above example because the second node passes the test in the predicate, therefore the node set returned is not empty.

Remember a node-set casts to Boolean "true" if there exists a node in the set, false if not.

I hope this helps--
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
======================================================================


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



Current Thread