Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 5th, 2005, 07:02 PM
Registered User
 
Join Date: Aug 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Newbie : How do you .. without setting a variable

Hi,

Yes, I've just read a ton of posts saying we can't set global
variables. Maybe its the procedural programming mindset, but I can't
seem to find a way to solve this problem without being able to
conditionally "set" a variable.

Basically I am converting an xml to a csv. Given the following sample xml :

Code:
<envelope>
   <request req_id="1">
           ------------
       <payload data="data1"/>
           ------------
   </request>
   <response req_id="1">
       <result id="result1"/>
       <result id="result2"/>
       <result id="result3"/>
   </response>
   <request req_id="2">
           ------------
       <payload data="data2"/>
           ------------
   </request>
   <response req_id="2">
       <result id="result4"/>
       <result id="result5"/>
       <result id="result6"/>
   </response>
   .
   .
   .
   .
   .
</envelope>
I first want to make sure that the req_id in the response is the same
as that in the immediately preceding request, before processing the
response.

The final output should look like :

Code:
data1, result1, result2, result3
data2, result4, result5, result6
.
.
.
.
.
dataN, result_, result__, result___

My approach would have been to set a 'reqId' variable each time within
a <xsl:template match="request"> and to set a 'dataVal' variable each
time within a <xsl:template match="payload"> and then use them
immediately as $reqId and $dataVal when i hit the response.

Any thoughts on how I can achieve this would be greatly appreciated.

Thanks very much,
rqaran

 
Old August 6th, 2005, 03:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

"I first want to make sure that the req_id in the response is the same
as that in the immediately preceding request, before processing the
response."

<xsl:if test="req_id = preceding-sibling::response[1]/req_id">
  ... process the response ...
</xsl:if>

The funny thing is, the XSLT code is a direct translation of the problem statement, yet people are so used to low-level programming using variables that it doesn't come to them naturally!


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 6th, 2005, 04:09 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Basically once a variable is declared it cannot be changed, but if it goes out of scope the variable can be re-used so global variables can only be set once but ones within an xsl:template can be different each time the template is called.

In your case you don't need to re-assign but to show the point, given this XML:
Code:
<envelope>
   <request req_id="1">
       <payload data="data1"/>
   </request>
   <response req_id="1">
       <result id="result1"/>
       <result id="result2"/>
       <result id="result3"/>
   </response>
   <request req_id="2">
       <payload data="data2"/>
   </request>
   <response req_id="2">
       <result id="result4"/>
       <result id="result5"/>
       <result id="result6"/>
   </response>
</envelope>
Then use this XSLT:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="/">
    <xsl:apply-templates select="envelope/request/payload" />
  </xsl:template>

  <xsl:template match="payload">
    <xsl:value-of select="@data"/><xsl:text>, </xsl:text>
    <xsl:variable name="req_id" select="../@req_id"/>
    <xsl:apply-templates select="/envelope/response[@req_id = $req_id]/result"/>
  </xsl:template>

  <xsl:template match="result">
    <xsl:value-of select="@id"/>
    <xsl:choose>
      <xsl:when test="position() != last()">
        <xsl:text>, </xsl:text>
      </xsl:when>  
      <xsl:otherwise>
        <xsl:text>#xd;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
conditionally setting variable ewel Beginning PHP 2 December 13th, 2007 07:39 PM
problem setting a variable jtrifts Javascript 4 August 9th, 2007 08:36 AM
(newbie) using a variable ypomonh XSLT 5 May 5th, 2007 02:34 AM
declare a variable without setting a value to it crmpicco Javascript How-To 1 July 18th, 2005 07:25 PM
setting each value from database as a variable Ashleek007 Beginning PHP 21 April 18th, 2005 03:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.