node-set problems

E

Eric

I am trying to figure out a good way to implement a XSLT
transformation. Basically my goal is that I want to be able to ouput
the following XML in a document:

<chart type="pie" width="100" height="100">
<data label="Foo" value="12"/>
<data label="Bar" value="15"/>
.....
</chart>

This should transform into HTML which looks like:

<img width="100" height="100"
src="chart.rhtml?type=pie&width=100&height=100&data1=Foo&label1=12&data2=Bar&label2=15"/>

chart.rhtml is a server-side script that reads the URL parameters and
generates a graphical chart in PNG format. The goal is to be able to
easily slap charts in pages. To generate the URL string my first
thought was to use <xsl:for-each>. But when attempting to do that I ran
into the problem of not being able to store the string built so far
because xsl:variable cannot be modified.

So my next attempt was to create a named template that calls itself
recusively passing the string built thus far as one of the parameters.
When it is on the last "data" tag is simply outputs the string that was
built. This works fine when using the XSLT engine of my choice
because I can use xxx:node-set to convert the data nodes passed into
the named template to a node set so that I can use the variable in
XPath queries. But when I attempt this method on Mozilla's XSLT engine
it fails because it does not seem to support a xxx:node-set function.
So how do I implement this transformation using straight XSLT 1.0 code
and have it work in Mozilla's XSLT engine.
Any ideas? I appreciate any help that I can get.

Eric
 
D

David Carlisle

Eric said:
I am trying to figure out a good way to implement a XSLT
transformation. Basically my goal is that I want to be able to ouput
the following XML in a document:

<chart type="pie" width="100" height="100">
<data label="Foo" value="12"/>
<data label="Bar" value="15"/>
....
</chart>

This should transform into HTML which looks like:

<img width="100" height="100"
src="chart.rhtml?type=pie&width=100&height=100&data1=Foo&label1=12&data2=Bar&label2=15"/>

chart.rhtml is a server-side script that reads the URL parameters and
generates a graphical chart in PNG format. The goal is to be able to
easily slap charts in pages. To generate the URL string my first
thought was to use <xsl:for-each>. But when attempting to do that I ran
into the problem of not being able to store the string built so far
because xsl:variable cannot be modified.

So my next attempt was to create a named template that calls itself
recusively passing the string built thus far as one of the parameters.
When it is on the last "data" tag is simply outputs the string that was
built. This works fine when using the XSLT engine of my choice
because I can use xxx:node-set to convert the data nodes passed into
the named template to a node set so that I can use the variable in
XPath queries. But when I attempt this method on Mozilla's XSLT engine
it fails because it does not seem to support a xxx:node-set function.
So how do I implement this transformation using straight XSLT 1.0 code
and have it work in Mozilla's XSLT engine.
Any ideas? I appreciate any help that I can get.

Eric

clearly you need to generate
<img width="100" height="100"
src="chart.rhtml?type=pie&amp;width=100&amp;height=100&amp;data1=Foo&amp;label1=12&amp;data2=Bar&amp;label2=15"/>

otherwise it wouldn't be well formed XML (or HTML). I don't understand
your comment about xx:node-set. mozilla doesn't support it but if you
are passing nodes into the template they are already a node set so you
don't need any extension function. xx:node-set is only used to coerce
result tree fragments to node sets ie nodes generated during the
transform, not nodes selected from the source. The fact that variables
can not be updated is not a restriction (it never is) and in this case
you don't need any variables at all, I think you just need something
like this (untested)

<xsl:template match="chart">
<img>
<xsl:attribute name="src">
<xsl:text>chart.rhtml?type=</xsl:text>
<xsl:value-of select="@type"/>
<xsl:text>&amp;width=</xsl:text>
<xsl:value-of select="@width"/>
<xsl:text>&amp;height=</xsl:text>
<xsl:value-of select="@height"/>
<xsl:for-each select="data">
<xsl:text>&amp;data</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>=</xsl:text>
<xsl:value-of select="@label"/>
<xsl:text>&amp;label</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>=</xsl:text>
<xsl:value-of select="@value"/>
</xsl:for-each>
</xsl:attribute>
</img>
</xsl:template>


David
 

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
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top