Does XSLT have any concept of an array?

2020-03-31 11:58发布

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

2条回答
冷血范
2楼-- · 2020-03-31 12:18

XSLT 2.0 supports sequences, which are similar in many ways to arrays. Sequences can contain nodes or atomic values (examples of atomic values are strings, numbers, dates, etc). The data structures in XSLT 1.0 are much more limited; you're basically limited to modelling things as XML trees.

However, you're asking the wrong question. Given your limited experience with the language, it would be better to describe the output you want your program to produce for a given input, and ask for advice on how to go about doing the transformation.

查看更多
SAY GOODBYE
3楼-- · 2020-03-31 12:27

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:

<xsl:variable name="vTransfers" select="/*/transfer"/>

defines a variable named vTransfers with value the node-set of all transfer elements each of which is a child of the top element of the XML document.

Then:

$vTransfers[1]

selects the first element that is contained in $vTransfers.

$vTransfers[2]

selects the second element that is contained in $vTransfers, ...

$vTransfers[position() = $k]

selects the node from $vTransfers whose 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:

<xsl:variable name="vNumList" as="xs:integer*" select="3, 5, 7"/>

then when referenced like this:

$vNumlist[2]

produces:

5

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.

查看更多
登录 后发表回答