Re: [xsl] using normalize-space with mixed element content

Subject: Re: [xsl] using normalize-space with mixed element content
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 08 Jun 2010 23:39:01 +0100
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