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

Re: using normalize-space with mixed element content

Subject: Re: using normalize-space with mixed element content
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 08 Jun 2010 23:39:01 +0100
Re:  using normalize-space with mixed element content
On 08/06/2010 23:24, Lynn Murdock wrote:
hi-

i want to remove trailing whitespace from the contents of an element (<article-title>), but because that element sometimes contains character-level formatting elements (<italic>) as well as text, normalize-space is creating problems. if i use normalize-space(), i lose the italics in the output. (i'm transforming to html.)

here's an example of where i want to remove whitespace (i want to remove the space before</article-title>):

       <title-group>
         <article-title>Effect of a Brief Video Intervention on Incident Infection among Patients Attending Sexually Transmitted Disease Clinics</article-title>
       </title-group>

and here's the xsl code i've used:

     <xsl:template match="title-group/article-title" mode="none">
         <xsl:value-of select="normalize-space(.)"/>
     </xsl:template>

this code works most of the time, but in a situation like this:

<article-title>A Global Survey of Gene Regulation during Cold Acclimation in<italic>Arabidopsis</italic> <italic>thaliana</italic></article-title>

it ends up removing the italic formatting.

does anyone know of a way to strip the whitespace that i don't want (in the first example) while keeping the character formatting that i do want (in the second example)?

any pointers would be greatly appreciated.


thanks-


lynn

basically whenever doing string operations like thi son mixed content, the trick is to apply the operation to th etext nodes rather than al the content, so instead of


<xsl:template match="title-group/article-title" mode="none">

have

<xsl:template match="title-group/article-title/node()[last()][self::text()]" mode="none">

so you just apply this to the last node, if it's text.
and you want to remove trailing white space

if you are using xslt2 could use a regxep, but possibly if you also want to collapse space, the xslt1 way with normalise-space is easiest.

<xsl:value-of select="substring(normalize-space(concat('x',.)),2)"/>

</xsl:template>

then one for the first node:

<xsl:template match="title-group/article-title/node()[1][self::text()]" mode="none">
nt to collapse space, the xslt1 way with normalise-space is easiest.


<xsl:variable name="x" select="normalize-space(concat(.,'x'))"/>
<xsl:value-of select="substring($x,1,string-length($x)-1)"/>
</xsl:template>

and one for the simple case (which you need otherwise you will get an error about both the above matching the same node)

 <xsl:template match="title-group/article-title[not(*)]" mode="none">
  <xsl:value-of select="normailze-space(.)"/>
</xsl:template>

David

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.