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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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.

   
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.