开发者

Wrap words in tags using XSLT

How can I wrap span tags (or any tag) around individual words in XSLT? I'm using XSLT1 but seem to be getting stuck each time I try.

Essentially, I want to pass开发者_如何学Go in a paragraph (or string of text):

<p>This is my text!</p>

and wrap each word like so, preserving the whitespace and punctuation between each word:

<p><span class="word-1">This</span> <span class="word-2">is</span> <span class="word-3">my</span> <span class="word-4">text!</span>

It's mainly for presentational purposes, I'd appreciate any help


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pSeparators">&#xA;&#x9;&#x20;,.;:?!()'"</xsl:param>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="p/text()" name="tokenize">
        <xsl:param name="pString" select="."/>
        <xsl:param name="pMask"
                   select="translate(.,translate(.,$pSeparators,''),'')"/>
        <xsl:param name="pCount" select="1"/>
        <xsl:choose>
            <xsl:when test="not($pString)"/>
            <xsl:when test="$pMask">
                <xsl:variable name="vSeparator"
                              select="substring($pMask,1,1)"/>
                <xsl:variable name="vString"
                              select="substring-before($pString,$vSeparator)"/>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString" select="$vString"/>
                    <xsl:with-param name="pMask"/>
                    <xsl:with-param name="pCount" select="$pCount"/>
                </xsl:call-template>
                <xsl:value-of select="$vSeparator"/>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString"
                               select="substring-after($pString,$vSeparator)"/>
                    <xsl:with-param name="pMask"
                                    select="substring($pMask,2)"/>
                    <xsl:with-param name="pCount"
                                    select="$pCount + boolean($vString)"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <span class="word-{$pCount}">
                    <xsl:value-of select="$pString"/>
                </span>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Output:

<p><span class="word-1">This</span> <span class="word-2">is</span> <span class="word-3">my</span> <span class="word-4">text</span>!</p>

Note: Tokenize by several separators. Edit: Better names. Adding white space characters to separators sequence.


A simple recursive named template should do the trick:

  <xsl:template name="wordsInTags">
      <xsl:param name="text"
                 select="."/>
      <xsl:param name="index"
                 select="1"/>
      <xsl:choose>
        <xsl:when test="contains($text, ' ')">
          <span class="word-{$index}">
            <xsl:value-of select="substring-before($text, ' ')"/>
          </span>
          <xsl:text> </xsl:text>
          <xsl:call-template name="wordsInTags">
            <xsl:with-param name="text">
              <xsl:value-of select="substring-after($text, ' ')"/>
            </xsl:with-param>
            <xsl:with-param name="index">
              <xsl:value-of select="$index + 1"/>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <span class="word-{$index}">
            <xsl:value-of select="$text"/>
          </span>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

And then simply invoke it for the element in which the words need to be wrapped:

    <xsl:template match="p">
      <p>
        <xsl:call-template name="wordsInTags"/>
      </p>
    </xsl:template>

Threw this together real quick, so it may need some more work, but this should get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜