Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2008, 08:00 AM
Registered User
 
Join Date: Jan 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing and counting using XSLT

Hello!

I m trying to transform a XML file into new XML file .

Description:

Each unique value from the <name> elements in the input record are to be uniqued, alphabetized and stored with a "count" attribute. The new "count" attribute provides the number of occurrences of that value .Must parse and input element on semi-colon if this delimits a list of values.

input XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<School>
<student rec='1'>
<name>Robart J</name>
</student>
<student rec='2'>
<name>Alex M; Peter R</name>
</student>
<student rec='3'>
<name>Alex M;Robart J; Dave T</name>
</student>
</School>


Output XML should be like..

<School>
<name count='2'>Robart J</name>
<name count='2'>Alex M</name>
<name count='1'>Peter R</name>
<name count='1'>Dave T</name>
</School>

I am using XSLT 2.0 processor. I just can't work it out. Is this even possible with XSL? Or is there a
better way of doing the substitution that I'm missing? Please let me know if it is possible in XSLT 1.0 ….

Thanks in advance

Manish


 
Old January 23rd, 2008, 08:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0 it looks something like this:

<xsl:for-each-group select="student/name/tokenize(., ';\s*')">
  <xsl:sort select="current-grouping-key()"/>
  <name count="{count(current-group())}">
    <xsl:value-of select="current-grouping-key()"/>
  </name>
</xsl:for-each-group>

Check the "word count" example in Chapter 1 of XSLT Programmer's Reference 3rd ed for a very similar example with more detail given.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 24th, 2008, 02:38 AM
Registered User
 
Join Date: Jan 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael

Thanks for your input. It was indeed very useful. But just a small change - as for-each-group requires a mandatory group-by attribute I have modified the xslt code as follows and it worked !!

<xsl:for-each-group select="School/student" group-by="name/tokenize(., ';\s*')">
  <xsl:sort select="current-grouping-key()"/>
  <name count="{count(current-group())}">
    <xsl:value-of select="current-grouping-key()"/>
  </name>
</xsl:for-each-group>

Thanks again.

Manish






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT for parsing XHTML Form shahbhat XSLT 6 August 26th, 2008 06:22 PM
XSLT loop counting and breaking. warhero XSLT 1 July 3rd, 2007 03:02 PM
Urgent help regarding XSLT parsing.... to xml.. netbramha XSLT 1 September 19th, 2005 09:03 AM
Parsing an XSLT stylesheet failed .... :( Thebravehearth XML 0 October 12th, 2004 10:55 PM
XSLT simple parsing problem misu XSLT 3 August 18th, 2004 02:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.