cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT Enhancement with Java, Tokenize Functionality

Former Member
0 Kudos

I tried the following requirement using EXSLT with no luck. Is there any other alternative to tokenize in XSLT 1.0.

The syntax i have here is

<xsl:for-each select="tokenize($this/Field1,',')">

<xsl:variable name="f1v" select="."/>

<xsl:for-each select="tokenize($this/Field2,'\|')">

<xsl:variable name="f2v" select="."/>

<xsl:for-each select="tokenize($this/Field3,'\|')">

<xsl:variable name="f3v" select="."/>

There are multiple for-each based on tokens

I am leaning towards to Enhancing XSLT with Java Function. As i have less experience with Java, can anybody help me with the code that can be used by XSLT for Tokenize functionality.

I have written something like following. I still have errors to fix. But i am not sure about having resultSet or return at the end, to make sure all the tokens were sent to XSLT.

public class MyStringToken

{

public static void main(String [] args)

{

String str = "sssd;wwer;ssswwwwdwwwwwwwwwww;wwwwe";

String delimiter = ";";

MyStringToken my = new MyStringToken();

my.getTokens(str,delimiter);

}

public void getTokens(String str,String delimiter)

{

StringTokenizer st = new StringTokenizer(str,delimiter);

while(st.hasMoreElements())

{

try

{

String delString = new String(st.nextElement().toString());

return (delString);

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

Any help is appreciated

Accepted Solutions (0)

Answers (2)

Answers (2)

jorgehuedo
Explorer
0 Kudos

Hi,

I have been searching about, and I have found the next example that maybe can help you, It's a template to tokenize in XSLT 1.0 from the web [http://stackoverflow.com/questions/1018974/tokenizing-and-sorting-with-xslt-1-0]

<xsl:template name="tokenize">

<xsl:param name="string" />

<xsl:param name="delimiter" select="' '" />

<xsl:choose>

<xsl:when test="$delimiter and contains($string, $delimiter)">

<token>

<xsl:value-of select="substring-before($string, $delimiter)" />

</token>

<xsl:text> </xsl:text>

<xsl:call-template name="tokenize">

<xsl:with-param name="string"

select="substring-after($string, $delimiter)" />

<xsl:with-param name="delimiter" select="$delimiter" />

</xsl:call-template>

</xsl:when>

<xsl:otherwise>

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

<xsl:text> </xsl:text>

</xsl:otherwise>

</xsl:choose>

</xsl:template>

Also I have found the next example of use tokenize in EXSLT from [http://exslt.org/str/functions/tokenize/index.html] It can be that inI your tests you are not using "str:tokenize", no?

<xsl:template match="a">

<xsl:apply-templates />

</xsl:template>

<xsl:template match="*">

<xsl:value-of select="." />

-

<xsl:value-of select="str:tokenize(string(.), ' ')" />

<xsl:value-of select="str:tokenize(string(.), '')" />

<xsl:for-each select="str:tokenize(string(.), ' ')">

<xsl:value-of select="." />

</xsl:for-each>

<xsl:apply-templates select="*" />

</xsl:template>

I hope that helps you.

Best.

Jorge

Former Member
0 Kudos

Chris / Jorge Huedo

The template works with out token result set using XI XML Tool Kit. In this case, i can't go with for-each loops on node-set().

And no support for Functions in XPATH 1.0 using EXSLT. So that's a no go

I have already tried with both the given examples.

Using Enhanced Java method, I have difficulties calling function and do for-each. I am trying still

jorgehuedo
Explorer
0 Kudos

Yeah it's true...well... in worst of cases you can make what you want to do inside the tokenize template, depends on you want to do.

Good luck.

Jorge

Former Member
0 Kudos

Hi,

I'd rather go for the split() method from String class, easy and powerful to use (regex patterns accepted) and it returns an array of strings : http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split%28java.lang.String%29

According to your need, you can then parse this array and add values to a ResultSet (or whatever) in your class

Or you can have a look at this thread, it provides a tokenize template that could make it : http://www.stylusstudio.com/xsllist/200603/post30080.html

Rgds

Chris