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

Re: Normalizing string containing entities

Subject: Re: Normalizing string containing entities
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 14 Jul 2000 12:36:32 GMT
xsl combining strings
> Does anyone know a simple way to normalise strings containing entities and
> subelement ?

entities are no problem as they are all expanded before XSL sees the
document.

normalising space in mixed content is _always_ a problem.
Of course normally you just don't do it, as the renderer (eg html in
this case) can more easily do it as it lays out the characters, after
all the markup is gone.

If you do want to do it within the markup, the problem as stated was
underspecified, but here is a possible (probably partial) solution.
As always documented to the "literate programming" standards discussed
in an earlier thread on this list.

David


<x>
<para>Some    text    <em>some    other   text</em>   remaining text</para>
<para>Some    text<em>    some    other   text</em>   remaining text</para>
<para>Some    text    <em>   some    other   text</em>   remaining text</para>
<para>Some    text    <em>some    other   text </em>   remaining text</para>
<para>Some    text    <em>some    other   text </em>remaining text</para>
<para> Some    text    <em>some    other   text</em>   remaining text </para>
</x>





<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0"
                >

<xsl:output method="xml" indent="yes"/>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>


<xsl:template match="para/text()">
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="contains(concat(.,'^$%'),' ^$%') and following-sibling::* and
 not(following-sibling::*[1]/node()[1][self::text() and starts-with(.,' ')])">
  <xsl:text> </xsl:text>
</xsl:if>
</xsl:template>



<xsl:template match="para/*/text()">
<xsl:if test="starts-with(.,' ')"><xsl:text> </xsl:text></xsl:if>
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="contains(concat(.,'^$%'),' ^$%') or 
  ../following-sibling::node()[1][self::text() and starts-with(.,' ')]">
  <xsl:text> </xsl:text>
</xsl:if>
</xsl:template>

</xsl:stylesheet>


bash-2.01$ xt normsp.xml normsp.xsl 
<?xml version="1.0" encoding="utf-8"?>
<x>
<para>Some text <em>some other text </em>remaining text</para>
<para>Some text<em> some other text </em>remaining text</para>
<para>Some text<em> some other text </em>remaining text</para>
<para>Some text <em>some other text </em>remaining text</para>
<para>Some text <em>some other text </em>remaining text</para>
<para>Some text <em>some other text </em>remaining text</para>
</x>



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.