[xsl] Using result of one template in another

Subject: [xsl] Using result of one template in another
From: "Rasoul Hajikhani" <Rasoul.Hajikhani@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 31 May 2006 10:03:31 -0700
Hi there,

I am new to XSLT and need some help with my translation. I have an XML
doc that needs to have its element names changed and then re-structured
based on come criteria. The criteria is category names. I need to create
new elements that group elements based on common categories. Here is my
XSLT doc:



<xsl:stylesheet version="2.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

xmlns:els="http://www.consumerdirect.com/namespaces";>

            <xsl:import href="copy.xslt"/>

            <xsl:variable name="cat" select="/*/*/@cat[not(. =
../preceding-sibling::*/@cat)]"/>

            <!-- Define the mapping rules -->

            <xsl:variable name="mapping-rules"
select="document('UoPToELSMapping.xslt')"/>

            <!-- Load the lookup table. We define it locally but it can
also come from an external file -->

            <xsl:variable name="lookup"
select="$mapping-rules/*[els:*]"/>

            <xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>

            <xsl:template match="/">

                        <xsl:apply-templates mode="sub"/>

                         <xsl:apply-templates mode="grouper"/>

            </xsl:template>

            <xsl:template match="*" mode="sub">

                        <xsl:choose>

                                    <xsl:when
test="$lookup/els:element[@from=name(current())]">

                                                <xsl:element
name="{$lookup/els:element[@from=name(current())]/@to}">


<xsl:attribute name="cat">


<xsl:value-of select="$lookup/els:element[@from=name(current())]/@cat"/>


</xsl:attribute>


<xsl:attribute name="ids">


<xsl:value-of select="$lookup/els:element[@from=name(current())]/@ids"/>


</xsl:attribute>

                                                            <!-- applies
to all of the attributes -->


<xsl:apply-templates select="@*"/>


<xsl:apply-templates/>

                                                </xsl:element>

                                    </xsl:when>

                                    <!-- Copies the values of the nodes
-->

                                    <xsl:otherwise>

                                                <xsl:apply-imports/>

                                    </xsl:otherwise>

                        </xsl:choose>

            </xsl:template>



            <xsl:template match="*" mode="grouper">

            <xsl:for-each select="$cat">

                        <xsl:variable name="cat-name" select="."/>

                                    <xsl:element name="{$cat-name}">

                                    <xsl:for-each
select="/*/*[@cat=$cat-name]">

                                                <xsl:copy>


<xsl:apply-templates select="."/>

                                                </xsl:copy>

                                    </xsl:for-each>

                                    </xsl:element>

            </xsl:for-each>

            </xsl:template>



</xsl:stylesheet>



And my mapper is:



<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

 xmlns:els="http://www.consumerdirect.com/namespaces";>

<!-- Define mapping rules -->

<els:element from="Person_Names_First_Name" to="FIRST_NAME"
cat="LEAD_TRANS_DETAILS"/>

<els:element from="Person_Names_Last_Name" to="LAST_NAME"
cat="LEAD_TRANS_DETAILS"/>

<els:element from="Address_Home_Street" to="STREET1" cat="LEAD_ADDRESS"
type="Home" ids="1"/>

<els:element from="Address_Home_Line2" to="STREET2" cat="LEAD_ADDRESS"
type="Home" ids="1"/>

<els:element from="Address_Home_City" to="CITY" cat="LEAD_ADDRESS"
type="Home" ids="1"/>

<els:element from="Address_Home_State_Prov" to="STATE"
cat="LEAD_ADDRESS" type="Home" ids="1"/>

<els:element from="Address_Home_Country" to="COUNTRY" cat="LEAD_ADDRESS"
type="Home" ids="1"/>

<els:element from="Address_Home_Postal_Code" to="ZIP" cat="LEAD_ADDRESS"
type="Home" ids="1"/>

<els:element from="Address_Business_Street" to="STREET1"
cat="LEAD_ADDRESS" type="Business" ids="2"/>

<els:element from="Address_Business_Line2" to="STREET2"
cat="LEAD_ADDRESS" type="Business" ids="2"/>

<els:element from="Address_Business_City" to="CITY" cat="LEAD_ADDRESS"
type="Business" ids="2"/>

<els:element from="Address_Business_State_Prov" to="STATE"
cat="LEAD_ADDRESS" type="Business" ids="2"/>

<els:element from="Address_Business_Country" to="COUNTRY"
cat="LEAD_ADDRESS" type="Business" ids="2"/>

<els:element from="Address_Business_Postal_Code" to="ZIP"
cat="LEAD_ADDRESS" type="Business" ids="2"/>

<els:element from="Phone_Home_Country" to="Phone_Home_Country"
cat="LEAD_PHONE" type="Home" ids="3"/>

<els:element from="Phone_Home_Area" to="Phone_Home_Area"
cat="LEAD_PHONE" type="Home" ids="3"/>

<els:element from="Phone_Home_Number" to="PHONE_NUMBER" cat="LEAD_PHONE"
type="Home" ids="3"/>

<els:element from="Phone_Business_Country" to="Phone_Business_Country"
cat="LEAD_PHONE" type="Business" ids="4"/>

<els:element from="Phone_Business_Area" to="Phone_Business_Area"
cat="LEAD_PHONE" type="Business" ids="4"/>

<els:element from="Phone_Business_Number" to="PHONE_NUMBER"
cat="LEAD_PHONE" type="Business" ids="4"/>

<els:element from="Phone_Business_Extension"
to="Phone_Business_Extension" cat="LEAD_PHONE" type="Business" ids="4"/>

<els:element from="Email_Address_Home_Email" to="LEAD_EMAIL_ADDRESS"
cat="LEAD_EMAIL" type="Home" ids="5"/>

<els:element from="Email_Address_Business_Email" to="LEAD_EMAIL_ADDRESS"
cat="LEAD_EMAIL" type="Business" ids="6"/>

<els:element from="Std_Primary_Lead_Source" to="Std_Primary_Lead_Source"
cat="LEAD_INFO_DETAIL"/>

<els:element from="Create_Date" to="Create_Date"
cat="LEAD_INFO_DETAIL"/>

<els:element from="Actual_Program" to="Actual_Program"
cat="LEAD_INFO_DETAIL"/>

<els:element from="Program_of_Interest" to="Program_of_Interest"
cat="LEAD_INFO_DETAIL"/>

<els:element from="Enrollment_Status" to="Enrollment_Status"
cat="LEAD_INFO_DETAIL"/>

<els:element from="Std_Orga_Number" to="Std_Orga_Number"
cat="LEAD_INFO_DETAIL"/>

<els:element from="Site_ids" to="Site_ids" cat="LEAD_INFO_DETAIL"/>

</xsl:stylesheet>



However, I do not get the desired effect. I can successfully substitute
the element names, but grouping part never behaves the way that I need
it.

I appreciate any help that you can provide.

Thanks.

-r

Current Thread