Re: [xsl] Needing suggestions for better XSL logic

Subject: Re: [xsl] Needing suggestions for better XSL logic
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Wed, 25 Oct 2006 22:50:40 +0530
(Learning from David's answer), I suggest the following XSLT 2.0 stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                      xmlns:xs="http://www.w3.org/2001/XMLSchema";
                      xmlns:my="http://dummy";
                      version="2.0">

<xsl:output method="html"/>

<xsl:template match="/resultset">
 <xsl:variable name="here" select="." />
 <html>
  <head>
    <title/>
  </head>
  <body>
    <xsl:for-each select="65 to 90">
      <xsl:variable name="letter" select="codepoints-to-string(.)"/>
      <p><xsl:value-of select="$letter" /></p>
      <xsl:variable name="htmlFragment"
select="my:getHtmlFragment($letter, $here)" />
      <xsl:copy-of select="$htmlFragment" />
    </xsl:for-each>
  </body>
</html>
</xsl:template>

<xsl:function name="my:getHtmlFragment" as="node()*">
 <xsl:param name="letter" as="xs:string"/>
 <xsl:param name="here" as="element()"/>

 <ul>
   <xsl:for-each select="$here/term[starts-with(termname, $letter) or
starts-with(termname, lower-case($letter))]">
     <li><xsl:value-of select="termname" /> (<xsl:value-of
select="termattrs/termdef" />)</li>
   </xsl:for-each>
 </ul>
</xsl:function>

</xsl:stylesheet>


On 10/25/06, Kevin.Weiss@xxxxxxx <Kevin.Weiss@xxxxxxx> wrote:
Hi all,

I'm trying to find a better alternative to our current test XSL
template. Currently, we have the following XML file structure (extremely
simplified from the original):

<resultset>
   <term>
       <termname>First Term</termname>
       <termattrs>
           <termdef>This is the firstterm description.</termdef>
       </termattrs>
   </term>
   <term>
       <termname>Second Term</termname>
       <termattrs>
           <termdef>This is the second term description.</termdef>
       </termattrs>
   </term>
</resultset>

We're trying build a glossary of the terms listed in alphabetical order.
In other words, after the translation, viewing the XML (in IE) should
produce an output similar to the following:

A
B
...
F
First Term (This is the first term description.)
...
S
Second Term (This is the second term description.)
...
Z

Unfortunately, our current XSL logic involves an <xsl:for-each> loop
which tests whether the first letter of <termname> begins with a letter.
However, this is done for *each* letter, so we have duplicate code,
i.e.:

<xsl:stylesheet version="2.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
...
<p>A</p>
<xsl:template match="/resultset">
   <xsl:for-each select="term">
       <xsl:if test="starts-with(termname, 'A') or
           starts-with(termname, 'a')">
           <xsl:value-of select="termname" />
           <xsl:value-of select="termattrs/termdef" />
       </xsl:if>
   </xsl:for-each>

<p>B</p>
<xsl:template match="/resultset">
   <xsl:for-each select="term">
       <xsl:if test="starts-with(termname, 'B') or
           starts-with(termname, 'b')">
           <xsl:value-of select="termname" />
           <xsl:value-of select="termattrs/termdef" />
       </xsl:if>
   </xsl:for-each>
...
</xsl:template>
</xsl:stylesheet>

Is there a better method than using multiple <xsl:for-each> loops? We've
looked into other options (e.g. xsl:for-each-group, using multiple
apply-templates), but nothing produces our desired output. My brain is
mush right now, so any suggestions are greatly appreciated. If there's
anything else you need from me, please let me know. Thanks in advance!

Kevin Weiss


--
Regards,
Mukul Gandhi

Current Thread