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

Re: Convert string to a list of nodes

Subject: Re: Convert string to a list of nodes
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 4 Dec 2001 14:33:38 +0000
convert string to element
Hi Alexandra,

> I have a template that should perform some action for a set of
> nodes. This set can contain a various number of elements, and each
> element is unique within my XML document scope.

Assuming that the current node is the elem1 element, you can pick out
any of the elements named in the $nodelist parameter using:

  *[contains(concat(';', $nodelist, ';'),
             concat(';', name(), ';'))]

(Adding the ;s at the beginning and end of the node list and name of
the element ensures that you don't have problems with element names
being substrings of other element names.)

When you use xsl:for-each with these elements, you'll iterate over
them *in document order*, so calling the template with $nodelist equal
to:

  'node1;node2;node3;node4'

will give you exactly the same result as calling it with:

  'node3;node4;node2;node1'

and any other combination. If you want them to be sorted in the order
that they're named as well, you can sort on the number of characters
before the name of the element within the $nodelist that's passed as a
parameter, as follows:

  <xsl:for-each select="*[contains(concat(';', $nodelist, ';'),
                                   concat(';', name(), ';'))]">
    <xsl:sort data-type="number"
      select="string-length(
                substring-before(concat(';', $nodelist, ';'),
                                 concat(';', name(), ';')))" />
    ...
  </xsl:for-each>

> Or maybe there is another way to do, what I want to do, and my
> solution is not the best way ? ;-)

Well it would be a lot easier if you could pass in the node set that
you want rather than pass in a string of element names. For example,
if you really call it with:

<xsl:call-template name="maintemplate">
  <xsl:with-param name="nodelist">node1;node2;node3;node4</xsl:with-param>
   <!-- some params... -->
</xsl:call-template>

then you may as well do:

<xsl:call-template name="maintemplate">
  <xsl:with-param name="nodelist"
                  select="node1 | node2 | node3 | node4" />
   <!-- some params... -->
</xsl:call-template>

So that you actually pass in a node set to the maintemplate template
rather than the element names. The two reasons you could have for
passing in a string is (a) if the nodes that you wanted to pick out
where selected dynamically (so the content of the xsl:with-param was
actually a xsl:value-of or something) or (b) if you wanted to process
them in an order other than document order. In either case, you might
find that using matching templates rather than named templates gives
you what you need. It's hard to tell without knowing what you're
actually trying to do.

I hope that helps anyway,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 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.