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 "Lagomasino, Adolfo (Adolfo)" <ad...@lucent.com> on 2005/03/11 23:47:14 UTC

Sorting the nodes of a node set and still get a node set

I am having some trouble creating a variable that would contain a node-set with a desired order.
Consider the following document
<TABLE>
  <DATA_ITEM IS_KEY="false"/>
  <DATA_ITEM IS_KEY="true"/>
  <DATA_ITEM IS_KEY="false"/>
  <DATA_ITEM IS_KEY="false"/>
  <DATA_ITEM IS_KEY="true"/>
</TABLE>

I would like to create a node-set (not a node fragment) where the IS_KEY='true' nodes are listed first and then the rest. I tried using a variable with a copy-of but it creates a node fragment which limits the operations I can use (I need to use [] later in the code.

This is the code,
<xsl:variable name="field-set">
   <xsl:for-each select="DATA_ITEM">
        <xsl:sort select="@IS_KEY='true'"/>
        <xsl:sort select="@IS_KEY='false'"/>
        <xsl:copy-of select="."/>
   </xsl:for-each>
</xsl:variable>
The resulting order is what I want, but the it is a node-fragment, so I can not use [].

I tried the following:

<xsl:variable name="key_fields_set" select="DATA_ITEM[@IS_KEY='true']"/>
         
<xsl:variable name="data_fields_set" select="DATA_ITEM[[@IS_KEY='false']"/>

<xsl:variable name="field-set" select="$key_fields_set | $data_fields_set"/>

and to my surprise, the union restores the original order ??

Any suggestions would be appreciated.

Thanks,

Adolfo

Re: Sorting the nodes of a node set and still get a node set

Posted by Christine Li <jy...@ca.ibm.com>.
Hi, Adolfo

You can use nodeset extension function to cast a Result Tree Fragment to a 
node set. For more detail, please refer to 
http://xml.apache.org/xalan-j/extensionslib.html#nodeset

hope it helps,

Christine Li
XSLT Development
IBM Toronto Lab
Tel: (905)413-2601
Email: jycli@ca.ibm.com



"Lagomasino, Adolfo (Adolfo)" <ad...@lucent.com> 
03/11/2005 05:47 PM

To
xalan-j-users@xml.apache.org
cc

Subject
Sorting the nodes of a node set and still get a node set






I am having some trouble creating a variable that would contain a node-set 
with a desired order.
Consider the following document
<TABLE>
  <DATA_ITEM IS_KEY="false"/>
  <DATA_ITEM IS_KEY="true"/>
  <DATA_ITEM IS_KEY="false"/>
  <DATA_ITEM IS_KEY="false"/>
  <DATA_ITEM IS_KEY="true"/>
</TABLE>

I would like to create a node-set (not a node fragment) where the 
IS_KEY='true' nodes are listed first and then the rest. I tried using a 
variable with a copy-of but it creates a node fragment which limits the 
operations I can use (I need to use [] later in the code.

This is the code,
<xsl:variable name="field-set">
   <xsl:for-each select="DATA_ITEM">
        <xsl:sort select="@IS_KEY='true'"/>
        <xsl:sort select="@IS_KEY='false'"/>
        <xsl:copy-of select="."/>
   </xsl:for-each>
</xsl:variable>
The resulting order is what I want, but the it is a node-fragment, so I 
can not use [].

I tried the following:

<xsl:variable name="key_fields_set" select="DATA_ITEM[@IS_KEY='true']"/>
 
<xsl:variable name="data_fields_set" 
select="DATA_ITEM[[@IS_KEY='false']"/>

<xsl:variable name="field-set" select="$key_fields_set | 
$data_fields_set"/>

and to my surprise, the union restores the original order ??

Any suggestions would be appreciated.

Thanks,

Adolfo