Link to home
Start Free TrialLog in
Avatar of greg_roberts
greg_roberts

asked on

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

Avatar of greg_roberts
greg_roberts

ASKER

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
Avatar of Gertone (Geert Bormans)
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
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
Take 2, system did not like a zip with an xml inside
example.txt
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
welcome,
yeah, it takes a while to get the grips