param containing nodes and for-each

Discussion in 'XML' started by Jean-Christophe Michel, Jul 12, 2003.

  1. Hi,

    I have a stylesheet with an accumulator like this

    <xsl:param name="new-x">
    <xsl:for-each select="anode">
    <mynode>
    <xsl:attribute name="attr">
    <xsl:value select="anode/atag" />
    </xsl:attribute>
    </mynode>
    </xsl:for-each>
    <xsl:copy-of select="$x" />
    </xsl:param>

    if I test with
    <print-x>
    <xsl:copy-of select="$new-x" />
    </print-x>

    i get the expected output:

    <print-x>
    <mynode attr="foo1" />
    <mynode attr="foo2" />
    <mynode attr="foo3" />
    </print-x>

    but later in the same template

    count($new-x) returns 1

    and

    <xsl:for-each select="$new-x">
    <xsl-value-of select="@attr" />
    </xsl:for-each>

    returns an empty result...

    Some tips ?

    I use xsltproc under debian sid.
     
    Jean-Christophe Michel, Jul 12, 2003
    #1
    1. Advertising

  2. Jean-Christophe Michel

    Marrow Guest

    Hi,

    > I have a stylesheet with an accumulator like this
    >
    > <xsl:param name="new-x">
    > <xsl:for-each select="anode">
    > <mynode>
    > <xsl:attribute name="attr">
    > <xsl:value select="anode/atag" />
    > </xsl:attribute>
    > </mynode>
    > </xsl:for-each>
    > <xsl:copy-of select="$x" />
    > </xsl:param>
    > [snip]
    > but later in the same template
    >
    > count($new-x) returns 1


    With most transformation engines that would actually cause an error -
    because $new-x contains an RTF (result tree fragment) but the count()
    function takes a node-set as an argument. So it looks like your
    transformation engine, xsltproc, is doing automatic RTF to node-set
    conversions.

    Assuming that it is the case (that automatic RTF to node-set conversions are
    occuring) then a result of 1 would be correct. This is because an RTF, like
    an XML document, has a single root node.

    Therefore, if you try...
    <xsl:value-of select="count($new-x/*)"/>
    or...
    <xsl:value-of select="count($new-x/mynode)"/>

    you will most likely get the result you expect.

    Also...

    > <xsl:for-each select="$new-x">
    > <xsl-value-of select="@attr" />
    > </xsl:for-each>


    You probably want...
    <xsl:for-each select="$new-x/*">
    or...

    <xsl:for-each select="$new-x/mynode">

    Hope this helps
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator


    "Jean-Christophe Michel" <> wrote in message
    news:p...
    > Hi,
    >
    > I have a stylesheet with an accumulator like this
    >
    > <xsl:param name="new-x">
    > <xsl:for-each select="anode">
    > <mynode>
    > <xsl:attribute name="attr">
    > <xsl:value select="anode/atag" />
    > </xsl:attribute>
    > </mynode>
    > </xsl:for-each>
    > <xsl:copy-of select="$x" />
    > </xsl:param>
    >
    > if I test with
    > <print-x>
    > <xsl:copy-of select="$new-x" />
    > </print-x>
    >
    > i get the expected output:
    >
    > <print-x>
    > <mynode attr="foo1" />
    > <mynode attr="foo2" />
    > <mynode attr="foo3" />
    > </print-x>
    >
    > but later in the same template
    >
    > count($new-x) returns 1
    >
    > and
    >
    > <xsl:for-each select="$new-x">
    > <xsl-value-of select="@attr" />
    > </xsl:for-each>
    >
    > returns an empty result...
    >
    > Some tips ?
    >
    > I use xsltproc under debian sid.
     
    Marrow, Jul 13, 2003
    #2
    1. Advertising

  3. On Sun, 13 Jul 2003 08:27:57 +0100, Marrow wrote:
    > With most transformation engines that would actually cause an error -
    > because $new-x contains an RTF (result tree fragment) but the count()
    > function takes a node-set as an argument. So it looks like your
    > transformation engine, xsltproc, is doing automatic RTF to node-set
    > conversions.
    >
    > Assuming that it is the case (that automatic RTF to node-set conversions
    > are occuring) then a result of 1 would be correct. This is because an
    > RTF, like an XML document, has a single root node.
    >
    > Therefore, if you try...
    > <xsl:value-of select="count($new-x/*)"/>
    > or...
    > <xsl:value-of select="count($new-x/mynode)"/>
    >
    > you will most likely get the result you expect.


    No... i thought if it, but i only get

    XPath error Invalid type in count($new-x/*)
    or
    XPath error Invalid type in count($new-x/mynode)

    >
    > Also...
    >
    >> <xsl:for-each select="$new-x">
    >> <xsl-value-of select="@attr" />
    >> </xsl:for-each>

    >
    > You probably want...
    > <xsl:for-each select="$new-x/*">
    > or...
    >
    > <xsl:for-each select="$new-x/mynode">


    I tried all this; even a
    <xsl:call-template name="another" select="$new-x" />
    or
    <xsl:apply-templates select="$new-x" />
    with
    <xsl:template match="mynode" >

    but in the 'another' template i cannot do more.

    I think it's very strange to be able to place some xml into a param but
    not being able to parse it later as real xml :(
    said differently, this distinction between node-set and result tree
    fragment seems to me like weak point of xsl; i cannot see its advantage.

    I'll have to output the param content in a temp file then call a second
    xsl :(
     
    Jean-Christophe Michel, Jul 13, 2003
    #3
  4. Jean-Christophe Michel

    Marrow Guest

    Hi,

    In that case your transformatin engine is not, after all, performing
    automatic RTF to node-set conversions. In which case...
    <xsl:value-of select="count($new-x)"/>
    should cause an error.

    Anyway, as you are dealing with an RTF...

    In most transformation engines they supply an extension function (e.g.
    xxx:node-set() or xxx:nodeset() etc.) that allow you to convert an RTF to a
    node-set. You will have to check the documentation for your transformation
    engine.

    > I think it's very strange to be able to place some xml into a param but
    > not being able to parse it later as real xml :(
    > said differently, this distinction between node-set and result tree
    > fragment seems to me like weak point of xsl; i cannot see its advantage.


    Probably the reason that the whole concept of RTFs has gone in XSLT 2.0 ;)

    Cheers
    Marrow

    "Jean-Christophe Michel" <> wrote in message
    news:p...
    > On Sun, 13 Jul 2003 08:27:57 +0100, Marrow wrote:
    > > With most transformation engines that would actually cause an error -
    > > because $new-x contains an RTF (result tree fragment) but the count()
    > > function takes a node-set as an argument. So it looks like your
    > > transformation engine, xsltproc, is doing automatic RTF to node-set
    > > conversions.
    > >
    > > Assuming that it is the case (that automatic RTF to node-set conversions
    > > are occuring) then a result of 1 would be correct. This is because an
    > > RTF, like an XML document, has a single root node.
    > >
    > > Therefore, if you try...
    > > <xsl:value-of select="count($new-x/*)"/>
    > > or...
    > > <xsl:value-of select="count($new-x/mynode)"/>
    > >
    > > you will most likely get the result you expect.

    >
    > No... i thought if it, but i only get
    >
    > XPath error Invalid type in count($new-x/*)
    > or
    > XPath error Invalid type in count($new-x/mynode)
    >
    > >
    > > Also...
    > >
    > >> <xsl:for-each select="$new-x">
    > >> <xsl-value-of select="@attr" />
    > >> </xsl:for-each>

    > >
    > > You probably want...
    > > <xsl:for-each select="$new-x/*">
    > > or...
    > >
    > > <xsl:for-each select="$new-x/mynode">

    >
    > I tried all this; even a
    > <xsl:call-template name="another" select="$new-x" />
    > or
    > <xsl:apply-templates select="$new-x" />
    > with
    > <xsl:template match="mynode" >
    >
    > but in the 'another' template i cannot do more.
    >
    > I think it's very strange to be able to place some xml into a param but
    > not being able to parse it later as real xml :(
    > said differently, this distinction between node-set and result tree
    > fragment seems to me like weak point of xsl; i cannot see its advantage.
    >
    > I'll have to output the param content in a temp file then call a second
    > xsl :(
     
    Marrow, Jul 13, 2003
    #4
  5. On Sun, 13 Jul 2003 10:03:16 +0100, Marrow wrote:

    > Hi,
    >
    > In that case your transformatin engine is not, after all, performing
    > automatic RTF to node-set conversions. In which case...
    > <xsl:value-of select="count($new-x)"/>
    > should cause an error.


    Strange, but it doesn't :)

    > Anyway, as you are dealing with an RTF...
    >
    > In most transformation engines they supply an extension function (e.g.
    > xxx:node-set() or xxx:nodeset() etc.) that allow you to convert an RTF to
    > a node-set. You will have to check the documentation for your
    > transformation engine.


    It' xsltproc, based on libxslt (for Gnome), but no mention of this...

    Thanks, i could use it with the name space
    xmlls:exslt="http://exslt.org/common"

    then call exslt:node-set($myparam)
    in all expressions.
     
    Jean-Christophe Michel, Jul 13, 2003
    #5
    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. asd
    Replies:
    3
    Views:
    461
    Arnaud Berger
    May 23, 2005
  2. Geathaa
    Replies:
    2
    Views:
    727
    Geathaa
    Jul 30, 2003
  3. kurt hansen
    Replies:
    1
    Views:
    1,063
    David Carlisle
    Sep 7, 2004
  4. ectoplasm
    Replies:
    12
    Views:
    671
    Zorro
    Jul 28, 2005
  5. Dave
    Replies:
    5
    Views:
    753
    John Bokma
    Apr 26, 2011
Loading...

Share This Page