开发者

how to handle sequence OR array of strings in ".xsl" file?

I have sequence/array of string in xsl stlesheet.

e.g varList when I print the value of above variable as following

<xsl:value-of select="$varList"/>

It prints as follows which is

varList="hw.co.gdh gd.kj.xhd bh.ko.sag hf.sj.kjh"

Now I have to get each string separatly from the above varibale.开发者_运维百科

i.e "hw.co.gdh" , "gd.kj.xhd" separeted.

How I can do it? is there any option of applying <xsl:for-each> loop or somthing else?

I m using version="1.0" of xsl.


I show you a demonstration on how to split the strings in XSLT 1.0 based on recursive function provided here.

INPUT

<root>
 <varlist>a.a.a b.b.b c.c.c</varlist>
</root>

XSL

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/root">
  <xsl:variable name="varList" select="varlist" />
  <xsl:variable name="nodelist">
   <xsl:call-template name="output-tokens">
    <xsl:with-param name="list"><xsl:value-of select="$varList"/></xsl:with-param>
   </xsl:call-template>
  </xsl:variable>
  <nodelist>
   <xsl:copy-of select="$nodelist"/>
  </nodelist>
 </xsl:template>

<xsl:template name="output-tokens">
 <xsl:param name="list" /> 
  <xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" /> 
  <xsl:variable name="first" select="substring-before($newlist, ' ')" /> 
  <xsl:variable name="remaining" select="substring-after($newlist, ' ')" /> 
 <id>
     <xsl:value-of select="$first" /> 
 </id>
 <xsl:if test="$remaining">
    <xsl:call-template name="output-tokens">
            <xsl:with-param name="list" select="$remaining" /> 
    </xsl:call-template>
 </xsl:if>
</xsl:template>

OUTPUT

<nodelist>
 <id>a.a.a</id>
 <id>b.b.b</id>
 <id>c.c.c</id>
</nodelist>

NOTE that I didn't do anything more than calling the template output-tokens.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜