XSL - Loop through data twice processing differently

M

m0nkeymafia

I have an XSL stylesheet that process an XML document once, and
outputs it how I want it to...fine.
I would like to also populate a "table of contents" based on what XML
is there, which would require looping through the data a second time
[actually this should be done first] and processing it differently.

My question is how can I loop through the data twice [or can it not be
done?]
And if so how do I specify which templates it should use on which loop
round?

Thanks in advance
Chris
 
P

Pavel Lepin

Please don't double-post. We heard you just fine the first
time.

m0nkeymafia said:
I have an XSL stylesheet that process an XML document
once, and outputs it how I want it to...fine.
I would like to also populate a "table of contents" based
on what XML is there, which would require looping through
the data a second time
[actually this should be done first] and processing it
[differently.

The term 'looping' is largely inappropriate in XSLT context.
My question is how can I loop through the data twice [or
can it not be done?]
And if so how do I specify which templates it should use
on which loop round?

Use modes. Should be something along the lines of:

<xsl:template match="root">
<result>
<toc>
<xsl:apply-templates select="item" mode="toc"/>
</toc>
<content>
<xsl:apply-templates select="item"/>
</content>
</result>
</xsl:template>
 
M

m0nkeymafia

Please don't double-post. We heard you just fine the first
time.

I have an XSL stylesheet that process an XML document
once, and outputs it how I want it to...fine.
I would like to also populate a "table of contents" based
on what XML is there, which would require looping through
the data a second time
[actually this should be done first] and processing it
[differently.

The term 'looping' is largely inappropriate in XSLT context.
My question is how can I loop through the data twice [or
can it not be done?]
And if so how do I specify which templates it should use
on which loop round?

Use modes. Should be something along the lines of:

<xsl:template match="root">
<result>
<toc>
<xsl:apply-templates select="item" mode="toc"/>
</toc>
<content>
<xsl:apply-templates select="item"/>
</content>
</result>
</xsl:template>

I didnt post twice something messed up whilst posting!
Thanks for the reply that looks like itll work a treat, ill look into
modes!

How else should I refer to parsing [or looping] through an XML
document parsing it with XSL? I see it as looping round, perhaps I am
making a fundamental mistake with that?

Cheers
 
P

Pavel Lepin

m0nkeymafia said:
I have an XSL stylesheet that process an XML document
once, and outputs it how I want it to...fine.
I would like to also populate a "table of contents"
based on what XML is there, which would require looping
through the data a second time
[actually this should be done first] and processing it
[differently.

The term 'looping' is largely inappropriate in XSLT
context.

How else should I refer to parsing [or looping] through an
XML document parsing it with XSL? I see it as looping
round, perhaps I am making a fundamental mistake with
that?

The fundamental mistake is that 'looping' is usually
interpreted as going iteratively over a collection of
objects, doing something with each of them in turn. XSLT
spec contains no order-of-execution guarantee. Template
application to a nodeset may be performed in any order,
even simultaneously on several CPUs. Thinking of
apply-templates or for-each as loops invites the fallacy
common among the people new to XSLT: that is, thinking of
them as of stateful loops, similar to foreach() constructs
in imperative languages, while they're anything but that.
Common problems solved by statefulness of loops in other
languages (such as a trivial case of alternating row
backgrounds in a table) have vastly different solutions in
XSLT.
 
M

m0nkeymafia

<[email protected]>:


<[email protected]>:
I have an XSL stylesheet that process an XML document
once, and outputs it how I want it to...fine.
I would like to also populate a "table of contents"
based on what XML is there, which would require looping
through the data a second time
[actually this should be done first] and processing it
[differently.
The term 'looping' is largely inappropriate in XSLT
context.
How else should I refer to parsing [or looping] through an
XML document parsing it with XSL? I see it as looping
round, perhaps I am making a fundamental mistake with
that?

The fundamental mistake is that 'looping' is usually
interpreted as going iteratively over a collection of
objects, doing something with each of them in turn. XSLT
spec contains no order-of-execution guarantee. Template
application to a nodeset may be performed in any order,
even simultaneously on several CPUs. Thinking of
apply-templates or for-each as loops invites the fallacy
common among the people new to XSLT: that is, thinking of
them as of stateful loops, similar to foreach() constructs
in imperative languages, while they're anything but that.
Common problems solved by statefulness of loops in other
languages (such as a trivial case of alternating row
backgrounds in a table) have vastly different solutions in
XSLT.

Very interesting cheers
I can't seem to find any decent tutorials past the basics that would
outline such methods...I use XSL as if it were a state based
programming language like you suggest but want to learn how to use it
properly :)

Know any good tutorial sites? Everything on google seems to be w3 or
forum based.

Thanks again
 
P

Pavel Lepin

m0nkeymafia said:
I have an XSL stylesheet that process an XML
document once, and outputs it how I want it
to...fine. I would like to also populate a "table of
contents" based on what XML is there, which would
require looping through the data a second time
[actually this should be done first] and processing
[it differently.
The term 'looping' is largely inappropriate in XSLT
context.
How else should I refer to parsing [or looping] through
an XML document parsing it with XSL? I see it as
looping round, perhaps I am making a fundamental
mistake with that?

The fundamental mistake is that 'looping' is usually
interpreted as going iteratively over a collection of
objects, doing something with each of them in turn. XSLT
spec contains no order-of-execution guarantee. Template
application to a nodeset may be performed in any order,
even simultaneously on several CPUs. Thinking of
apply-templates or for-each as loops invites the fallacy
common among the people new to XSLT: that is, thinking of
them as of stateful loops, similar to foreach()
constructs in imperative languages, while they're
anything but that. Common problems solved by statefulness
of loops in other languages (such as a trivial case of
alternating row backgrounds in a table) have vastly
different solutions in XSLT.

I can't seem to find any decent tutorials past the basics
that would outline such methods...I use XSL as if it were
a state based programming language like you suggest but
want to learn how to use it properly :)

Know any good tutorial sites? Everything on google seems
to be w3 or forum based.

I'm unaware of any good, comprehensive kick-start XSLT
tutorials. Stay away from w3schools, their tutorials will
cripple you for life.

IBM's developerWorks at:

http://www.ibm.com/xml/xslt/

....has a section on XSLT. There's over a hundred articles
there, so don't get lost.

XSLT FAQ is an invaluable resource, but fishing the info you
need out of it is quite an art in itself:

http://www.dpawson.co.uk/xsl/sect2/sect21.html

W3C's XSLT/XPath specs are the ultimate reference materials.
Always keep them handy. Note that it's important to have
good working knowledge of XPath to make full use of XSLT
capabilities.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,008
Messages
2,570,270
Members
46,874
Latest member
CyberGateway

Latest Threads

Top