p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 11th, 2008, 10:34 AM
Authorized User
 
Join Date: Mar 2008
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Way to incrementing value of variable in xsl

Hi,

Is there anyway to increment the value of a variable in xsl? Actually I want to increment a variable's value by some constant value, say 1. Thanks in advance for the help.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old March 11th, 2008, 10:46 AM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
Default

A variable is bound to a value once and you can't change the value afterwards.
You can however write recursive templates or functions (in XSLT 2.0) where you pass in an incremented value on each invocation.
You might want to try to explain what you want to achieve in terms of the input you have and the output you want to generate, then we might be able to suggest an XSLT way of solving that without needing to try to change variables.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old March 11th, 2008, 10:55 AM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
Default

XSLT is a declarative language, and you're trying to apply procedural thinking to it.

Tell us what your problem is, not how you're trying (and failing) to solve it...

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old March 11th, 2008, 11:16 PM
Authorized User
 
Join Date: Mar 2008
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following is my scenario :
I want to add one attribute called "sNo" for the element "Response" in the output stream. "sNo" denotes values from 1 to 10. In the input xml, I can get multiple "Response" elements but that won't have "sNo" attribute.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old March 12th, 2008, 04:26 AM
joefawcett's Avatar
Wrox Author
Points: 8,994, Level: 40
Points: 8,994, Level: 40 Points: 8,994, Level: 40 Points: 8,994, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 Posts
Default

You can probably do this using either the position() function or the xsl:number element, depending on the scenario.

--

Joe (Microsoft MVP - XML)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old March 12th, 2008, 04:28 AM
samjudson's Avatar
Friend of Wrox
Points: 4,453, Level: 28
Points: 4,453, Level: 28 Points: 4,453, Level: 28 Points: 4,453, Level: 28
Activity: 60%
Activity: 60% Activity: 60% Activity: 60%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
Default

Try giving us an example of your input XML, what your current XSLT looks like and what you are wanting your output XML to look like.

/- Sam Judson : Wrox Technical Editor -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old March 12th, 2008, 04:44 AM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
Default

Sounds like something that can easily be done using xsl:number, or perhaps even using the position() function.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old March 12th, 2008, 08:56 AM
Authorized User
 
Join Date: Mar 2008
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for all of you for the response. The following is the sample input xml :
<?xml version = "1.0" encoding = "utf-8"?>
<?xml-stylesheet type="text/xsl" href="address.xsl"?>
<Response>
 <address>
  <street>MidTown Road</street>
  <city>Luxemberg</city>
 </address>
 <address>
  <street>Hudson Road</street>
  <city>California</city>
 </address>
</Response>

The following is my xsl (address.xsl) :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
<xsl:output method="xml" indent="yes"/>
    <xsl:template match="Response">
      <Response>
      <xsl:for-each select="address">
       <xsl:call-template name="address_template"/>
      </xsl:for-each>
     </Response>
    </xsl:template>
    <xsl:template name="address_template">
     <Address>
      <xsl:attribute name="sequenceNbr">
       <xsl:value-of select="position()"/>
      </xsl:attribute>
     </Address>
    </xsl:template>
</xsl:stylesheet>

Is the code correct. When I am running the xml file in IE, I am not getting any output. Please help me to resolve this.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old March 12th, 2008, 09:08 AM
samjudson's Avatar
Friend of Wrox
Points: 4,453, Level: 28
Points: 4,453, Level: 28 Points: 4,453, Level: 28 Points: 4,453, Level: 28
Activity: 60%
Activity: 60% Activity: 60% Activity: 60%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
Default

The reason you are not getting any 'visible' output is because you are outputting XML and viewing it in IE. There is no text to display, only empty elements, which IE does not render.

IE is a very bad tool for 'testing' your XSLT if that is what you are using it for (I notice the xalan namespace on your XSLT, so suspect your real environment might be Java).

Other than that you're XSLT is working fine.

/- Sam Judson : Wrox Technical Editor -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #10 (permalink)  
Old March 12th, 2008, 09:09 AM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
Default

First of all using IE or any other browser to test XSLT stylesheet is not the best choice, in particular when you transform XML to XML. Consider to use an XSLT processor from the command line or an XML editor with XSLT support. IE will treat the result of your XSLT transformation as HTML and that way you will not see a meaningful result if the stylesheet generates XML.

As for your stylesheet, instead of
Code:
<xsl:for-each select="address">
       <xsl:call-template name="address_template"/>
      </xsl:for-each>
I think you want
Code:
  <xsl:apply-templates select="address"/>
and then you need a template
Code:
<xsl:template match="address">
  <Address sequenceNbr="{position()}">
    <xsl:apply-templates/>
  </Address>
</xsl:template>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing the variable value Swetha XSLT 2 April 28th, 2008 05:43 PM
Incrementing member variable in Synchronized block vikkiefd Pro Java 0 March 24th, 2008 05:15 AM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 11:41 AM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
incrementing value of a variable akibaMaila VB.NET 2002/2003 Basics 4 July 5th, 2005 01:04 PM



All times are GMT -4. The time now is 11:29 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc