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
1.0k views
in Technique[技术] by (71.8m points)

xml - XSLT - Best way to split and render comma separated text as HTML

I have some data in an XML element that looks like this:

<item value="category1,category2">Item Name</item>

The bit I'm interested in is the value attribute. I'm able to get the data contained in this attribute into a template which looks like this:

<xsl:template name="RenderValues">
    <xsl:param name="ValueList" />
    <xsl:value-of select="$ValueList" /> <!-- outputs category1,category2-->
</xsl:template>

What I want to do is to process the comma separated values in an efficient manner. What is the best way to render something like the following from inside the RenderValues template?

<a href="x.asp?item=category1">category1</a>
<a href="x.asp?item=category2">category2</a>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

In XSLT 2.0/XPath 2.0 use the standard XPath 2.0 function tokenize().

In XSLT 1.0 one needs either to write a recursively called template or, more conveniently, use the str-split-to-words function/template of the FXSL library.

Here is an example of the latter:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"

>
<!--                                                 -->
   <xsl:import href="strSplit-to-Words.xsl"/>
<!--                                                 -->
   <xsl:output indent="yes" omit-xml-declaration="yes"/>
<!--                                                 -->
    <xsl:template match="/*">
      <xsl:variable name="vwordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="string(@value)"/>
          <xsl:with-param name="pDelimiters" 
                          select="', &#10;'"/>
        </xsl:call-template>
      </xsl:variable>
<!--                                                 -->
      <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
    </xsl:template>
<!--                                                 -->
    <xsl:template match="word" priority="10">
      <a href="x.asp?item={.}"><xsl:value-of select="."/></a>
    </xsl:template>
<!--                                                 -->
</xsl:stylesheet>

When the above transformation is applied on the provided XML document:

<item value="category1,category2">Item Name</item>

the wanted result is produced:

<a href="x.asp?item=category1" xmlns:ext="http://exslt.org/common">category1</a>
<a href="x.asp?item=category2" xmlns:ext="http://exslt.org/common">category2</a>

The pDelimiters parameter of this template allow multiple delimiting characters to be specified. In the above example, any separating character can be either a comma, a space or a new line character.


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

...