xsl: Setting and accessing a value within the xsl file

The context here is formatting a XML file with an XSL:stylesheet producing a html report.
Most things are working but i am having trouble getting the following to work :

- I want to set a variable to a dummy value then using this value in the code as the previous field value and then adjust teh variable to the new value. The idea is each time a column changes i want to provide a new heading, e.g.
  if prev_category != category then  ! category is a column in the XML file.
     new heading category
  end if
This isn't as easy as you would think because XSL does not allow you to set a variable in 2 places and aborts if you reference a variable before it is initialised.

I have tried several variants based on some examples on the net and here but to no avail. Typically end up with "A reference to variable or parameter 'prev_section' cannot be resolved"

>> So i am wanting the good oil on how to set a parameter, variable, anything and then access this information in an 'if' test ? For any other language this would be simple but xsl has its own way...

Thanks
nb: Points only awarded for a working example, i have burned so much time i don't want to still not be across the line after assigning points ,.
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <!-- Do anything that allows a var / param to be initialised -->
    <xsl:template name="section_prev">
   	<xsl:param name="previous">abc</xsl:param>
    <xsl:variable name="prev_section" select="$previous" />
   </xsl:template>
 
  <xsl:template match="/">
      <html>
      <xsl:for-each select="DocumentElement/Results[section != 'Help']">
         <xsl:if test="section != $prev_section">
<!-- do stuff in here -->
         </xsl:if>
<!-- Other stuff in here -->
 
<!-- Try to update something we can use -->
              <xsl:call-template name="section_prev">
              	<xsl:with-param name="previous" select="section"/>
              </xsl:call-template>
        </xsl:for-each>
     </html>
  </xsl:template>
    
</xsl:stylesheet>

Open in new window

greg_robertsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

greg_robertsAuthor Commented:
NB: The context is opening the .xml file in IE7 or Firefox. However Firefox is the only one that can save the rendered html as html.

Thanks
0
Geert BormansInformation ArchitectCommented:
you are looking at this problem too much from a procedural perspective... and XSLT is not procedural, but rules driven.
If you really want to do it the way you try to do it, you need a recursive process.
But I strongly recommend you to use some sort of a grouping mechanism, to bring nodes together and work the title per group
This is how this is usually done in XSLT
If you send me a sample of your XML I can show you how to in your example

Geert
0
greg_robertsAuthor Commented:
Attached is a cutdown sample of xml.

In this the "section" label is used and when the name changes a new heading is output

Thanks
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

greg_robertsAuthor Commented:
Take 2, system did not like a zip with an xml inside
example.txt
0
Geert BormansInformation ArchitectCommented:
This example uses the muenchian grouping algorithm,
which is explained very well here
http://www.jenitennison.com/xslt/grouping/muenchian.html
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:key name="res" match="Results[section != 'Help']" use="section"/>
    
 
    <xsl:template match="DocumentElement">
        <html>
            <xsl:for-each select="Results[generate-id() = generate-id(key('res', section)[1])]">
                <h2><xsl:value-of select="section"/></h2>
                <xsl:for-each select="key('res', section)">
                    <p><xsl:value-of select="bug_id"/></p>
                    </xsl:for-each>
                </xsl:for-each>
        </html>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
greg_robertsAuthor Commented:

Thanks, while i am not around the inner workings, i get the general idea.
Hard to get a handle on the vars and params use in xsl as the different help has such varying examples.

Many thanks
0
Geert BormansInformation ArchitectCommented:
welcome,
yeah, it takes a while to get the grips
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.