node-set problems

Discussion in 'XML' started by Eric, Nov 29, 2004.

  1. Eric

    Eric Guest

    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
     
    Eric, Nov 29, 2004
    #1
    1. Advertising

  2. "Eric" <> writes:

    > 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
     
    David Carlisle, Nov 30, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wolf
    Replies:
    0
    Views:
    564
  2. Replies:
    0
    Views:
    1,568
  3. Alan Silver
    Replies:
    0
    Views:
    537
    Alan Silver
    Dec 21, 2005
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,458
    Dimitre Novatchev
    Aug 24, 2006
  5. Replies:
    0
    Views:
    524
Loading...

Share This Page