|
next
|
Subject: Formatting data after normalising the space? Author: Johnny Breen Date: 03 May 2005 10:01 AM
|
Hi I have an xslt query that I hoped someone could help me with.
Suppose I have the following xml data:
<?xml version="1.0"?>
<testthis>
<words name="alphabet">
<chars name="abc"/>
<chars name="def"/>
</words>
<words name="nums">
<chars name="123"/>
<chars name="456"/>
</words>
<words name="others">
<chars name="try"/>
<chars name="this"/>
</words>
</testthis>
If I use the following template on the data:
<xsl:template name="getArrays">
{<xsl:for-each word>
{
<xsl:for-each select="./chars">
<xsl:value-of select="./@name"/>,
<xsl:for-each>
},
</xsl:for-each>};
</xsl:template>
I would get an output something like this:
{
{abc,def,},
{123,456,},
{try,this,},};
So to strip out the trailing commas I did something like as follows:
<xsl:template name="getArrays">
{<xsl:variable name="itemListOutSide">
<xsl:for-each select="testthis/words">
{<xsl:variable name="itemListInside">
<xsl:for-each select="./chars">
<xsl:value-of select="./@name"/>,
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="substring(normalize-space($itemListInside),0,
string-length(normalize-space($itemListInside))) "/>
},</xsl:for-each>
</xsl:variable>
<xsl:value-of select="substring(normalize-space($itemListOutside),0, string-length(normalize-space($itemListOutside)))"/>};
</xsl:template>
which gave me the correct output:
{{abc,def},{123,456},{try,this}};
but the normalize-space I needed to extract the trailing comma means the output is always stuck together on one line, what I need to do is ensure that each array is on a seperate line like this:
{{abc,def},
{123,456},
{try,this}};
Is there anyway to do this with the approach I am using here, or if not does anyone have any other suggestions as to how I should use a different approach to this problem.
It is necessary for me to output in this way as the arrays i'm working with are really large.
Any help would be appreciated.
Thanks,
Johnny
|
|
|
|