Subject: Re: [xsl] Tokenize comma separated string in XSL From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx> Date: Wed, 21 Mar 2007 22:27:18 +0530 |
Abel rightly said, your desired output is not well-formed XML (so it cannot be consumed by any XML 1.x complaint tool / parser). Assuming that is an oversight on your part, here is the solution which Abel outlined:
<xsl:template match="/"> <AllCodes> <xsl:call-template name="tokenize"> <xsl:with-param name="string" select="$string" /> <xsl:with-param name="delim" select="','" /> </xsl:call-template> </AllCodes> </xsl:template>
<xsl:template name="tokenize"> <xsl:param name="string" /> <xsl:param name="delim" />
<xsl:choose> <xsl:when test="contains($string, $delim)"> <Code value="{substring($string, 1, 1)}"> <Description><xsl:value-of select="substring-before($string, $delim)" /></Description> </Code> <xsl:call-template name="tokenize"> <xsl:with-param name="string" select="substring-after($string, $delim)" /> <xsl:with-param name="delim" select="$delim" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <Code value="{substring($string, 1, 1)}"> <Description><xsl:value-of select="$string" /></Description> </Code> </xsl:otherwise> </xsl:choose> </xsl:template>
<?xml version="1.0" encoding="UTF-8"?> <x/>
<?xml version="1.0" encoding="UTF-8"?> <AllCodes> <Code value="A"> <Description>ABC</Description> </Code> <Code value="D"> <Description>DEF</Description> </Code> <Code value="G"> <Description>GHI</Description> </Code> </AllCodes>
java org.apache.xalan.xslt.Process -in test.xml -xsl test.xsl -PARAM string ABC,DEF,GHI
On 3/21/07, Lalit.Chanchlani@xxxxxxxxxxxxxxxxxx <Lalit.Chanchlani@xxxxxxxxxxxxxxxxxx> wrote:
Hi,
I am using XSLT 1.0 In the XSL I get the comma separated string ABC,DEF,GHI as a parameter which is set by a Java program.
The output XML should be:
<AllCodes> <Code value = "A"> <Description="ABC"/> </Code> <Code value = "D"> <Description="DEF"/> </Code> <Code value = "G"> <Description="GHI"/> </Code> </AllCodes>
Can you please give me an example on the recursive template using substring-before and substring-after?
Thanks, Lalit
-----Original Message----- From: Abel Braaksma [mailto:abel.online@xxxxxxxxx] Sent: Wednesday, March 21, 2007 4:50 PM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] Tokenize comma separated string in XSL
Lalit.Chanchlani@xxxxxxxxxxxxxxxxxx wrote: > Hi All, > I have a comma-separated string like ABC,DEF,GHI which I want to > tokenize and return to a template which retrieves separately its > corresponding code from a XML input. > What version of XSLT are you using?
In XSLT 2, this is:
<xsl:for-each select="tokenize($your_input, ',')" > <Description value="{.}" /> </xsl:for-each>
In XSLT 1, use a recursive template call with substring-before and substring-after to "walk" through the string.
> The output should be: > > <AllCodes> > <Code value = "A"> > <Description="ABC"> > your output is not valid XML and as such is not (easily) possible with XSLT. I assume you meant something else?
Cheers, -- Abel Braaksma http://www.nuntia.nl
-- Regards, Mukul Gandhi
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Tokenize comma separated , Abel Braaksma | Thread | [xsl] Naming the Column head based , jeb501 |
Re: [xsl] Question Regarding the di, David Carlisle | Date | RE: [xsl] Applying Attributes with , Michael Kay |
Month |