 |
| 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
|
|
|
|

March 11th, 2008, 09:34 AM
|
|
Authorized User
|
|
Join Date: Mar 2008
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

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

March 11th, 2008, 09:55 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

March 11th, 2008, 10:16 PM
|
|
Authorized User
|
|
Join Date: Mar 2008
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

March 12th, 2008, 03:26 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You can probably do this using either the position() function or the xsl:number element, depending on the scenario.
--
Joe ( Microsoft MVP - XML)
|
|

March 12th, 2008, 03:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

March 12th, 2008, 03:44 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

March 12th, 2008, 07:56 AM
|
|
Authorized User
|
|
Join Date: Mar 2008
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

March 12th, 2008, 08:08 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

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