Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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

0
greg_roberts
Asked:
greg_roberts
  • 4
  • 3
1 Solution
 
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 BormansCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
greg_robertsAuthor Commented:
Take 2, system did not like a zip with an xml inside
example.txt
0
 
Geert BormansCommented:
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
 
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 BormansCommented:
welcome,
yeah, it takes a while to get the grips
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now