xsl-list
[Top] [All Lists]

Re: [xsl] Novice Question - matching entire text children

2010-12-21 08:11:57

(fourth try to send this email to xsl-list, this time without reply)

In any case, it's a good idea to make sure that your code still works if
someone puts a comment in the middle of the text, so it's best not to
assume that all the text of an element is in a single node. Usually the
best way of doing that (unless you are dealing with mixed content) is to
use the string-value of the element node, rather than its text node
children.

As already stated this makes a difference for mixed content.
Take this simple XML file as sample:

$ cat mixed.xml
<a> 1<b>2</b>3 </a>
$

While string(/a) returns a string, /a/text() returns a node-set:
$ xpath++ "string(/a)" mixed.xml
 123
$ xpath++ "/a/text()" mixed.xml

###################################################################
 1
###################################################################
3
$

This makes a big difference if applying the often used normalize-space()
function to the result. If you want to get the normalized concatenation
of the content ("123"/"13") directly applying normalize-space() does not
work (for "13"). The reason is that normalize-space() applied to a node-set
will only be applied to the first node of that node-set:
$ xpath++ "normalize-space(string(/a))" mixed.xml
123
$ xpath++ "normalize-space(/a/text())" mixed.xml
1
$

What I do for getting "13" from mixed.xml:
<xsl:variable name="txt"><xsl:copy-of select="text()"/></xsl:variable>
<xsl:value-of select="normalize-space($txt)"/>

Not sure whether this is the best way to do it, but it works.

Is getting the same result possible in XPath (1.0)?


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler, L3
Fixpack team lead
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294


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

<Prev in Thread] Current Thread [Next in Thread>