xsl-list
[Top] [All Lists]

RE: RE: RE: RE: Need to use generate-id() or other method?

2003-04-25 09:03:20
-----Original Message-----
From:     "Kathy Burke" <Kathy_Burke(_at_)Jabil(_dot_)com>
Subject:  RE: RE: RE: [xsl] Need to use generate-id() 
or other method?

I've looked over your template. If I understand what you are doing with it, it 
looks like you want to identify each step with a multi-level number. A step may 
be decomposed into smaller steps with the smaller steps being numbered as part 
of the hierarchy. That is to say, there may exist a "step 1" which is composed 
of sub-steps 1.1, 1.2, 1.3, etc. And this pattern may continued down through an 
arbitrary number of levels.

The step elements in the XML file have no unique indentifiers, that's why you 
are using the <xsl:number> element to output a unique number for each step.

Each step is expressed as a single row in the output HTML table. Sub-steps are 
visually represented by altering the width of the cell based on the number of 
ancestors the step represented in the cell has. Sub-steps are produced by 
calling the step template recursively.

If these things are true, I recommend that you modify your template this way.:

Instead of outputting the number created by the <xsl:number> element directly, 
I suggest that you capture it to a variable. You can then place it in the table 
division with an <xsl:value-of select="$var" />. You will further want to add 
an <xsl:parameter> element to the step template which will receive the value of 
$var when the template is called recursively. That way you can concatenate the 
value of $var from the calling template with the value of the variable 
generated with each recursive call to produce a unique "id" attribute for each 
button.

I took a simple hierarchical menu XML file I have and made an XSLT along the 
lines I've suggested which will produce HTML to demonstrate the technique. My 
output consists of nested <div> elements rather than nested tables, but the 
differences should be minor.

XML file
================================================================
<?xml version="1.0" encoding="UTF-8" ?>
<menu>
   <item title="ABC">
      <item uri="http://www.domain.com#books";>Library Home</item>
      <item uri="">Phone Directories</item>
      <item uri="">Acquisition Policy</item>
      <item uri="">Publications</item>
      <item uri="">Directives</item>
      <item uri="">HQ Offices</item>
      <item uri="">Links Library</item>
      <item uri="">Policy</item>
      <item uri="">Program Documents</item>
   </item>
   <item type="menu" title="DEF">
      <item uri="http://www.domain.com#books";>Library Home</item>
      <item uri="">Phone Directories</item>
      <item uri="">Acquisition Policy</item>
      <item uri="">Publications</item>
      <item uri="">Directives</item>
      <item uri="">HQ Offices</item>
      <item uri="">Links Library</item>
      <item uri="">Policy</item>
      <item uri="">Program Documents</item>
      <item title="Warner Bros.">
         <item uri="">Foghorn Leghorn</item>
         <item title="Elmer Fudd">
            <item uri="">Ride of the Valkyries</item>
            <item uri="">Il Trovatore</item>
         </item>
         <item uri="">Daffy Duck</item>
      </item>
   </item>
   <item uri="">Bugs Bunny</item>
</menu>

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

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

   <xsl:template match="item">
      <xsl:param name="parent-id" select="''" />
      <xsl:variable name="id">
         <xsl:number level="multiple" count="item" format="1.1"/>
      </xsl:variable>
      <div id="{$parent-id}{substring($id, 1, string-length($id)-1)}">
         <xsl:apply-templates>
            <xsl:with-param name="parent-id" select="$id" />
         </xsl:apply-templates>
      </div>
   </xsl:template>

</xsl:stylesheet>

-- 
Charles Knell
cknell(_at_)onebox(_dot_)com - email


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