Is there anyway to create a multi dimensional array like we do in procedural programming a[4,4,4] in XSLT, and if yes can anyone give a sample for that!
-
What for? What are your needs?Kirill Polishchuk– Kirill Polishchuk2011-12-10 19:06:59 +00:00Commented Dec 10, 2011 at 19:06
-
I'm implementing the CYK algorithm (en.wikipedia.org/wiki/CYK_algorithm) in XSLT and I do need an array for that.Peymankh– Peymankh2011-12-10 19:08:47 +00:00Commented Dec 10, 2011 at 19:08
2 Answers
Beyond simple sequences, the only way of implementing complex data structures in XSLT 2.0 is as XML trees. One way of implementing a three-dimensional array would be as a set of labelled cells:
<cell i="1" j="2" k="3">value</cell>
Alternatively you could use a positional representation:
<plane>
<row>
<cell>value</cell>
<cell>value</cell>
</row>
<row>
...
The problem with implementing algorithms such as CYK is that they are designed to use mutable data structures. This means that for efficiency, you often need something different in a functional language. For example, changing the content of one cell in the three-dimensional array may (depending on the implementation) involve making a copy of the whole structure. That doesn't make functional programming intrinsically inefficient - it just means that you sometimes have to design different algorithms to take advantage of its strengths.
The constraint you've been given for this exercise - telling you what algorithm to use - is one that should never be included in a requirements statement. The requirements should describe the problem to be solved, and not constrain the way you choose to solve it.
Comments
The short answer is negative.
There is nothing "procedural" in a multidimensional array. Simply, XPath is a query (navigational) language for tree structures (XML documents) and tree structures have almost nothing in common with mutlti-dimensional arrays.
Tree structures are just 2-dimensional.
This said, one can build analogies (models) to multi-dimensional objects:
<t>
<a>
<b>
<c>11</c>
<c>12</c>
<c>13</c>
<c>14</c>
</b>
<b>
<c>15</c>
<c>16</c>
<c>17</c>
<c>18</c>
</b>
<b>
<c>19</c>
<c>20</c>
<c>21</c>
<c>22</c>
</b>
<b>
<c>23</c>
<c>24</c>
<c>25</c>
<c>26</c>
</b>
</a>
<a>
<b>
<c>27</c>
<c>28</c>
<c>29</c>
<c>30</c>
</b>
<b>
<c>31</c>
<c>32</c>
<c>33</c>
<c>34</c>
</b>
<b>
<c>35</c>
<c>36</c>
<c>37</c>
<c>38</c>
</b>
<b>
<c>39</c>
<c>40</c>
<c>41</c>
<c>42</c>
</b>
</a>
<a>
<b>
<c>43</c>
<c>44</c>
<c>45</c>
<c>46</c>
</b>
<b>
<c>47</c>
<c>48</c>
<c>49</c>
<c>50</c>
</b>
<b>
<c>51</c>
<c>52</c>
<c>53</c>
<c>54</c>
</b>
<b>
<c>55</c>
<c>56</c>
<c>57</c>
<c>58</c>
</b>
</a>
<a>
<b>
<c>59</c>
<c>60</c>
<c>61</c>
<c>62</c>
</b>
<b>
<c>63</c>
<c>64</c>
<c>65</c>
<c>66</c>
</b>
<b>
<c>67</c>
<c>68</c>
<c>69</c>
<c>70</c>
</b>
<b>
<c>71</c>
<c>72</c>
<c>73</c>
<c>74</c>
</b>
</a>
</t>
When we evaluate against the XML document above this XPath expression:
/*/a[4]/*[4]/*[4]
the selected node is:
<c>74</c>
A complete XSLT transformation example, using such XPath expression:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:value-of select="a[4]/*[4]/*[4]"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the XML document above, the wanted, correct result is produced:
74
Update: The OP has revealed in a comment that he needs the multidimensional array capability in order to implement a general parsing algorithm in XSLT.
I would advise to try to implement a simpler and more efficient parsing algorithm, such as LALR(1) parsing.
Here are some advantages LALR(1) parsers:
- An LALR parser can be automatically generated from an LALR grammar.
- An LALR grammar can be used to define many computer languages.
- An LALR parser is small.
- An LALR parser is fast (if the parsing algorithm uses a matrix parser-table format).
- An LALR parser is linear in speed (i.e. the speed is based on the size of the input text file only and not based on the size of the language being recognized).
- The LALR grammar provides valuable documentation of the language being recognized.
- Error recovery may already be built-in to the parser.
- Generalized error messages may already be built into the parser.
- Abstract-syntax-tree construction may already be built into the parser.
- Recognition of context-sensitive language constructs may already be built into the parser.
A general LALR(1) parser is already implemented in pure XSLT 2.0 and is part of FXSL.
5 Comments
matr(M, ind1, ind2, ind3) and implement this extension function in another, conventional PL.document(). Therefore, an XML model of a multi-dimensional array is fully possible and probably is the only possible way to implement "something like a multi-dimensional array" purely (not using extension functions) in XSLT.