[xsl] Insurance Transform (was XSL Tranformation!!)

Subject: [xsl] Insurance Transform (was XSL Tranformation!!)
From: cknell@xxxxxxxxxx
Date: Wed, 29 Dec 2004 10:26:49 -0500
All replies should be to the list. Questions posted on the list are there to benefit anyone who might have a similar problem. Replying off-list is bad form.

> -----Original Message-----
> From:     Kalkunda, Venkat <Kalkunda.Venkat@xxxxxxxxxxxxx>
> Sent:     Tue, 28 Dec 2004 15:37:42 -0600
> To:       <cknell@xxxxxxxxxx>
> Subject:  RE:
>
> Hi Charles,
>
> Yes your understanding of my requirement is right, this is what I am looking for except for > that, (x) is not any sequential number but the value of the <SeqNumber>(which is always > sequential). I have tried your previous solutions where you had suggested to use the 
> following rule
>
> <xsl:for-each select="Risk[@RepeatDet]">
> 
> This worked for me in the sense that, it does match only those <Risk> nodes which has 
> <RepeatDet> attribute but there is one problem. It is going through the for-each loop as 
> many times as there are <Risk> nodes with <RepeatDet> attributes, I mean, if there are 3 > <Risk> nodes with RepeatDet attributes, it iterating through the look 9 times and I have 
> ExistingProductType, ExistingPolNumber....... .etc. 9 times instead of just 3 times.  What 
> could be the reason?

There are two reasons:
1) You have written the for-each loop incorrectly
2) You shouldn't be using a for-each loop in the first place


We'll chalk up reason #2 to the fact that you are used to programming in a procedural language (i.e., Java). You will find XSL much easier if you let the processor work out the details of how to get things done. In this way, XSL is like SQL. You tell the database engine what you want and let the DBMS figure out the best way to get it. You wouldn't try to retrieve data from a database by writing a program to open two tables inside nested for-each loops and pull out data with matching columns. You would simply tell the database

SELECT FROM tbl_1 where tbl_1.SSN = tbl_2.SSN;

in order to pull all the rows from tbl_1 that have a value in the SSN column that matches a row in tbl_2 with the same value in the SSN column.

Given that, here is a stylesheet that does what you want. Before I give it to you though, there are a couple of things to point out.

1) <XML> is not a legal element name as "XML" is reserved. I changed your document element name to "doc" to clear this up.

2) I couldn't figure out why you put the xmlns="" attribute in each <field> element, so I left it out. If there is a good reason for having it there, I'm sure you'll figure out how to get it back.

3) I don't see the point of having a <value> child node inside each <field> element (although I left it there). Wouldn't your life be simpler if you eliminated the <value> element altogether and put its value in the <field> element?

insurance.xsl
==========
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" indent="yes" encoding="UTF-8" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="doc">
    <fields>
      <xsl:apply-templates />
    </fields>
  </xsl:template>

  <xsl:template match="PFG">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="IDI">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="DiPolicy">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="Roles">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="Insured">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="Party">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="Risk[not(@RepeatDet)]" />
  <xsl:template match="SeqNumber" />
  <xsl:template match="ExistingInsurancePaidBy" />

  <xsl:template match="Risk[@RepeatDet]">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="ExistingProductType">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingProductType{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ExistingPolNumber">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingPolNumber{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ExistingInsuranceAmt">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingInsuranceAmt{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ExistingInsuranceName">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingInsuranceName{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ExistingInsurancePending">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingInsurancePending{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ExistingInsuranceEP">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingInsuranceEP{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ExistingInsuranceInd">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingInsuranceInd{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ExistingInsuranceBP">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ExistingInsuranceBP{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

  <xsl:template match="ReplacementType">
    <xsl:variable name="sn" select="preceding-sibling::SeqNumber[1]" />
    <field name="ReplacementType{$sn}"><value><xsl:value-of select="." /></value></field>
  </xsl:template>

</xsl:stylesheet>


-- 
Charles Knell
cknell@xxxxxxxxxx - email

Current Thread