[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Dear all,
What I am trying to accomplish is to transform an account XML file using my XSL stylesheet into a CSV file with following format (please find the content of the files below):
Date; CategoryName; Recipient; Value; Notes
This works already and I want to extend the stylesheet now as follows:
- Each entry in the account file has got a category.
- Each category may have parent categories.
- If a category belongs to higher level categories the CategoryName string should built up with all categories concatenated by colons.
The category relation is stored in a 2nd XML file.
So, the CSV file would have following format:
Date; ParentCategory1:ParentCategoryN:CategoryName; Recipient; Value; Notes
All categories do have a GUID in the ID attribute. The main category does have an empty ParentID attribute whereas sub-categories do have a GUID in the ParentID attribute.
My problem is the recursive loop until all higher level categories are found and concatenated by colons.
It would be fantastic if somebody could help me in order to come up with a working stylesheet.
structure of the Categories.xml file:
--- start code
<?xml version="1.0"?>
<Categories Version="3">
<Category ID="095802e5-4d1f-45fe-998d-18c324722a02" Income="False" ParentID="">
<en default="1">Illness expenses</en>
</Category>
<Category ID="b1e5e520-50b6-4ff6-b5bc-399ab1b8908e" Income="False" ParentID="095802e5-4d1f-45fe-998d-18c324722a02">
<en default="1">Pharmacy</en>
</Category>
...
</Categories>
--- end code
my XSL stylesheet so far:
--- start code
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:template match="Account">
<xsl:apply-templates select="Entry">
<xsl:sort select="number(concat(substring(Date,7,4),substring(Date,1,2),substring(Date,4,2)))" data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Entry">
<xsl:variable name="catid" select="CategoryID"/>
<xsl:param name="parentcatid" select="document('Categories.xml')/Categories/Category[@ID=$catid]/@ParentID"/>
<xsl:param name="parentcat"><xsl:value-of select="CategoryName"/></xsl:param>
<xsl:call-template name="ParentCategories">
<xsl:with-param name="parentcatid" select="."/>
<xsl:with-param name="parentcat" select="."/>
</xsl:call-template>
<xsl:value-of select="Date"/><xsl:text>;</xsl:text><xsl:value-of select="$parentcat"/><xsl:text>;</xsl:text><xsl:value-of select="Recipient"/><xsl:text>;</xsl:text><xsl:value-of select="Value"/><xsl:text>;</xsl:text><xsl:value-of select="Notes"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template name="ParentCategories">
<xsl:param name="parentcatid" select="."/>
<xsl:param name="parentcat" select="."/>
<xsl:if test="string($parentcatid)">
<xsl:param name="parentcat"><xsl:value-of select="document('Categories.xml')/Categories/Category[@ID=$parentcatid]/de"/><xsl:text>:</xsl:text><xsl:value-of select="$parentcat"/></xsl:param>
<xsl:param name="parentcatid" select="document('Categories.xml')/Categories/Category[@ID=$parentcatid]/@ParentID"/>
[xsl] recursive loop in XSL stylesheet is failing
Subject: [xsl] recursive loop in XSL stylesheet is failing From: George <news.net@xxxxxxx> Date: Thu, 25 Mar 2010 13:33:46 +0100 |
Dear all,
What I am trying to accomplish is to transform an account XML file using my XSL stylesheet into a CSV file with following format (please find the content of the files below):
Date; CategoryName; Recipient; Value; Notes
This works already and I want to extend the stylesheet now as follows:
- Each entry in the account file has got a category.
- Each category may have parent categories.
- If a category belongs to higher level categories the CategoryName string should built up with all categories concatenated by colons.
The category relation is stored in a 2nd XML file.
So, the CSV file would have following format:
Date; ParentCategory1:ParentCategoryN:CategoryName; Recipient; Value; Notes
All categories do have a GUID in the ID attribute. The main category does have an empty ParentID attribute whereas sub-categories do have a GUID in the ParentID attribute.
My problem is the recursive loop until all higher level categories are found and concatenated by colons.
It would be fantastic if somebody could help me in order to come up with a working stylesheet.
Thank you very much in advance. Best regards, George
structure of the source Account.xml file: --- start code <?xml version="1.0"?> <Account> <Entry> <ID>4a0d255e-5829-4592-859d-4e397ba5735c</ID> <Date>02/10/2010</Date> <Recipient>Test Pharmacy, Oakland</Recipient> <CategoryID>b1e5e520-50b6-4ff6-b5bc-399ab1b8908e</CategoryID> <Value>-19.1</Value> <CategoryName>Pharmacy</CategoryName> </Entry> ... </Account> --- end code
structure of the Categories.xml file:
--- start code
<?xml version="1.0"?>
<Categories Version="3">
<Category ID="095802e5-4d1f-45fe-998d-18c324722a02" Income="False" ParentID="">
<en default="1">Illness expenses</en>
</Category>
<Category ID="b1e5e520-50b6-4ff6-b5bc-399ab1b8908e" Income="False" ParentID="095802e5-4d1f-45fe-998d-18c324722a02">
<en default="1">Pharmacy</en>
</Category>
...
</Categories>
--- end code
my XSL stylesheet so far:
--- start code
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:template match="Account">
<xsl:apply-templates select="Entry">
<xsl:sort select="number(concat(substring(Date,7,4),substring(Date,1,2),substring(Date,4,2)))" data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Entry">
<xsl:variable name="catid" select="CategoryID"/>
<xsl:param name="parentcatid" select="document('Categories.xml')/Categories/Category[@ID=$catid]/@ParentID"/>
<xsl:param name="parentcat"><xsl:value-of select="CategoryName"/></xsl:param>
<xsl:call-template name="ParentCategories">
<xsl:with-param name="parentcatid" select="."/>
<xsl:with-param name="parentcat" select="."/>
</xsl:call-template>
<xsl:value-of select="Date"/><xsl:text>;</xsl:text><xsl:value-of select="$parentcat"/><xsl:text>;</xsl:text><xsl:value-of select="Recipient"/><xsl:text>;</xsl:text><xsl:value-of select="Value"/><xsl:text>;</xsl:text><xsl:value-of select="Notes"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template name="ParentCategories">
<xsl:param name="parentcatid" select="."/>
<xsl:param name="parentcat" select="."/>
<xsl:if test="string($parentcatid)">
<xsl:param name="parentcat"><xsl:value-of select="document('Categories.xml')/Categories/Category[@ID=$parentcatid]/de"/><xsl:text>:</xsl:text><xsl:value-of select="$parentcat"/></xsl:param>
<xsl:param name="parentcatid" select="document('Categories.xml')/Categories/Category[@ID=$parentcatid]/@ParentID"/>
<xsl:call-template name="ParentCategories"> <xsl:with-param name="parentcatid" select="."/> <xsl:with-param name="parentcat" select="."/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet> --- end code
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Calculating Column Total, David Carlisle | Thread | Re: [xsl] recursive loop in XSL sty, David Carlisle |
Re: [xsl] FO checkbox from 2 inline, Kevin Brown | Date | Re: [xsl] recursive loop in XSL sty, David Carlisle |
Month |