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

March 11th, 2008, 10:34 AM
|
|
Authorized User
|
|
Join Date: Mar 2008
Location: , , .
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, 10:46 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 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, 10:55 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 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, 11:16 PM
|
|
Authorized User
|
|
Join Date: Mar 2008
Location: , , .
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, 04:26 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 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, 04:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 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, 04:44 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 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, 08:56 AM
|
|
Authorized User
|
|
Join Date: Mar 2008
Location: , , .
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, 09:08 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 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, 09:09 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 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>
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |