RE: [xsl] Testing for Missing or Empty Tags

Subject: RE: [xsl] Testing for Missing or Empty Tags
From: "Schwartz, Rechell R, ALABS" <rrschwartz@xxxxxxx>
Date: Thu, 8 Jan 2004 12:42:42 -0600
Thanks, Wendell, Vasu, Michael, and David for your helpfule responses.

I am assuming that the data elements will only have text in them, and
that there
will be only one or zero elements and that there will be no white-space
issues,
so not(string(Tag)) seemed to do the trick, but I am still curiuous
about the difference between the two solutions:

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?

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>

Thanks,
Rechell

-----Original Message-----
From: Wendell Piez [mailto:wapiez@xxxxxxxxxxxxxxxx]
Sent: Thursday, January 08, 2004 12:58 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Testing for Missing or Empty Tags


Maybe Rechell wants

<xsl:if test="not(Tag[normalize-space() or *]">...</xsl:if>

or the same leaving out the "or *" if she already knows there will never
be 
an element inside the Tag element, or doesn't care if there is.

string(Tag), alas, checks to see whether the first Tag element child has
a 
string value (not the empty string). This may or may not be useful 
depending on the data (do Tag elements appear clustered or are they
always 
alone?). It also doesn't address the empty string issue, and will test
true 
on <Tag>    </Tag> (which is not always wanted).

Whenever this question comes up, it turns out the answer depends on what

the OP means by "empty" elements. If Rechell wants to assure that there
is 
present a Tag child that includes some kind of text not whitespace, 
not(Tag[normalize-space()] will do that: it checks whether there does
(not) 
exist a Tag element child with a non-empty string value. But David has 
already pointed out that this will fail on <Tag><some/></Tag>. Only
Rechell 
can say whether that case should be in or out.... :->

Cheers,
Wendell

At 10:30 AM 1/8/2004, David wrote:
>XSLT has no access to the tags in the document.
>
>the test <xsl:if test="not(Tag)" would test if there are any Tag
element
>node children of the current node,
>
>
><xsl:if test="not(Tag[1]) or Tag ='' ">
>
>is the same as
>
>
><xsl:if test="not(Tag) or Tag ='' ">
>
>and tests if there is no Tag element or if some empty Tag element has
>empty string value, so it would be true on
>
><x><Tag><y/></Tag></x>


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


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


Current Thread