[xsl] Keys with different parents, but same name

Subject: [xsl] Keys with different parents, but same name
From: Ganesh Sindigi <ganeshaks@xxxxxxxxx>
Date: Fri, 24 Oct 2003 13:24:01 -0700 (PDT)
Hello,

I'm a bit stuck in usage of XSLT lookup tables using
xsl:key.  
I'm sure someone on the list can help me or direct me
in the right direction.

I have created a stylesheet which would render the xml
document by picking up the 
labels for the fields from a separate document.  The
'field' is used as key.  
These 'field' elements could be contained within group
so they are repeatable e.g. field@id = city, but label
for them would be different 
depending upon the group they are in like in this case
for home address it is called City and for company
address its Location.

The problem i am facing is I am always getting the
first 'field' element.  The way i have done could be
wrong.  

I tried looking into all the archives, but could not
find anything related to this.  

How do I get the label related to field element wrt
its parent.  Any Help would be greatly appreciated.  

Thanks,
Ganesh.

Here is my styleSheet:

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

   <xsl:key name="field-lookup" match="field"
use="@id"/>

   <xsl:key name="fieldGroup-lookup"
match="fieldGroup" use="@id"/>

   <xsl:variable name="fieldHeaders"
select="document('Headers.xml')/fieldHeaders"/>


   <xsl:template match="/">
      <html>
         <style type="text/css">
            body {color:black; background-color:
white}
         </style>
         <body>
            <table border="1">
               <xsl:apply-templates/>
            </table>
         </body>
      </html>
   </xsl:template>

   <xsl:template match="customer">
      <br />
       <table border="1"> <xsl:apply-templates />
</table>
   </xsl:template>
   <xsl:template match="fieldGroup">
      <TR>
         <TD colspan="2" bgcolor="CCAA00"
style="font-family:verdana;font-size:80%;color:white">
            <xsl:apply-templates
select="$fieldHeaders">
               <xsl:with-param name="curr-label"
select="."/>
            </xsl:apply-templates>
         </TD>
      </TR>
      <xsl:apply-templates/>
   </xsl:template>

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

   <xsl:template match="field">
      <TR>
         <TD bgcolor="00AACC"
style="font-family:verdana;font-size:80%;color:white">
            <xsl:apply-templates
select="$fieldHeaders">
               <xsl:with-param name="curr-label"
select="."/>
            </xsl:apply-templates>
         </TD>
         <TD>
            <xsl:value-of select="text()"/>
         </TD>
      </TR>
   </xsl:template>

   <xsl:template match="fieldHeaders">
      <xsl:param name="curr-label"/>
      <xsl:variable name="switch"
select="name($curr-label/.)"/>
      <xsl:variable name="parent"
select="name($curr-label/..)"/>
      <xsl:value-of select="key(concat($switch,
'-lookup'), $curr-label/@id)/label"/>
   </xsl:template>

</xsl:stylesheet>

Here is XML document:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="format.xsl"?>
<customerList>
   <customer>
      <field id="customerId">cust1</field>
      <field id="customerName">Customer 1</field>
      <fieldGroup id="homeAddress">
         <fieldList>
            <field id="street">98th Street</field>
            <field id="city">Chicago</field>
         </fieldList>
      </fieldGroup>
      <fieldGroup id="companyAddress">
         <fieldList>
            <field id="street">128th Street</field>
            <field id="city">Chicago</field>
         </fieldList>
      </fieldGroup>
   </customer>
   <customer>
      <field id="customerId">cust2</field>
      <field id="customerName">Customer 2</field>
      <fieldGroup id="homeAddress">
         <fieldList>
            <field id="street">13th Street</field>
            <field id="city">TeaNeck</field>
         </fieldList>
      </fieldGroup>
      <fieldGroup id="companyAddress">
         <fieldList>
            <field id="street">12th Street</field>
            <field id="city">New York</field>
         </fieldList>
      </fieldGroup>
   </customer>
</customerList>

Here is Headers.xml :

<?xml version="1.0" standalone="yes"?>
<fieldHeaders>
   <field id="customerId">
      <label>Customer Id : </label>
   </field>
   <field id="customerName">
      <label>Customer Name : </label>
   </field>
   <fieldGroup id="homeAddress">
      <label>House Address</label>
      <field id="street">
         <label>Street</label>
      </field>
      <field id="city">
         <label>City : </label>
      </field>
   </fieldGroup>
   <fieldGroup id="companyAddress">
      <label>Company Address</label>
      <field id="street">
         <label>Street : </label>
      </field>
      <field id="city">
         <label>Location : </label>
      </field>
   </fieldGroup>
</fieldHeaders>

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread