Re: [xsl] Checking for empty elements

Subject: Re: [xsl] Checking for empty elements
From: <mlcook@xxxxxxxxxx>
Date: Fri, 9 Oct 2009 08:41:08 -0400
> At 2009-10-07 15:04 -0400, mlcook@xxxxxxxxxx wrote:
> >Is there a way using XSLT 2.0 to determine whether an element has no
> >attributes present in an XML document?
>
> That isn't quite what you are asking, though.
>
> >Say, given the type, with default values:
> >
> ><xs:complexType name="TimeType">
> >     <xs:attribute name="day" type="xs:unsignedByte" default="0"/>
> >     <xs:attribute name="hour" type="xs:unsignedByte" default="0"/>
> >     <xs:attribute name="minute" type="xs:unsignedByte" default="0"/>
> >     <xs:attribute name="second" type="xs:unsignedByte" default="0"/>
> ></xs:complexType>
> >
> >And an element definition of that type:
> >
> ><xs:element name="time" type="myown:TimeType"/>
> >
> >Is there an <xsl:if test=... /> to determine the difference between
the
> >empty element (which will have default attribute values if processed)
> ><time />
>
> Right ... which means to the stylesheet it reads, when validation is
turned on:
>
>    <time day="0" hour="0" minute="0" second="0"/>
>
> which is indistinguishable from an authored:
>
>    <time day="0" hour="0" minute="0"/>
>    <time day="0" hour="0" second="0"/>
>    ...
>    any combination of specified attributes of "0"

I was coming to this conclusion based on the tests I had tried.  I was
thinking that processing was being done as though the attributes had
actually been entered into the source XML.

I was going to head in the direction of testing each attribute
explicitly for '0' to find any non-zero cases, but your test below
(match="time[@*!='0']") is much better.

Thanks for the help, and another example of matching.  I'm better than I
used to be, but I can see I have quite a way to go.

Thanks, Ken.

-- Mike


> >and an element with explicit attributes, for example,
> ><time hour="5" minute="27" />
> >
> >I'm trying to not process the empty <time /> element.
>
> I have an example below working with XSLT 1 (and so XSLT 2).  This
> works whether or not you have supplied the schema and turned on
validation.
>
> I hope this helps.
>
> . . . . . . . . . . . Ken
>
> t:\ftemp>type mlcook.xml
> <times>
>    <time day="0" hour="0" minute="0" second="0">a</time>
>    <time hour="5" minute="27">b</time>
>    <time day="0">c</time>
>    <time>d</time>
> </times>
> t:\ftemp>xslt mlcook.xml mlcook.xsl
>
> "Empty": a
>
> Not "empty": b
>
> "Empty": c
>
> "Empty": d
>
> t:\ftemp>type mlcook.xsl
> <?xml version="1.0" encoding="US-ASCII"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>                  version="1.0">
>
> <xsl:output method="text"/>
>
> <xsl:template match="time">
> "Empty": <xsl:value-of select="."/>
> </xsl:template>
>
> <xsl:template match="time[@*!='0']">
> Not "empty": <xsl:value-of select="."/>
> </xsl:template>
>
> </xsl:stylesheet>
> t:\ftemp>


This email and any attachments are only for use by the intended recipient(s)
and may contain legally privileged, confidential, proprietary or otherwise
private information.  Any unauthorized use, reproduction, dissemination,
distribution or other disclosure of the contents of this e-mail or its
attachments is strictly prohibited.  If you have received this email in error,
please notify the sender immediately and delete the original.

Current Thread