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 March 11th, 2008, 09:34 AM
Authorized User
 
Join Date: Mar 2008
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.

 
Old March 11th, 2008, 09:46 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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.

 
Old March 11th, 2008, 09:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old March 11th, 2008, 10:16 PM
Authorized User
 
Join Date: Mar 2008
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.

 
Old March 12th, 2008, 03:26 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 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)
 
Old March 12th, 2008, 03:28 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 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 -/
 
Old March 12th, 2008, 03:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old March 12th, 2008, 07:56 AM
Authorized User
 
Join Date: Mar 2008
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.

 
Old March 12th, 2008, 08:08 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 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 -/
 
Old March 12th, 2008, 08:09 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing the variable value Swetha XSLT 2 April 28th, 2008 04:43 PM
Incrementing member variable in Synchronized block vikkiefd Pro Java 0 March 24th, 2008 04:15 AM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10: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 12:04 PM





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