This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Normalizing string containing entities
- To: xsl-list at mulberrytech dot com
- Subject: Re: Normalizing string containing entities
- From: David Carlisle <davidc at nag dot co dot uk>
- Date: Fri, 14 Jul 2000 12:36:32 GMT
- References: <NCBBJFCJCLKHONKCLLKOAEJFCJAA.pys@volga.fr>
- Reply-To: xsl-list at mulberrytech dot com
> 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