473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XSLT 1.0: Iteration over a sequence

I need to iterate over the letters of the alphabet; the only solution I
found using only XSLT 1.0 is:

<xsl:key name="hostnames" match="host/name" use="substring(., 1, 1)" />

<my:alphabet xmlns:my="my.com">
<l>a</l><l>b</l><l>c</l><l>d</l><l>e</l><l>f</l><l>g</l><l>h</l><l>i</l><l>j</l><l>k</l><l>l</l><l>m</l><l>n</l><l>o</l><l>p</l><l>q</l><l>r</l><l>s</l><l>t</l><l>u</l><l>v</l><l>w</l><l>x</l><l>y</l><l>z</l>
</my:alphabet>

<xsl:variable name="root" select="/"/>
<xsl:variable name="alphabet" select="document('')/*/my:alphabet/l"/>
<xsl:template name="summary">
...
<!--this works, but I don't need it -->
<xsl:for-each select="$root">
<xsl:apply-templates select="key('hostnames','b')" />
</xsl:for-each>

<!--this doesn't work and I need it!!! -->
<xsl:for-each select="$alphabet">
<xsl:variable name="letter" select="." />
<xsl:for-each select="$root">
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames','a')" />
</xsl:for-each>
</xsl:for-each>

</xsl:template>
What can I do?

Thanks,
Giacomo.


Aug 11 '06 #1
7 1324
I get the output
<?xml version="1.0" encoding="UTF-8"?>
...

a: b: c: d: e: f: g: h: i: j: k: l: m: n: o: p: q: r: s: t: u: v: w:
x: y: z:

(modulo the tabs, of course.) So your iteration is working fine. Of
course I don't have your input file (or the other parts of your
stylesheet), so it isn't surprising the key lookup of 'b' isn't finding
anything.

Working copy follows.
<xsl:stylesheet version="1.0" xmlns:my="my.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="hostnames" match="host/name" use="substring(., 1, 1)" />
<my:alphabet xmlns:my="my.com">
<l>a</l><l>b</l><l>c</l><l>d</l><l>e</l><l>f</l><l>g</l><l>h</l><l>i</l><l>j</l><l>k</l><l>l</l><l>m</l><l>n</l><l>o</l><l>p</l><l>q</l><l>r</l><l>s</l><l>t</l><l>u</l><l>v</l><l>w</l><l>x</l><l>y</l><l>z</l>
</my:alphabet>

<xsl:variable name="root" select="/"/>
<xsl:variable name="alphabet" select="document('')/*/my:alphabet/l"/>

<xsl:template match="/">
<xsl:call-template name="summary"/>
</xsl:template>

<xsl:template name="summary">
...
<!--this works, but I don't need it -->
<xsl:for-each select="$root">
<xsl:apply-templates select="key('hostnames','b')" />
</xsl:for-each>

<!--this works just fine... -->
<xsl:for-each select="$alphabet">
<xsl:variable name="letter" select="." />
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames','a')" />
</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Aug 12 '06 #2
One thought: When you wrote
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames','a')" />

I suspect you meant something more like:
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames',$letter)" />

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Aug 12 '06 #3
Joe Kesselman wrote:
I get the output
<?xml version="1.0" encoding="UTF-8"?>
...

a: b: c: d: e: f: g: h: i: j: k: l: m: n: o: p: q: r: s: t: u: v:
w: x: y: z:

(modulo the tabs, of course.) So your iteration is working fine. Of
course I don't have your input file (or the other parts of your
stylesheet), so it isn't surprising the key lookup of 'b' isn't finding
anything.
It doesn't work even if you have an input file!

The problem is that this works:

<xsl:for-each select="$root">
<xsl:apply-templates select="key('hostnames',$letter)" />
</xsl:for-each>
but when I put that code inside another for-each it doesn't work anymore:

<xsl:for-each select="$alphabet">
<xsl:variable name="letter" select="." />
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames',$letter)" />
</xsl:for-each>
If you want to try you can use as input:

<hosts>
<host>
<name>aasd/name>
</host>
<host>
<name>azsddz</name>
</host>
<host>
<name>bfsds</name>
</host>
<host>
<name>casd</name>
</host>
....
</hosts>
Thanks,
Giacomo.
Aug 12 '06 #4
Joe Kesselman wrote:
I suspect you meant something more like:
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames',$letter)" />
Of course, I was just trying to analyze the problem without causing
other possible problems :)

Giacomo.
Aug 12 '06 #5
The following is producing expected output for me. I suspect your
problem isn't the scan, but what you're doing with the values you've
retrieved from the key. Either that, or you've got a broken XSLT
implementation...?
Stylesheet:

<xsl:stylesheet version="1.0" xmlns:my="my.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="hostnames" match="host/name" use="substring(., 1, 1)" />
<my:alphabet xmlns:my="my.com">
<l>a</l><l>b</l><l>c</l><l>d</l><l>e</l><l>f</l><l>g</l><l>h</l><l>i</l><l>j</l><l>k</l><l>l</l><l>m</l><l>n</l><l>o</l><l>p</l><l>q</l><l>r</l><l>s</l><l>t</l><l>u</l><l>v</l><l>w</l><l>x</l><l>y</l><l>z</l>
</my:alphabet>

<xsl:variable name="root" select="/"/>
<xsl:variable name="alphabet" select="document('')/*/my:alphabet/l"/>

<xsl:template match="/">
<xsl:call-template name="summary"/>
</xsl:template>
<xsl:template name="summary">
<SingleTest>
<xsl:for-each select="$root">
<xsl:apply-templates select="key('hostnames','b')" />
</xsl:for-each>
</SingleTest>

<MultipleTest>
<xsl:for-each select="$alphabet">
<xsl:variable name="letter" select="." />
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames',$letter)" />
</xsl:for-each>
</MultipleTest>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:

<?xml version="1.0" encoding="UTF-8"?>
<SingleTest xmlns:my="my.com"><name>bfsds
</name>
</SingleTest><MultipleTest xmlns:my="my.com">a: <name>aasd
</name>
<name>azsddz
</name>
b: <name>bfsds
</name>
c: <name>casd
</name>
d: e: f: g: h: i: j: k: l: m: n: o: p: q: r: s: t: u: v: w: x: y: z:
</MultipleTest>

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Aug 12 '06 #6
Joe Kesselman wrote:
The following is producing expected output for me. I suspect your
problem isn't the scan, but what you're doing with the values you've
retrieved from the key. Either that, or you've got a broken XSLT
implementation...?
I tried exactly your code and I obtain:

<SingleTest xmlns:my="my.com"><name>bfsds
</name>
</SingleTest><MultipleTest xmlns:my="my.com">a: b: c: d: e: f: g: h: i:
j: k: l: m: n: o: p: q: r: s: t: u: v: w: x: y: z: </MultipleTest>

so I think it's an awful and old implementation of XSLT. From phpinfo:

XSL enabled
libxslt Version 1.0.30
libxslt compiled against libxml Version 2.5.7
EXSLT enabled
libexslt Version 1.0.30

but unfortunately I can't change it because it's on a university server
and laboratory technicians don't want to update it...
Thank you very much for you patience,
Giacomo.
Aug 12 '06 #7
but when I put that code inside another for-each it doesn't work anymore:
>
<xsl:for-each select="$alphabet">
<xsl:variable name="letter" select="." />
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames',$letter)" />
</xsl:for-each>

The xsl:for-each instruction changes thje current document to the one, which
contains the node-set defined as the value of the variable $alphabet.

So, the current document is changed to the stylesheet document. It, most
probably, is not the input xml document and doesn't conatain any host/name
nodes.

This is why the key() function in the above code will be evaluated to the
empty node-set.

Most probably (guessing without having your source xml document), the
following will do what you intended:

<xsl:for-each select="$alphabet">
<xsl:variable name="letter" select="." />
<xsl:for-each select="$root">
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames',$letter)" />
</xsl:for-each>
</xsl:for-each>
Hope this helped.

Cheers,
Dimitre Novatchev
"Giacomo" <a@b.cdewrote in message news:eb**********@le1.cs.unibo.it...
Joe Kesselman wrote:
>I get the output
<?xml version="1.0" encoding="UTF-8"?>
...

a: b: c: d: e: f: g: h: i: j: k: l: m: n: o: p: q: r: s: t: u: v:
w: x: y: z:

(modulo the tabs, of course.) So your iteration is working fine. Of
course I don't have your input file (or the other parts of your
stylesheet), so it isn't surprising the key lookup of 'b' isn't finding
anything.

It doesn't work even if you have an input file!

The problem is that this works:

<xsl:for-each select="$root">
<xsl:apply-templates select="key('hostnames',$letter)" />
</xsl:for-each>
but when I put that code inside another for-each it doesn't work anymore:

<xsl:for-each select="$alphabet">
<xsl:variable name="letter" select="." />
<xsl:value-of select="$letter" />: <xsl:apply-templates
select="key('hostnames',$letter)" />
</xsl:for-each>
If you want to try you can use as input:

<hosts>
<host>
<name>aasd/name>
</host>
<host>
<name>azsddz</name>
</host>
<host>
<name>bfsds</name>
</host>
<host>
<name>casd</name>
</host>
...
</hosts>
Thanks,
Giacomo.

Aug 13 '06 #8

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

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
13
by: Mark Constant | last post by:
I posted this on topxml.com and nobody has responded so I was hoping somebody could help me here. I am following some tutorials in VS.NET. I created a XSD schema file and then from there created an...
3
by: Kevin Brown | last post by:
Is there anyway to generate this type of resulting HTML table from this XML using XSLT? Basically I need to be able to consult 2 trees of data to generate the HTML, but I have not been able to...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
3
by: shaun roe | last post by:
mild rant follows Working now for a couple of years with xslt and now xslt 2.0, does anyone else get the impression that xslt 2.0 somehow missed the point? Yes its got a fancy new data model...
10
by: rplobue | last post by:
im trying to get urllib2 to work on my server which runs python 2.2.1. When i run the following code: import urllib2 for line in urllib2.urlopen('www.google.com'): print line i will...
3
by: WideBoy | last post by:
Hi, I have a software generated schema which creates a few empty elements, e.g. <xsd:sequence/>. I would like to be able to delete these elements from the schema post-generation using XSLT. ...
2
by: RolfK | last post by:
Dear ALL, I need some help on xsl:sequence. I'm using the Altova XSLT processor, but I'm quite confident it is not an processor issue. It's probably my bad knowledge of xslt 2.0. I have probably...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.