2

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
balaji
  • 1,075
  • 3
  • 12
  • 26
  • Can you provide a sample of your source XML and the desired output please? Given your information only general answers can be given. Do you want a general direction? – Emiliano Poggi May 10 '11 at 09:37
  • @balaji: Note you can [format lines as code](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) by indenting them four spaces. This is particularly necessary for XML and HTML, otherwise they won't be visible. The "{}" button in the editor toolbar does this for you. Empo did it for you this time; try it yourself the next. Click the orange question mark in the editor toolbar for more information and tips on formatting. – outis May 10 '11 at 09:48
  • @empo i have done chnages in question .... can u tell now how to resolve issue. Thanks – balaji May 10 '11 at 09:50
  • Possible [duplicate](http://stackoverflow.com/questions/1018974/tokenizing-and-sorting-with-xslt-1-0)? – Emiliano Poggi May 10 '11 at 10:03
  • @balaji: I answered this for your previous question -- this is indeed a duplicate. – Dimitre Novatchev May 10 '11 at 13:27
  • possible duplicate of [how to handle array of strings in ".xsl" file?](http://stackoverflow.com/questions/5945430/how-to-handle-array-of-strings-in-xsl-file) – Dimitre Novatchev May 10 '11 at 13:28

1 Answers1

1

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.

Community
  • 1
  • 1
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67