I have never really used XSLT before and am looking for some advice.
I have the following items returned in XML from GSA box:
<MT N="searchCategories" V="Category 1"/>
<MT N="searchCategories" V="Category 2"/>
etc etc
There can be any amount of these categories.
I am just wondering if XSLT has any concept of an array?
If so:
- How could I enumerate all the nodes above into an array?
- How would I get the length of the array?
If not:
- Is there any work around available?
I believe I am using XSLT version 1.0
As explained by @Michael Kay, there is no array data structure supported by the XPath data model (XDM) both for XPath 1.0 and XPath 2.0.
However, it is possible to use an array-like syntax like this:
In XPath 1.0/2.0 one can define a variable to contain a specific set of nodes and these can be accessed by their position (in document order), specifying this position in a predicate.
Here is an example:
defines a variable named
vTransferswith value the node-set of alltransferelements each of which is a child of the top element of the XML document.Then:
selects the first element that is contained in
$vTransfers.selects the second element that is contained in
$vTransfers, …selects the node from
$vTransferswhose position, in document order, is equal to the value contained in the variable$k.In addition XPath 2.0 supports the concept of sequences. A sequence is like a list of items. An item can be of any type — not only node. The items in a sequence are ordered in the way they appear (are defined) in the sequence. If two items in a sequence are nodes, their order is still that defined in the sequence and this may be different from their document order.
Example:
then when referenced like this:
produces:
Remember: Although these synthactic constructs resemble selection of an item from an array, node-sets and sequences are not arrays. In particular, they typically lack the very fast access O(1) that an array has to its elements. In the case of node-sets and sequences the efficiency of accessing an item at a random position is typically O(N). This means that an algorithm that is O(N) when using an array, may be O(N^2) when using the array-like notations with node-sets or sequences.