xsl-list
[Top] [All Lists]

[xsl] recursive loop in XSL stylesheet is failing

2010-03-25 07:34:17
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[(_at_)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>&#xa;</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[(_at_)ID=$parentcatid]/de"/><xsl:text>:</xsl:text><xsl:value-of select="$parentcat"/></xsl:param> <xsl:param name="parentcatid" select="document('Categories.xml')/Categories/Category[(_at_)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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--