4

I want to know how nodesets and sequences differ. Why are sequences considered better in xslt 2.0? Can someone please give me an example of a nodeset(with a select attribute) and how that can be converted to a sequence?

harsh
  • 2,399
  • 9
  • 29
  • 47

2 Answers2

5

Sequences in XSLT 2.0 are a generalization of node-sets in XSLT 1.0. They differ in two ways

(a) they can contain things other than nodes: for example, you can have a sequence of strings or integers

(b) they can contain duplicates, and can contain nodes in any order (node-sets are unordered).

It's not possible to convert a node-set to a sequence for the simple reason that XSLT 1.0 only has node-sets, and XSLT 2.0 only has sequences: an expression like /a/b which in XSLT 1.0 returned a node-set, will return a sequence in XSLT 2.0.

(If you want, you can use the term "node-set" to refer to any sequence that consists entirely of nodes, in which there are no duplicates, and in which the nodes are always in document order. But that terminology isn't used by the 2.0 spec.)

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
2

With XSLT/XPath 1.0 a location path returns a node set (http://www.w3.org/TR/xpath/#node-sets), with XSLT/XPath 2.0 a path expression (http://www.w3.org/TR/xpath20/#id-path-expressions) returns a sequence of items where an item can be a node or an atomic value. XSLT/XPath 2.0 does not have node sets so I can't provide an example on how to convert a nodeset to a sequence.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • thanks what I mean by example is this. Lets say I have something like 1 . Is the part "$style/df:table/df:Borders/df:top[not(@df:val = "xoxo")]" is a node set? (from what I understood) Can it be converted to use a sequence. – harsh Jun 28 '12 at 17:57
  • That is a path expression that with XPath 1.0 selects a set of nodes and with XPath 2.0 a sequence of nodes. I don't get what you want to convert, if you use XPath or XSLT 2.0 then you don't have any node-sets, you only have sequences of nodes. – Martin Honnen Jun 28 '12 at 18:05
  • I have that example in xslt 1.0. I want to change it so that it would give the same output in xslt 2.0. Do I make sense? :-/ – harsh Jun 28 '12 at 18:12
  • I don't see why a snippet of `1` would give a different result when run with an XSLT 2.0 processor compared to being run with an XSLT 1.0 processor. Do you get different results? – Martin Honnen Jun 28 '12 at 18:18
  • No I don't. I get the same results. I thought that can be changed for better performance in xslt 2.0. Is that wrong? – harsh Jun 28 '12 at 18:21
  • 1
    Sorry, I don't get what you are after. Maybe the response by Michael Kay helps to clarify your understanding. The only suggestion I have seen to improve performance (if the optimizer of the XSLT processor does not do it under the hood) is to rewrite expressions using the union operator with the sequence operator e.g. `` where you know you want to process first all attribute nodes, then all child nodes can be rewritten in XSLT 2.0 as ``. But for a single path I am not aware of a change to improve performance. – Martin Honnen Jun 29 '12 at 09:44