Re: [xsl] Str:tokenize modification to change the <token> tag

Subject: Re: [xsl] Str:tokenize modification to change the <token> tag
From: "Vasu Chakkera" <vasucv@xxxxxxxxxxx>
Date: Wed, 17 Apr 2002 16:09:30 +0000
Hi Naresh. I know i am very late in answering your question. You might have already found your answer with the XSL gurus in this list.
I However want to post a solution that i had done earlier
The following is my solution
1. Write a Java Class and add the following method in it


public Node getTokenizedNode(String str, String delim) {
System.out.println(str);
StringTokenizer strTokeniser = new StringTokenizer(str, delim, false);
Document doc = null;
Element tokens = null;
// Create Document object


try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
tokens = doc.createElement("tokens");
while (strTokeniser.hasMoreTokens()) {
String token = strTokeniser.nextToken();
System.out.println(token);
Element tokenTag = doc.createElement("token");
tokenTag.appendChild(doc.createTextNode(token));
tokens.appendChild(tokenTag);
}


       } catch (ParserConfigurationException pce) {
           pce.printStackTrace();
       }
       catch(TransformerConfigurationException tce)
       {
        tce.printStackTrace();
       }
       catch(TransformerException te)
       {
        te.printStackTrace();
       }
       System.out.println("before returning");
       return tokens;
   }

If your delimited String is "A,B,C,D"
( here delimiter is comma , it could be anything.. and is in your control from XSL Sheet )
This Method will return to the XSL the XML element tokens , which is of the structure
<tokens>
<token></token>
<token></token>
<token></token>
<token></token>
</tokens>


For an XML File like below
<delimitedStrings>
<delimitedString delim = "|">
pipe1|pipe2|pipe3|pipe4
</delimitedString>

<delimitedString delim = ",">
comma1,comma2,comma3,comma4
</delimitedString>

<delimitedString delim = ":" >
colon1:colon2:colon3:colon4
</delimitedString>

</delimitedStrings>

Now your XSL sheet would look like this

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xalan = "http://xml.apache.org/xalan"; exclude-result-prefixes="xalan"
xmlns:myclass= "myPackage.myClass" extension-element-prefixes ="myclass">
<xsl:template match="/">


<xsl:apply-templates select="//delimitedString"/>
</xsl:template>

<xsl:template match="delimitedString">
<xsl:variable name="delimString" select="."/>
<xsl:variable name="delim" select="./@delim"/>
<!-- call your java method passing the String and the delimiter -->
<xsl:apply-templates select="myclass:getTokenizedNode($delimString,$delim)"/>
</xsl:template>


<xsl:template match="//tokens">

<xsl:apply-templates select="token"/>
</xsl:template>

<xsl:template match="token">
<!-- Here You have them with you -->
<xsl:value-of select="."/> <!-- Each token with you -->
<xsl:text>&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>

your text output.


pipe1 pipe2 pipe3 pipe4


comma1 comma2 comma3 comma4


colon1 colon2 colon3 colon4

The above method would give you good understanding of the JAVA-XSL communication ... and you can create your own classes to do utility stuff like this and use them. Please note that in the XSL code above the namespace declaration is important
xmlns:myclass= "myPackage.myClass" extension-element-prefixes ="myclass"
myPackage is the package of your class and
myClass is the class that defines the above method..
you might then want to add a functionality that would allow ur XSL to read from a file and display its contents. so you might want to then add a method to the same class like below
public static String getFileContents(String fileName) throws IOException {


FileInputStream fi = new FileInputStream(fileName);
StringBuffer buf = new StringBuffer();
int fileSize = fi.available();
for (int i = 0; i < fileSize; i++) {
buf.append((char) fi.read());
}
//*** read operation
fi.close();
return buf.toString();
}
and in the XSL where you would want to display the file contents , just call the xsl:value-of like


<xsl:value-of disable-output-escaping="yes" select ="myclass:getFileContents('c:/myfile.txt)"/>

this would print the file contents to your output..
just keep adding the functionality u want in your java code and use them in your XSL.


Hope this helps
Vasu

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread