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 November 2nd, 2007, 06:41 AM
Authorized User
 
Join Date: Nov 2007
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Default XSLT 2.0 array/map

Hi,

Is there any "array" like in XSLT 2.0?
I would like to increment every key that I found.

For example:

<line>ABC</line> ==> will create key="ABC" value=1
<line>DEF</line> ==> will create key="DEF" value=1
<line>ABC</line> ==> will increment value key="ABC" by 1 (value= 2)

Any suggestion?


Cheers,
 
Old November 2nd, 2007, 06:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT 2.0 supports sequences, which are very like the arrays of other programming languages.

However, XSLT is a functional language, so values are immutable. I'm not sure you are thinking about your transformation task in the right way: could you explain the problem you are trying to solve, rather than the solution you are trying to design?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 2nd, 2007, 06:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't increment values, and XSLT doesn't have the concept of an array as such.

What about simply doing "count(line[string(.)='ABC'])".

You could use the distinct-values(line/text()) to get a list of unique values of line values.

Code:
<?xml version='1.0' encoding='utf-8' ?>
<lines>
    <line>ABC</line>
    <line>BC</line>
    <line>ABC</line>
    <line>NBC</line>
    <line>ABC</line>
    <line>BC</line>
</lines>



<xsl:template match="/">
<xsl:variable name="root" select="."/>
<xsl:for-each select="distinct-values(//line/text())">
    <xsl:sequence select="."/> = <xsl:sequence select="count($root//line[string(.)=current()])"/>
</xsl:for-each>
</xsl:template>
Produces: ABC = 3 BC = 2 NBC = 1


/- Sam Judson : Wrox Technical Editor -/
 
Old November 2nd, 2007, 07:17 AM
Authorized User
 
Join Date: Nov 2007
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for the reply :)

data example:
<data>
   <line>ABC 2007-11-01T12:00:01</line>
   <line>DEF 2007-11-01T12:00:01</line>
   <line>ABC 2007-11-01T13:60:02</line>
   <line>ABC 2007-11-01T17:60:02</line>
</data>

I have to read <line> one by one. Everytime I found for example "ABC", then I need to display how many I have read that "ABC"

so in this case when I read
 * 1st <line> will produce something like
   <name counter="1">ABC</name>
 * 2nd <line> will produce <name counter="1">DEF</name>
 * 3rd <line> will produce <name counter="2">ABC</name>
 * 4th <line> will produce <name counter="3">ABC</name>

I hope this explain my intention.

If XSLT can't produce this, is it possible if I parsing a parameter from XSLT to my java application? therefore I can send a parameter like "ABC" and get the java to do the counter and send me back the result.

Thanks,
 
Old November 2nd, 2007, 09:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't have to read the lines one by one, you can process them all at once. That's what declarative programming is about.

Sounds like you want

<xsl:template match="line">
  <name counter="{1+count(preceding-sibling::line[.=current()]}">
    <xsl:value-of select="."/>
  </name>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 2nd, 2007, 10:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I think <xsl:number> might be able to do what you are after:

<xsl:template match="line">
    <name>
        <xsl:attribute name="counter">
            <xsl:number count="//line[text()=current()]"/>
        </xsl:attribute>
        <xsl:sequence select="."/>
    </name>
</xsl:template>


/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Array like processing (Xpath 1 / XSLT 1) akentanaka XSLT 1 July 2nd, 2008 02:22 AM
how to map a url to another? worldcrafter Apache Tomcat 0 April 2nd, 2008 06:19 PM
Heat Map over Google Map ajit Javascript 0 March 7th, 2008 09:01 AM
xslt: pass index of array EefjeC XSLT 1 July 2nd, 2007 06:31 AM
conditional xslt processing based on java array twilson997 XSLT 7 June 28th, 2006 07:32 AM





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