Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'd like to trim the leading whitespace inside p tags in XML, so this:

<p>  Hey, <em>italics</em> and <em>italics</em>!</p>

Becomes this:

<p>Hey, <em>italics</em> and <em>italics</em>!</p>

(Trimming trailing whitespace won't hurt, but it's not mandatory.)

Now, I know normalize-whitespace() is supposed to do this, but if I try to apply it to the text nodes..

<xsl:template match="text()">
  <xsl:text>[</xsl:text>
  <xsl:value-of select="normalize-space(.)"/>
  <xsl:text>]</xsl:text>
</xsl:template>

...it's applied to each text node (in brackets) individually and sucks them dry:

[Hey,]<em>[italics]</em>[and]<em>[italics]</em>[!]

My XSLT looks basically like this:

<xsl:template match="p">
    <xsl:apply-templates/>
</xsl:template>

So is there any way I can let apply-templates complete and then run normalize-space on the output, which should do the right thing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
802 views
Welcome To Ask or Share your Answers For Others

1 Answer

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="p//text()[1][generate-id()=
                                      generate-id(ancestor::p[1]
                                                  /descendant::text()[1])]">
        <xsl:variable name="vFirstNotSpace"
                      select="substring(normalize-space(),1,1)"/>
        <xsl:value-of select="concat($vFirstNotSpace,
                                     substring-after(.,$vFirstNotSpace))"/>
    </xsl:template>
</xsl:stylesheet>

Output:

<p>Hey, <em>italics</em> and <em>italics</em>!</p>

Edit 2: Better expression (now only three function calls).

Edit 3: Matching the first descendant text node (not just the first node if it's a text node). Thanks to @Dimitre's comment.

Now, with this input:

<p><b>  Hey, </b><em>italics</em> and <em>italics</em>!</p>

Output:

<p><b>Hey, </b><em>italics</em> and <em>italics</em>!</p>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...