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.

6
  • 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? Commented May 10, 2011 at 9:37
  • @balaji: Note you can format lines as code 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. Commented May 10, 2011 at 9:48
  • @empo i have done chnages in question .... can u tell now how to resolve issue. Thanks Commented May 10, 2011 at 9:50
  • Possible duplicate? Commented May 10, 2011 at 10:03
  • @balaji: I answered this for your previous question -- this is indeed a duplicate. Commented May 10, 2011 at 13:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.