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
Stan  MatthewsSubject: XSL incremental count -
Author: Stan Matthews
Date: 15 Oct 2007 05:57 AM
First post so please excuse any Faux Pas's

I have an xml file as below

<?xml version="1.0"?>
<Car>
<Type>Ford Sierra</Type>
<Service>1
<Date>01/09/2006</Date>
<Tyres>OK</Tyres>
<Oil>Filled</Oil>
<Water>Filled</Water>
</Service>
<Service>2
<Date>01/09/2005</Date>
<Tyres>OK</Tyres>
<Oil>Filled</Oil>
<Water>Filled</Water>
</Service>
<Service>3
<Date>01/09/2004</Date>
<Tyres>OK</Tyres>
<Oil>Filled</Oil>
<Water>Filled</Water>
</Service>
</Car>

I want to produce the below xml file from this

<?xml version="1.0"?>
<CarHistory>
<Type>Ford Sierra</Type>
<NoOfServices>3</NoOfServices>Ford Sierra
<ServiceDetails>
<ServiceNo>1</ServiceNo> <!-- note the count -->
<Date>01/09/2006</Date>
<Water>Filled</Water>
</ServiceDetails>
<ServiceDetails>
<ServiceNo>2</ServiceNo> <!-- note the count increase by 1 -->
<Date>01/09/2005</Date>
<Water>Filled</Water>
</ServiceDetails>
<ServiceDetails>
<ServiceNo>3</ServiceNo> <!-- note the count increase by 1 again and should do till end of services -->
<Date>01/09/2004</Date>
<Water>Filled</Water>
</ServiceDetails>
</CarHistory>

So far my xls looks like this

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<CarHistory>
<Type>
<xsl:choose><!-- Test to see if Type is there or if it has no value -->
<xsl:when test="Car/Type and '' != Car/Type"><!--If it is return the Value -->
<xsl:value-of select="Car/Type"/>
</xsl:when><!-- If its not return "Not Given" -->
<xsl:otherwise>Not Given</xsl:otherwise>
</xsl:choose>
</Type>
<NoOfServices>
<xsl:value-of select="count(Car/Service)"/>
</NoOfServices>
<xsl:apply-templates/>
</CarHistory>
</xsl:template>
<xsl:template match="Service">
<ServiceDetails>
<ServiceNo>
<!-- here lies the problem -->
</ServiceNo>
<Date>
<xsl:value-of select="Date"/>
</Date>
<Water>
<xsl:value-of select="Water"/>
</Water>
</ServiceDetails>
</xsl:template>
</xsl:stylesheet>

I wish to find the simplest way to have the <ServiceNo> incremnet by 1 each time around Position() does not seem to help me here as it increases by 2.

Thanks in advance.

Stan

Postnext
James DurningSubject: XSL incremental count -
Author: James Durning
Date: 15 Oct 2007 09:18 AM
<xsl:number count="ServiceNo"/>

http://www.w3schools.com/xsl/el_number.asp

Postnext
Stan  MatthewsSubject: XSL incremental count -
Author: Stan Matthews
Date: 15 Oct 2007 09:52 AM
That is excellent ... it could solve the problem, however the parser I am forced to use does not support XPATH 2.

So this works in the latest parser but not in the .net parser :-(

Postnext
James DurningSubject: XSL incremental count -
Author: James Durning
Date: 15 Oct 2007 11:04 AM
Originally Posted: 15 Oct 2007 11:03 AM
Odd, xsl:number is an XSLT 1.0 feature.
<xsl:value-of select="1 + count(preceding::ServiceNo)"/>

Posttop
Stan  MatthewsSubject: XSL incremental count -
Author: Stan Matthews
Date: 15 Oct 2007 11:22 AM
Ah yes your right the first time, there was a small syntax error throwing it out.

Cheers this answer was most helpfull.

 
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.