xsl-list
[Top] [All Lists]

RE: [xsl] Implementing a (fairly) complex business rule

2008-09-30 11:28:30
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/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--