Stripping whitespace around certain elements

Discussion in 'XML' started by Andy Jefferies, Jun 25, 2003.

  1. I'm having problems stripping out the whitespace at the beginning of a
    particular element. In the XML snippet I've highlighted tabs and returns
    as ^I and ^M respectively:

    <para> ^I ^I ^I^M
    Some text with occasional highlighting. Some text with occasional^M
    highlighting. Some text with occasional <high>highlighting</high>.^M
    Some text with occasional highlighting. Some <high>text</high> with^M
    occasional highlighting.</para>^M

    I'd like my XSL to remove all whitespace immediately following <para> and
    convert all other ^M's to a single space. My attempt:

    <xsl:template match="para">{P}<xsl:apply-templates/>{\P}</xsl:template>
    <xsl:template match="high">{B}<xsl:apply-templates/>{\B}</xsl:template>
    <xsl:template match="text()"><xsl:value-of select="normalize-space()"/>
    </xsl:template>

    Strips out too much white space:

    {P}Some text with occasional highlighting. Some text with occasional
    highlighting. Some text with occasional{B}highlighting{\B}. Some
    text with occasional highlighting. Some{B}text{\B}with occasional
    highlighting.{\P}

    Where the spaces are lost around the highlighting. I understand why it's
    happening but can't fix it. Adding spaces around {B} and {\B} in the XSL
    helps to some extent, but breaks if the highlighting is at the beginning
    or end of <para> and around punctuation.

    Could somebody help me make it more selective?

    Many thanks,
    Andy.
    Andy Jefferies, Jun 25, 2003
    #1
    1. Advertising

  2. "Colin Mackenzie" <> wrote :

    > Not sure if this is what you want to do, but it sort of works.


    Thanks! There's just one case I've found where this doesn't work but it's
    perfectly acceptable until I can invest the time in learning extensions.

    > Text processing (as opposed to node processing) is still best done in any
    > language with regular expresion handling built0-in or via extensions (Perl,
    > Python, Java, VB, VBScript, JScript etc).


    Yes...

    Cheers!
    Andy.

    >
    > This would be easier with XSLT2 I guess...
    >
    > <xsl:template match="para">{P}<xsl:apply-templates/>{\P}</xsl:template>
    > <xsl:template match="high">{B}<xsl:apply-templates/>{\B}</xsl:template>
    >
    > <xsl:template match="text()">
    > <!-- if its NOT the first node in the parent and it starts with a space,
    > out put space -->
    > <xsl:if test="(position() != 1) and (substring(., 1,1)=' ')">
    > <xsl:text> </xsl:text>
    > </xsl:if>
    > <!-- throw away all leading or trailing spaces -->
    > <xsl:value-of select="normalize-space()"/>
    >
    > <!-- if it ends with a space, bung a space out -->
    > <xsl:if test="substring(., string-length(.),1)=' '">
    > <xsl:text> </xsl:text>
    > </xsl:if>
    > </xsl:template>
    >
    Andy Jefferies, Jun 26, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Tyler
    Replies:
    7
    Views:
    22,937
    danielson317
    Sep 15, 2011
  2. ryan k

    Stripping whitespace

    ryan k, Jan 23, 2008, in forum: Python
    Replies:
    22
    Views:
    810
    Reedick, Andrew
    Jan 24, 2008
  3. Taylor Strait
    Replies:
    13
    Views:
    370
    Chris Gernon
    Dec 28, 2006
  4. Douglas Wells
    Replies:
    8
    Views:
    142
    Nobuyoshi Nakada
    Jan 27, 2007
  5. Virendra Negi
    Replies:
    0
    Views:
    103
    Virendra Negi
    May 26, 2009
Loading...

Share This Page