[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

building a nodeset from selected nodes

Subject: building a nodeset from selected nodes
From: "Hall, Kurt" <khall@xxxxxxxxxxx>
Date: Wed, 4 Jul 2001 12:52:04 -0600
xsl select nodes by query
As is often the case, I found a solution while preparing this post to
ask for help.  I think there may be a simpler solution however (using
XPATH/predicates?), plus I'd like to be able to encapsulate my solution 
in a call-template so I can reuse it.   (As a newby I'd appreciate
any other recommendations on usage, robustness, performance, etc.).

I'm trying to generate a node-set from my input where the selection
is specified by a list of elements.  Complicating matters somewhat is that
the company I'm consulting for is using an older version of xalan that
doesn't
appear to support the nodeset() extension.

For example, given the input:

	<table>
	   <column>
	      <name>A</name>
 	      <type>int</type>
 	   </column>
	   <column>
	      <name>B</name>
 	      <type>long</type>
 	   </column>
	   <column>
	      <name>C</name>
 	      <type>double</type>
 	   </column>
	   <column>
	      <name>D</name>
 	      <type>string</type>
 	   </column>
	   ...

	   <query>
	      <column>A</column>
	      <column>C</column>
	   </query>
	   <query>
	      <column>B</column>
	      <column>C</column>
	   <query>
	   ...
	</table>

When I process the "query" elements, I'd like to construct a node-set which
contains
only nodes 'A' and 'C' (and their children), do something with those nodes,
then process
the next "query" which contains 'B' and 'C'. etc.

In psuedo-SQL, this would be like "SELECT * FROM nodes WHERE name IN ('A',
'C')".

Here's the xsl I've come up with so far:

     ...
     <xsl:for-each select="table/query">
        <xsl:call-template name="get-column-nodes">
            <xsl:with-param name="nodes" select="/.."/>     <!-- create an
empty node-set -->
            <xsl:with-param name="columns" select="column"/>
        </xsl:call-template>
        ... <!-- I'd like to have the node-set here -->
     </xsl:for-each>
     ...

<xsl:template name="get-column-nodes">
    <xsl:param name="nodes"/>
    <xsl:param name="columns"/>
    <xsl:if test="$columns">
        <xsl:variable name="col" select="$columns[1]"/>
        <xsl:variable name="new" select="//table/column[name=$col]"/>
        <xsl:variable name="newnodes" select="$nodes | $new"/>
        <xsl:variable name="rest" select="$columns[position()!=1]"/>
        <xsl:choose>
        <xsl:when test="not($rest)">
            DO SOMETHING WITH THESE NODES...
            <xsl:for-each select="$newnodes">
                <xsl:value-of select="."/>
            </xsl:for-each>
            DONE
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="get-column-nodes">
                <xsl:with-param name="nodes" select="$newnodes"/>
                <xsl:with-param name="columns" select="$rest"/>
            </xsl:call-template>
        </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
</xsl:template>

Thanks much for any help.
-kurt


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.