• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

XML to XSL conversion. Need to keep track of rows based on an if condition

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using XSL to convert an XML file to a csv file.
The XML file in the format below:

<file>
<record>
<name>Johnny</name>
<age>32</age>
</record>
<record>
<name>Jimmy</name>
<age>5</age>
</record>
<record>
<name>Baby Moon</name>
<age>1</age>
</record>
<record>
<name>Lisa</name>
<age>25</age>
</record>
</file>
My xsl file should be implemented such that all records where age <20 are ignored.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:for-each select="file/record">
<xsl:if test="age > 20">
<xsl:value-of select="position()"/>
<xsl:value-of select="name"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="age"/>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

I need to keep track of the records that have been picked and keep a sequence going. Following is the expected result.

1,Johnny,32
2,Lisa,25

but I am getting
1,Johnny,32
4,Lisa,25

How can I fix this?

Thanks!
[ May 13, 2008: Message edited by: Cindy Jones ]
 
Sheriff
Posts: 28331
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The position() function returns the element's position in the nodelist selected by xsl:for-each. That's why you're getting 1 and 4, because you consider only positions 1 and 4. What I would do is this:and drop the xsl:if element because you're just selecting the nodes you are interested in.
 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! Let me check if it solves my real problem I had posted a simplistic version.
 
This tiny ad is wafer thin:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic