Re: [xsl] Help Needed On Re-Organizing Structure

Subject: Re: [xsl] Help Needed On Re-Organizing Structure
From: omprakash.v@xxxxxxxxxxxxx
Date: Thu, 20 Apr 2006 19:09:57 +0530
Hi,
    Please see this stylesheet. Couldn't gather the correct requirements
from your mail. But this does something close enough to what you have asked
for. More specifically,  the role of your field having name like
LegalEntity, Class etc is not evident.


<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
             <xsl:output method="xml" encoding="utf-8"/>

             <!--Look in the Results-->
             <xsl:template match="results">
                         <tree>
                                     <!--First Find All The Portfolios-->
                                     <xsl:apply-templates
select="record/field[name='EntityType' and value='Portfolio']"/>
                         </tree>
             </xsl:template>


             <!--Process The Portfolios-->
             <xsl:template match="record/field[name='EntityType' and
value='Portfolio']">


                         <xsl:variable name="PortfolioID"
select="following-sibling::*[name='ID']/value"/>
                         <xsl:variable name="PortfolioDescription"
select="following-sibling::*[name='Description']/value"/>
                         <xsl:variable name="PortfolioCurrency"
select="following-sibling::*[name='Currency']/value"/>
                         <Entity>
                                     <xsl:attribute name="id">
                                                 <xsl:value-of
select="$PortfolioID"/>
                                     </xsl:attribute>
                                     <description>
                                                 <xsl:value-of
select="$PortfolioDescription"/>
                                     </description>
                                     <currency>
                                                 <xsl:value-of
select="$PortfolioCurrency"/>
                                     </currency>
                                     <contents>


            <xsl:apply-templates
select="../following-sibling::record[1]/field[not(name() = 'EntityType' and
value='Portfolio')]" mode="inside"/>

                                     </contents>

                         </Entity>


             </xsl:template>


                         <xsl:template match="record/field" mode="inside">



            <xsl:choose>
            <xsl:when test="name = 'EntityType' and (value='LegalEntity' or
value='Class' or value='InvestingFeeder')">

                  <xsl:apply-templates
select="following-sibling::field[1][not(name = 'EntityType' and
value='Portfolio')]" mode="inside"/>


            </xsl:when>
            <xsl:otherwise>


                         <xsl:variable name="PoID" select="value"/>
                         <xsl:variable name="PoDesc"
select="following-sibling::*[name='Description']/value"/>
                         <xsl:variable name="PoCurr"
select="following-sibling::*[name='Currency']/value"/>

       <Entity>
                                     <xsl:attribute name="id">
                                                 <xsl:value-of
select="$PoID"/>
                                     </xsl:attribute>
                                     <description>
                                                 <xsl:value-of
select="$PoDesc"/>
                                     </description>
                                     <currency>
                                                 <xsl:value-of
select="$PoCurr"/>
                                     </currency>



                         </Entity>


            <xsl:apply-templates
select="../following-sibling::record[1]/field[1][not(name = 'EntityType'
and value='Portfolio')]" mode="inside"/>

            </xsl:otherwise>
            </xsl:choose>



             </xsl:template>

</xsl:stylesheet>


Hope this helps.


cheers.
prakash





                                                                                                                                        
                      "Charles Gillanders"                                                                                              
                      <charlesgillanders@10tot         To:      <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>                                       
                      he18th.com>                      cc:      (bcc: omprakash.v/Polaris)                                              
                                                       Subject: [xsl] Help Needed On Re-Organizing Structure                            
                      04/20/2006 04:43 PM                                                                                               
                      Please respond to                                                                                                 
                      xsl-list                                                                                                          
                                                                                                                                        
                                                                                                                                        




Hi,

This is my first post to the list, I'm trying to get my head around
using XSLT to re-structure or re-organize an existing XML document.

My xml document comes from an existing 3rd party tool which I cannot
control and I cannot change the output from the tool.  I need to get the
XML into a different format for feeding into yet another 3rd party tool
again which has a fixed format!

The source format has an implied hierarchy in that there is a top level
"record" followed by a number of secondary records (and possibly further
levels under that) until the same top level record is encountered
further along in the document at which point the nodes after that point
are supposed to be considered to be children of that new top level
record type and so-on until the end of the document.

The destination format needs to have this hierarchy explicitly stated.

I've been trying to get my head around the examples in the O'Reilly XSLT
cookbook however I'm getting stuck....

I've managed to figure out code that will produce my top level
hierarchies correctly but I really don't know where to go from here
and/or if I'm going about this in the wrong way...

I'd really appreciate any help anyone can give me at this point.  I've
included my existing code and examples of the input and output formats
that I'm working with below....

Thanks (hopefully),

Charles





/* Existing Code that Partially Works */

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
             <xsl:output method="xml" encoding="utf-8"/>

             <!--Look in the Results-->
             <xsl:template match="results">
                         <tree>
                                     <!--First Find All The Portfolios-->
                                     <xsl:apply-templates
select="record/field[name='EntityType' and value='Portfolio']"/>
                         </tree>
             </xsl:template>


             <!--Process The Portfolios-->
             <xsl:template match="record/field[name='EntityType' and
value='Portfolio']">
                         <xsl:variable name="PortfolioID"
select="following-sibling::*[name='ID']/value"/>
                         <xsl:variable name="PortfolioDescription"
select="following-sibling::*[name='Description']/value"/>
                         <xsl:variable name="PortfolioCurrency"
select="following-sibling::*[name='Currency']/value"/>
                         <Entity>
                                     <xsl:attribute name="id">
                                                 <xsl:value-of
select="$PortfolioID"/>
                                     </xsl:attribute>
                                     <description>
                                                 <xsl:value-of
select="$PortfolioDescription"/>
                                     </description>
                                     <currency>
                                                 <xsl:value-of
select="$PortfolioCurrency"/>
                                     </currency>
                                     <contents>
                                     </contents>
                         </Entity>
             </xsl:template>


</xsl:stylesheet>

/* XML Input */

<?xml version="1.0" encoding="utf-8"?><reportResultsPortfolioStruct>
  <results>
    <record>
      <field>
        <name>EntityType</name>
        <value>Portfolio</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>LegalEntity</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1EUR</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - Euro Feeder Fund</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>EUR</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1EURClassA</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - EUR Feeder Fund - Class A
Ordinary Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>EUR</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1EURClassB</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - EUR Feeder Fund - Class B
Management Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>EUR</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>LegalEntity</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1JPY</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - Japanese Yen Feeder
Fund</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>JPY</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1JPYClassA</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - JPY Feeder Fund - Class A
Ordinary Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>JPY</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1JPYClassB</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - JPY Feeder Fund - Class B
Management Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>JPY</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>LegalEntity</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1Master</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - Master Fund</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1MasterClass</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - Master Class</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>InvestingFeeder</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1EUR</value>
      </field>
      <field>
        <name>Description</name>
        <value>zCGTest1 Master to EUR Investing Feeder</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>InvestingFeeder</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1JPY</value>
      </field>
      <field>
        <name>Description</name>
        <value>zCGTest1Master to JPY Investing Feeder</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>InvestingFeeder</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1USD</value>
      </field>
      <field>
        <name>Description</name>
        <value>zCGTest1Master to USD Investing Feeder</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>LegalEntity</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1USD</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - USD Feeder Fund</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1USDClassA</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - USD Feeder Fund - Class A
Ordinary Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest1USDClassB</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 1 - USD Feeder Fund - Class B
Management Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Portfolio</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest2</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 2</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>LegalEntity</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest2Standalone</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 2 - Standalone</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest2USDClassA</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 2 -  Class A Ordinary
Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest2USDClassB</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 2 -  Class B Management
Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Portfolio</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest3</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 3</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>LegalEntity</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest3Feeder</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 3 - Feeder</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Class</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest3USDClassA</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 3 -  Class A Ordinary
Shares</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Series</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest3USDClassASeries1</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 3 -  Class A Ordinary Shares -
Series 1</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>LegalEntity</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest3Master</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 3 - Master</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
    <record>
      <field>
        <name>EntityType</name>
        <value>Series</value>
      </field>
      <field>
        <name>ID</name>
        <value>zCGTest3USDClassASeries1</value>
      </field>
      <field>
        <name>Description</name>
        <value>CG Test Portfolio Number 3 -  Class A Ordinary Shares -
Series 1</value>
      </field>
      <field>
        <name>CurrencyCode</name>
        <value>USD</value>
      </field>
    </record>
  </results>
</reportResultsPortfolioStruct>

/* Exmaple Required XML Output */

<?xml version="1.0"?>
<tree>
             <entity id="zCGTest1">
                         <description>CG Test Portfolio 1</description>
                         <currency>USD</currency>
                         <contents>
                                     <entity id="zCGTest1Master">

<portfolio>zCGTest1</portfolio>
                                                 <description>CG Test
Portfolio 1 Master
Fund</description>
                                                 <currency>USD</currency>
                                                 <contents></contents>
                                     </entity>
                                     <entity id="zCGTest1USDFeeder">

<portfolio>zCGTest1</portfolio>
                                                 <description>CG Test
Portfolio 1 USD
Feeder Fund</description>

<image>images/paper.gif</image>
                                                 <currency>USD</currency>
                                                 <contents></contents>
                                     </entity>
                                     <entity id="zCGTest1EURFeeder">

<portfolio>zCGTest1</portfolio>
                                                 <description>CG Test
Portfolio 1 EUR
Feeder Fund</description>
                                                 <currency>EUR</currency>
                                                 <contents></contents>
                                     </entity>
                                     <entity id="zCGTest1JPYFeeder">

<portfolio>zCGTest1</portfolio>
                                                 <description>CG Test
Portfolio 1 JPY
Feeder Fund</description>
                                                 <currency>JPY</currency>
                                                 <contents></contents>
                                     </entity>
                         </contents>
             </entity>
             <entity id="zCGTest2">
                         <description>CG Test Portfolio 2</description>
                         <currency>GBP</currency>
                         <contents>
                                     <entity id="zCGTest02Master">

<portfolio>zCGTest2</portfolio>
                                                 <description>CG Test
Portfolio 2
Standalone Fund</description>
                                                 <currency>GBP</currency>
                                                 <contents>
                                                 <entity
id="zCGTest2MasterClassA">

<portfolio>zCGTest2</portfolio>
                                                 <description>CG Test
Portfolio 2 Class A
Ordinary Shares</description>
                                                 <currency>GBP</currency>
                                                 <contents></contents>
                                     </entity>
                                     <entity id="zCGTest2MasterClassB">

<portfolio>zCGTest2</portfolio>
                                                 <description>CG Test
Portfolio 2 Class B
Management Shares</description>
                                                 <currency>GBP</currency>
                                                 <contents></contents>
                                     </entity>
                         </contents>
             </entity>
</contents>
</entity>
<entity id="zCGTest3">
<description>CG Test Portfolio 3</description>
<currency>EUR</currency>
<contents>
             <entity id="zCGTest3Master">
                         <portfolio>zCGTest3</portfolio>
                                     <description>CG Test Portfolio 3
Master
Fund</description>
                                     <currency>EUR</currency>
                                     <contents></contents>
                         </entity>
                         </contents>
             </entity>
</tree>





POLARIS, THE LEADER IN SPECIALITY APPLICATION DEVELOPMENT :
-------------------------------------------------------------

Polaris has been awarded the Leader in the category of "Speciality Application Development" among the Top 100 global Companies from Cyber Media Publications for the Year 2006.

--------------------------------------------------------------

This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. 
If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit Us at http://www.polaris.co.in

Current Thread