This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: trying to bypass empty elements


Cedric,

> How can I bypass an empty element:
>
> <xsl:template match="/">
>     blahblah.....
>     ....
>     <xsl:apply-templates select="elmt"/>
> </xsl:template>
[snip]
> I want to do something like this on the <elmt> element but only if there is
> some data / subelement in it!

You can access an element's content by looking at its child nodes with
the XPath:

  child::node()

or (as the 'child' axis is the default axis), simply:

  node()

This XPath gives you a list of the nodes that form the content of the
element. When you cast that list of nodes to a boolean, it is true if
there are any nodes in the list or false if not. So 'boolean(node())'
will return false if the element is completely empty.

You can use this predicate to filter the list of elements to which you
apply templates:

  <xsl:apply-templates select="elmt[node()]" />

This says 'apply templates to those 'elmt' elements that have any
child nodes'. [Note: within a predicate, expressions are cast to
booleans - you don't have to do it explicitly.]

You may want to tighten this a bit more: you might only be interested
in whether the elmt elements have child elements or non-whitespace
textual content, for example (and want to ignore those that just have
comments or processing instructions as content), in which case you can
use:

  elmt[* or normalize-space()]

i.e. select elements that have element children or that have a string
value that isn't entirely whitespace.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]