Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
846 views
in Technique[技术] by (71.8m points)

xslt - Trim whitespace from parent element only

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

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

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...