[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Implementing a (fairly) complex business rule

Subject: RE: Implementing a (fairly) complex business rule
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 30 Sep 2008 16:28:00 +0100
RE:  Implementing a (fairly) complex business rule
> I know that this is pushing your goodwill a bit far, but 
> would you mind explaining when I do need text() and when I 
> don't.

If you're using data-oriented XML with element-only content (no mixed
content, no comments or processing-instructions) then the only time you
might use text() is in a conditional: <xsl:if test="MIDDLE_NAME/text()">
which will be false if there is no MIDDLE_NAME element or if there is a
MIDDLE_NAME element and it is empty (more strictly, if every MIDDLE_NAME
child of the context node is empty). But I prefer to write this as
test="string(MIDDLE_NAME)", or as test="normalize-space(MIDDLE_NAME)" if I
also want to exclude the case where the value exists but contains
whitespace.

When you want the string value of MIDDLE_NAME, you can always use the
element name alone: <xsl:value-of select="MIDDLE_NAME"/>.

The only time you need to select text nodes and process them independently
is when handling mixed content (elements and text nodes as children of the
same element, as in <p>This is <b>exciting</b>!</p>. Usually this is done
using recursive descent using template rules, and you never access the text
node explicitly; but you might have a template rule with match="text()" if
you want to do something special, like turning typewriter quotes into curly
66-99 quotes. 

A special case is what one might call "abnormal mixed content", where the
structure is that of mixed content but the semantics are unorthodox: for
example 

 <date>2008-09-30
  <source>estimated</source>
  <calendar>Gregorian</calendar>
 </date>

(It would be more usual to use attributes here rather than child elements;
but one also sees comments used.) In such a case you may need to find the
text node (or all the text nodes) explicitly using path expressions.

Michael Kay
http://www.saxonica.com/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.