XSL problem

Here should go questions about transforming XML with XSLT and FOP.
raydenl
Posts: 1
Joined: Tue Dec 21, 2004 4:40 am

XSL problem

Post by raydenl » Tue Dec 21, 2004 4:55 am

Hi, I have xml data as such:

Code: Select all


<x:Series>
<x:Data>Main</x:Data>
<x:Category>
<x:Data>{"a","b","c","d","e","f"}</x:Data>
</x:Category>
<x:Value>
<x:Data>{1,2,3,4,5,6}</x:Data>
</x:Value>
</x:Series>
I want to end up being able to export the data to:

Main, a, 1
Main, b, 2
Main, c, 3
Main, d, 3


Any ideas?

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Dec 21, 2004 12:38 pm

Hi,

First, your document is not wellformed, the prefix x is not bound to a namespace. Assuming as input:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<x:Series xmlns:x="http://test.com">
<x:Data>Main</x:Data>
<x:Category>
<x:Data>{"a","b","c","d","e","f"}</x:Data>
</x:Category>
<x:Value>
<x:Data>{1,2,3,4,5,6}</x:Data>
</x:Value>
</x:Series>
then you can use a named template to output the current line and call it recursivelly for the remainder, something like below:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://test.com">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:call-template name="print">
<xsl:with-param name="main" select="x:Series/x:Data"/>
<xsl:with-param name="categories" select="translate(x:Series/x:Category/x:Data, '{ }"', '')"/>
<xsl:with-param name="values" select="translate(x:Series/x:Value/x:Data, '{ }', '')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="print">
<xsl:param name="main"/>
<xsl:param name="categories"/>
<xsl:param name="values"/>
<xsl:if test="string-length($categories) > 0">
<xsl:choose>
<xsl:when test="contains($categories, ',')">
<xsl:value-of select="$main"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="substring-before($categories, ',')"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="substring-before($values, ',')"/>
<xsl:text xml:space="preserve">
</xsl:text>
<xsl:call-template name="print">
<xsl:with-param name="main" select="$main"/>
<xsl:with-param name="categories" select="substring-after($categories, ',')"/>
<xsl:with-param name="values" select="substring-after($values, ',')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$main"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="$categories"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="$values"/>
<xsl:text xml:space="preserve">
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:

Code: Select all


Main, a, 1
Main, b, 2
Main, c, 3
Main, d, 4
Main, e, 5
Main, f, 6
Best Regards,
George

Post Reply