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

Subject: Re: [xsl] Novice Question - matching entire text children
From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx>
Date: Wed, 22 Dec 2010 08:29:20 +0100
Hi Wendell,

> "normalize-space(text())" will do the same without copying the text
> nodes into the variable first, if that's what you're asking.

that is exactly the point, it does not do the same thing without copying!

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

1:  13
2:  1
$
$ cat mixed.xsl
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>
  <xsl:output omit-xml-declaration="yes" />

  <xsl:template match="/">
    <xsl:variable name="txt"><xsl:copy-of select="/a/text()"/>
</xsl:variable>
1:  <xsl:value-of select="normalize-space($txt)" />
2:  <xsl:value-of select="normalize-space(/a/text())" />
  </xsl:template>

</xsl:stylesheet>
$


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



From:       Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
To:         xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Date:       12/21/2010 06:18 PM
Subject:    Re: [xsl] Novice Question - matching entire text children



Hermann,

On 12/21/2010 9:11 AM, Hermann Stamm-Wilbrandt wrote:
> 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)?

"normalize-space(text())" will do the same without copying the text
nodes into the variable first, if that's what you're asking.

But as David and others keep reminding us, what we usually want is
actually "normalize-space(.)" i.e. "normalize-space(self::node())",
which does not drop the values of any descendant elements (so "123" not
"13").

In fact, when I am faced with the need to get "13" from
"<e>1<f>2</f>3</e>", I'm probably also thinking badly of the XML
designer, since prima facie it would appear to violate one of the
unwritten patterns of XML design, namely "discrete data points should be
designated by discrete elements". Not that there aren't always
exceptions, of course. But either 1 and 3 belong together without 2, or
they don't.

Cheers,
Wendell

======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
   Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread