This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Convert string to a list of nodes


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]