You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Raymond <ra...@doublebite.com> on 2007/08/10 18:55:19 UTC
Adding an attribute to elements of a dynamic result-set
Hello All,
I'm wondering if there is anyway to achieve something like this:
<xsl:variable name="category_defs">
<xsl:for-each select="$category_defs1" >
<xsl:copy>
<xsl:attribute name="mode">1</xsl:attribute>
</xsl:copy>
</xsl:for-each>
<xsl:for-each select="$category_defs2" >
<xsl:copy>
<xsl:attribute name="mode">2</xsl:attribute>
</xsl:copy>
</xsl:for-each>
</xsl:variable>
Essentially, what I'm trying to do is merge the two result sets into one
interleaved set, while maintaining the distinction between the two by
applying a "mode" attribute.
Realizing that there is a mutability issue somewhere in here, I tried
the copy, but no dice. Is this possible in anyway?
Currently I get a
SystemId Unknown; Line #19; Column #38; Can not convert #RTREEFRAG to a
NodeList!
message or stderr.
Thanks in advance,
Raymond Auge <ra...@liferay.com>
Software Engineer
Liferay, Inc.
Enterprise. Open Source. For Life.
Re: Adding an attribute to elements of a dynamic result-set
Posted by Raymond <ra...@doublebite.com>.
Hey Henry,
I took only a slightly different approach (still using the node-set
extension) to solve my problem:
<xsl:variable name="cat_temp">
<xsl:for-each select="$category_blog" >
<xsl:element name="category">
<xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute>
<xsl:attribute name="type"><xsl:value-of select="@type" /></xsl:attribute>
<xsl:attribute name="mode">blog</xsl:attribute>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="$category_podcast" >
<xsl:element name="category">
<xsl:attribute name="name"><xsl:value-of select="@name" /></xsl:attribute>
<xsl:attribute name="type"><xsl:value-of select="@type" /></xsl:attribute>
<xsl:attribute name="mode">podcast</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="category_defs" select="exsl:node-set($cat_temp)/category" />
It wasn't important that the nodes be actually copied, so much as the
data be collected into a common set. Later I just sort these and the
results are interleaved exactly as I wanted.
Thanks,
Ray
On Mon, 2007-08-13 at 09:17 -0400, Henry Zongaro wrote:
>
> Hi, Raymond.
>
> Raymond <ra...@doublebite.com> wrote on 2007-08-10 12:55:19 PM:
> > I'm wondering if there is anyway to achieve something like this:
> >
> > <xsl:variable name="category_defs">
> > <xsl:for-each select="$category_defs1" >
> > <xsl:copy>
> > <xsl:attribute name="mode">1</xsl:attribute>
> > </xsl:copy>
> > </xsl:for-each>
> > <xsl:for-each select="$category_defs2" >
> > <xsl:copy>
> > <xsl:attribute name="mode">2</xsl:attribute>
> > </xsl:copy>
> > </xsl:for-each>
> > </xsl:variable>
> >
>
> > Essentially, what I'm trying to do is merge the two result sets into
> > one interleaved set, while maintaining the distinction between the
> > two by applying a "mode" attribute.
>
> According to section 11.1 of XSLT 1.0,[1] "An operation is permitted
> on a result tree fragment only if that operation would be permitted on
> a string (the operation on the string may involve first converting the
> string to a number or boolean)." So in standard XSLT, you can't do
> it. To be able to access the nodes in a result tree fragment and
> directly manipulate them, you need to use the node-set extension
> function. Something like the following should work:
>
> <xsl:variable name="category_defs"
> xmlns:exslt="http://exslt.org/common">
> <xsl:for-each select="exslt:node-set($category_defs1)/node()" >
> <xsl:copy>
> <xsl:attribute name="mode">1</xsl:attribute>
> </xsl:copy>
> </xsl:for-each>
> <xsl:for-each select="exslt:node-set($category_defs2)/node()" >
> <xsl:copy>
> <xsl:attribute name="mode">2</xsl:attribute>
> </xsl:copy>
> </xsl:for-each>
> </xsl:variable>
>
> I hope that helps.
>
> Thanks,
>
> Henry
> [1] http://www.w3.org/TR/xslt#section-Result-Tree-Fragments
> ------------------------------------------------------------------
> Henry Zongaro XSLT Processors Development
> IBM SWS Toronto Lab T/L 969-6044; Phone +1 905 413-6044
> mailto:zongaro@ca.ibm.com
Raymond Auge <ra...@liferay.com>
Software Engineer
Liferay, Inc.
Enterprise. Open Source. For Life.
Re: Adding an attribute to elements of a dynamic result-set
Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Raymond.
Raymond <ra...@doublebite.com> wrote on 2007-08-10 12:55:19 PM:
> I'm wondering if there is anyway to achieve something like this:
>
> <xsl:variable name="category_defs">
> <xsl:for-each select="$category_defs1" >
> <xsl:copy>
> <xsl:attribute name="mode">1</xsl:attribute>
> </xsl:copy>
> </xsl:for-each>
> <xsl:for-each select="$category_defs2" >
> <xsl:copy>
> <xsl:attribute name="mode">2</xsl:attribute>
> </xsl:copy>
> </xsl:for-each>
> </xsl:variable>
>
> Essentially, what I'm trying to do is merge the two result sets into
> one interleaved set, while maintaining the distinction between the
> two by applying a "mode" attribute.
According to section 11.1 of XSLT 1.0,[1] "An operation is permitted on a
result tree fragment only if that operation would be permitted on a string
(the operation on the string may involve first converting the string to a
number or boolean)." So in standard XSLT, you can't do it. To be able to
access the nodes in a result tree fragment and directly manipulate them,
you need to use the node-set extension function. Something like the
following should work:
<xsl:variable name="category_defs"
xmlns:exslt="http://exslt.org/common">
<xsl:for-each select="exslt:node-set($category_defs1)/node()" >
<xsl:copy>
<xsl:attribute name="mode">1</xsl:attribute>
</xsl:copy>
</xsl:for-each>
<xsl:for-each select="exslt:node-set($category_defs2)/node()" >
<xsl:copy>
<xsl:attribute name="mode">2</xsl:attribute>
</xsl:copy>
</xsl:for-each>
</xsl:variable>
I hope that helps.
Thanks,
Henry
[1] http://www.w3.org/TR/xslt#section-Result-Tree-Fragments
------------------------------------------------------------------
Henry Zongaro XSLT Processors Development
IBM SWS Toronto Lab T/L 969-6044; Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com