472,353 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

XSLT - maintaining iteration count

I have an XSLT script that has to operate on some particularly nasty XML to
turn it into a SQL databse, but 'entries' in the XML aren't actually
unique, so their unique identifier is useless. I need to somehow create
crosslinked tables using the XSLT, but can only do this by maintaining a
counter for each line I write.

Is there a way to create a proxy 'increment' function that lets me maintain
a counter variable?

- Mike Kamermans
Jul 20 '05 #1
6 10488
To be more specific, I have an XML document consisting of entries roughly
like this:

<entry>
<seq_num>81732</seq_num>
<k_entries>
<k val="kval1"/>
<k val="kval2"/>
</k_entries>
<r_entries>
<r val="rval1"/>
<r val="rval2"/>
</r_entries>
</entry>

The XSLT operates using a for-each for entries, and needs to do all
combinations of [k] and [r] values. In non-XSL format I'd like to do the
following:

counter = 1;
foreach (entry in document)
foreach (k in entry)
foreach (r in entry)
(create a SQL like with the values of [counter/k/r])
(increment the counter by one)
/foreach
/foreach
/foreach

I'm not sure how to make this work in XSLT.

- Mike
Jul 20 '05 #2


something like

<xsl:for-each select="entry">
<xsl:for-each select="k_entries/k">
<xsl:value-of name="k" select="."/>
<xsl:for-each select="../../r_entries/r">
<xsl:variable name="n">
<xsl:number level="any" count="k|r"/>
</xsl:variable>
do something with . $k and $n
..

David
Jul 20 '05 #3
David,
<xsl:for-each select="entry">
<xsl:for-each select="k_entries/k">
<xsl:value-of name="k" select="."/>
<xsl:for-each select="../../r_entries/r">
<xsl:variable name="n">
<xsl:number level="any" count="k|r"/>
</xsl:variable>
do something with . $k and $n
..


Sadly, that'd only maintain the count inside an entry. this would still
leave the matter of maintaining the count for the whole document. for
instance, a document like

<doc>
<entry>
<k_entries><k val="ka"/><k val="kb"/></k_entries>
<r_entries><r val=ra"/></r_entries>
</entry>
<entry>
<k_entries><k val="kc"/></k_entries>
<r_entries><r val=rb"/><r val=rc"/><r val=rd"/></r_entries>
</entry>
<entry>
<k_entries><k val="kd"/></k_entries>
<r_entries><n val=re"/></r_entries>
</entry>
</doc>

Would have to end up looking like the following text:

insert into entry_table (id,k,r) values (1, ka, ra);
insert into entry_table (id,k,r) values (2, kb, ra);
insert into entry_table (id,k,r) values (3, kc, rb);
insert into entry_table (id,k,r) values (4, kc, rc);
insert into entry_table (id,k,r) values (5, kc, rd);
insert into entry_table (id,k,r) values (6, kd, re);

Know of any way to maintain a counter that creates these 'id' values
correctly?

- Mike
Jul 20 '05 #4
Sadly, that'd only maintain the count inside an entry.
No, if you wanted that you'd have to add from="entry" the default is the
whole document.
Know of any way to maintain a counter that creates these 'id' values
correctly?


As I posted.

David
Jul 20 '05 #5

I wrote
Sadly, that'd only maintain the count inside an entry.
No, if you wanted that you'd have to add from="entry" the default is the
whole document.


This is true.

Know of any way to maintain a counter that creates these 'id' values
correctly?


As I posted.

That is true as written, but not entirely helpful as xsl:number counts
the input tree (in this case the sum of the number of k and r nodes)
but you want the count in the output (essentially the product in this
case)

Your original post indicated that actually you just need a unique
identifier rather than a count, that's easier in xslt, the default mode
in the code below produces
[81732,kval1,rval1][d0e3d0e10d0e17]
[81732,kval1,rval2][d0e3d0e10d0e19]
[81732,kval2,rval1][d0e3d0e12d0e17]
[81732,kval2,rval2][d0e3d0e12d0e19]
[81732,kval1,rval1][d0e23d0e30d0e37]
[81732,kval1,rval2][d0e23d0e30d0e39]
[81732,kval2,rval1][d0e23d0e32d0e37]
[81732,kval2,rval2][d0e23d0e32d0e39]

on

<entries>
<entry>
<seq_num>81732</seq_num>
<k_entries>
<k val="kval1"/>
<k val="kval2"/>
</k_entries>
<r_entries>
<r val="rval1"/>
<r val="rval2"/>
</r_entries>
</entry>
<entry>
<seq_num>81732</seq_num>
<k_entries>
<k val="kval1"/>
<k val="kval2"/>
</k_entries>
<r_entries>
<r val="rval1"/>
<r val="rval2"/>
</r_entries>
</entry>
</entries>
which has a duplicated entry element.
You'll see the first [] on each line iterates twice through the k and r
values, but the second [] on each line is unique.

If you do need a count you have to work (a bit) harder. as shown in mode
b which passes the "count so far" as a parameter to the template for
entry. so in this case you get

[81732,kval1,rval1][1]
[81732,kval1,rval2][2]
[81732,kval2,rval1][3]
[81732,kval2,rval2][4]
[81732,kval1,rval1][5]
[81732,kval1,rval2][6]
[81732,kval2,rval1][7]
[81732,kval2,rval2][8]
David
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"


<xsl:template match="entries">
<xsl:apply-templates select="entry"/>
<xsl:apply-templates mode="b" select="entry[1]"/>
</xsl:template>

<xsl:template match="entry">
<xsl:variable name="e" select="."/>
<xsl:for-each select="k_entries/k">
<xsl:variable name="k" select="."/>
<xsl:for-each select="../../r_entries/r">
[<xsl:value-of select="concat($e/seq_num,',',$k/@val,',',@val,'][',generate-id($e),generate-id($k),generate-id(.),']')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

<xsl:template match="entry" mode="b">
<xsl:param name="c" select="0"/>
<xsl:variable name="e" select="."/>
<xsl:variable name="nr" select="count(r_entries/r)"/>
<xsl:for-each select="k_entries/k">
<xsl:variable name="k" select="."/>
<xsl:variable name="kp" select="position()"/>
<xsl:for-each select="../../r_entries/r">
[<xsl:value-of select="concat($e/seq_num,',',$k/@val,',',@val,'][',$c+($kp -1)*$nr + position(),']')"/>
</xsl:for-each>
</xsl:for-each>
<xsl:apply-templates select="following-sibling::entry[1]" mode="b">
<xsl:with-param name="c" select="$c + $nr * count(k_entries/k)"/>
</xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>
Jul 20 '05 #6

"M.Kamermans" <mk******@science.uva.nl> wrote in message
news:Xn*********************************@213.75.12 .135...
To be more specific, I have an XML document consisting of entries roughly
like this:

<entry>
<seq_num>81732</seq_num>
<k_entries>
<k val="kval1"/>
<k val="kval2"/>
</k_entries>
<r_entries>
<r val="rval1"/>
<r val="rval2"/>
</r_entries>
</entry>

The XSLT operates using a for-each for entries, and needs to do all
combinations of [k] and [r] values. In non-XSL format I'd like to do the
following:

counter = 1;
foreach (entry in document)
foreach (k in entry)
foreach (r in entry)
(create a SQL like with the values of [counter/k/r])
(increment the counter by one)
/foreach
/foreach
/foreach

I'm not sure how to make this work in XSLT.

- Mike

Here's a quick XSLT 2.0 + FXSL solution:

This transformation:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f xs" <xsl:import href="../f/func-standardXpathFunctions.xsl"/>
<xsl:import href="../f/func-map.xsl"/>

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:for-each select=
"for $indEntry in 1 to count(/*/entry),
$ind-r_entry in 1 to count(/*/entry[$indEntry]/r_entries/r)
return
f:map(f:insert-before(data(/*/entry[$indEntry]/r_entries/r[$ind-r_entry]/@val),1),
data(/*/entry[$indEntry]/k_entries/*/@val)
) "


<xsl:value-of select="if(position() mod 2 = 1) then
(position() + 1) idiv 2,
', ', ., ', '
else
(. , '&#xA;')
"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

when applied on this source xml document:

<cat>
<entry>
<seq_num>81732</seq_num>
<k_entries>
<k val="kval1"/>
<k val="kval2"/>
</k_entries>
<r_entries>
<r val="rval1"/>
<r val="rval2"/>
</r_entries>
</entry>
<entry>
<seq_num>81742</seq_num>
<k_entries>
<k val="kval3"/>
<k val="kval4"/>
</k_entries>
<r_entries>
<r val="rval3"/>
<r val="rval4"/>
</r_entries>
</entry>
<entry>
<seq_num>81752</seq_num>
<k_entries>
<k val="kval5"/>
<k val="kval6"/>
</k_entries>
<r_entries>
<r val="rval5"/>
<r val="rval6"/>
</r_entries>
</entry>
</cat>

produces the desired result:

1 , kval1 , rval1
2 , kval2 , rval1
3 , kval1 , rval2
4 , kval2 , rval2
5 , kval3 , rval3
6 , kval4 , rval3
7 , kval3 , rval4
8 , kval4 , rval4
9 , kval5 , rval5
10 , kval6 , rval5
11 , kval5 , rval6
12 , kval6 , rval6

Cheers,

Dimitre Novatchev
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: inquirydog | last post by:
Hi- Does anyone know a way to compare whether two nodes contain the same information in xslt (the name, attributes, and all content recursivly...
1
by: karthigan | last post by:
I am writing a simple hardware test program in C that would run from Windows command line. Inside one of the loops, I have this code fragment...
1
by: cameron | last post by:
I am attempting to pass in an XmlDocument as a parameter to a transform: from my sample ASPX page: string BaseDir =...
6
by: Michiel Kamermans | last post by:
Hi, I need to generate a list based on a sorted nodeset, except duplicates need to be discarded. I initially though of doing a sort on a nodeset...
4
by: gualtmacchi | last post by:
I'm processing an XML input file getting a plain text file where from M nodes I got N output lines... It's not relevant but the input file is a...
1
by: jrwarwick | last post by:
Hello, I believe I have uncovered a bug in the .Net XSLT engine to do with 'for' loops in XSLT. Here are the steps to reproduce it: -Create A...
2
by: Adam dR. | last post by:
I have an xml file similar to: <menu> <menuitem name="home" show="false"/> <menuitem name="about" show="true"/> <menuitem name="links"...
4
by: shaun roe | last post by:
I should like to count the frequency of strings embedded in a longer string, space separated. Specifically, I have: <phiModule> 5 5 5 5 6 6 6...
0
by: Leira | last post by:
Hi, I have a problem with grouping. My source XML has <record> elements that have a @name and a @group attribute. It looks something like this: ...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.