XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Peter BartellSubject: Incrementing a variable qname in a for-each?
Author: Peter Bartell
Date: 02 May 2006 01:53 PM
First time poster and relative newbie to XSLT. I am trying to load a list of variables, whose names increment by one, with a group of values into a CSV string.

<xsl:for-each select="//topic">
<xsl:variable name="var1"><!-- this needs to increment up by 1 on each pass -->
<xsl:value-of select="element1"/><xsl:text>,</xsl:text>
<xsl:value-of select="element2"/><xsl:text>,</xsl:text>
<xsl:value-of select="element3"/><xsl:text>,</xsl:text>
<xsl:value-of select="element4"/>
</xsl:variable>
</xsl:for-each>

I need to somehow keep these variable values 'alive' for later processing. I have looked for examples of this, but it seems that a Qname cannot be updated this way. Any suggestions are very much appreciated! Thanks.

P. Bartell

Postnext
(Deleted User) Subject: Incrementing a variable qname in a for-each?
Author: (Deleted User)
Date: 02 May 2006 03:57 PM
Hi Peter

XSLT doesn't allow for 'updating' a variable, once it is initialized.

You need to restructure your code appropriately.

What is it that you are trying to do as part of 'later processing' - can you elaborate on that?

Regards
- Srini

Postnext
Peter BartellSubject: Incrementing a variable qname in a for-each?
Author: Peter Bartell
Date: 02 May 2006 04:07 PM
Hi Srini,

Thanks for the reply. I realize that variables cannot be updated. That's why I wanted to be able to make a new variable for each pass.

What I need to do is find several elements in many topics, group them by one of the elements where the contents are the same, and then render them in HTML. I had thought if I could load them into some sort of array of many variables and then work with the variables, I could accomplish this.

I have seen some XSLT using <xsl:value-of select=key(... that looks like it might be what I'm looking for, but I really don't understand it. Any guidance is appreciated. Thanks.

Peter

Postnext
(Deleted User) Subject: Incrementing a variable qname in a for-each?
Author: (Deleted User)
Date: 02 May 2006 04:30 PM
Hi Peter

It would be easy to address this if you could provide a sample of the input document and the desired output result.

Regards
Srini

Postnext
Peter BartellSubject: Incrementing a variable qname in a for-each?
Author: Peter Bartell
Date: 02 May 2006 04:40 PM
My apologies. I started working on that right after I sent my last message.

XML source

<topic>
<element1>a</element1>
<element2>12</element2>
<element3>23</element3>
<element4>23</element4>
</topic>
<topic>
<element1>a</element1>
<element2>12</element2>
<element3>55</element3>
<element4>66</element4>
</topic>
<topic>
<element1>b</element1>
<element2>2</element2>
<element3>78</element3>
<element4>6</element4>
</topic>

Output HTML

<html>
<body>
<table>
<tr>
<td>a</td><td>12,12</td><td>23,55</td><td>23,66</td>
</tr>
<tr>
<td>b</td><td>2</td><td>78</td><td>6</td>
</tr>
</table>
</body>
</html>

As you can see, when element1 content is the same, it groups the rest of the elements' content from the other topics into the same html row. Thanks again for your help! Let me know if you need anything more.

Peter

Postnext
Ivan PedruzziSubject: Incrementing a variable qname in a for-each?
Author: Ivan Pedruzzi
Date: 02 May 2006 06:09 PM
Peter,

Your input document is not well-form, it doesn't have a single root, so i created one called root.

Please see the attached files as one the possible way to solve your problem.


Hope this helps
Ivan Pedruzzi
Stylus Studio Team


Documenttopics.xsl


Documenttopics.xml

Postnext
Ivan PedruzziSubject: Incrementing a variable qname in a for-each?
Author: Ivan Pedruzzi
Date: 02 May 2006 06:27 PM
A minor change to avoid duplicate the first column

Ivan Pedruzzi
Stylus Studio Team


Documenttopics(1).xsl

Postnext
Peter BartellSubject: Incrementing a variable qname in a for-each?
Author: Peter Bartell
Date: 03 May 2006 09:16 AM
Thank you all for your help! First I will try to understand the XPath, then I will try integrating it with the rest of my code soon. And sorry about the non-wellformed example; I just sent a snippet, but I should have stated that. Thanks again!

Postnext
Peter BartellSubject: Incrementing a variable qname in a for-each?
Author: Peter Bartell
Date: 03 May 2006 05:41 PM
After trying to implement the XSL, I see that I did a bad job of relating my situation. I gave you example XML that was not completely useful. The elements I need to place in rows in the HTML are not siblings, therefore the position() won't work. Instead the elements are all children, ancestors, or ancestor attribute values of topic. I had expected to explicitly define those at some point in the code, but I don't think position lets me do that.

Naturally I do not expect a complete solution, but I have included a real XML instance and here are the values I need to extract relative to their parent topic.

//change_description/@TEL-only_change_description
./title
//revision/@rev_number
//change_description

Sorry about the inconvenience; I am not used to posting. Any help or pointers are appreciated!

Peter


Documentsample(7).XML
XML file

Posttop
Peter BartellSubject: Incrementing a variable qname in a for-each?
Author: Peter Bartell
Date: 10 May 2006 09:11 AM
Thanks anyway for the help. Luckily I can use your sample code for another project I have. Take care.

Peter

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.