1

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!

2
  • What for? What are your needs? Commented 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. Commented Dec 10, 2011 at 19:08

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

  1. An LALR parser can be automatically generated from an LALR grammar.
  2. An LALR grammar can be used to define many computer languages.
  3. An LALR parser is small.
  4. An LALR parser is fast (if the parsing algorithm uses a matrix parser-table format).
  5. 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).
  6. The LALR grammar provides valuable documentation of the language being recognized.
  7. Error recovery may already be built-in to the parser.
  8. Generalized error messages may already be built into the parser.
  9. Abstract-syntax-tree construction may already be built into the parser.
  10. 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

It seems that I didn't make myself completely clear, I need to use an array structure in my XSLT file, meaning I iterate through that array of mine which is index based.
@Peymankh: XSLT uses XPath and XPath (1.0, 2.0 and even the forth-coming 3.0) doesn't have arrays. Therefore, there is no such direct capability in XPath. You could call an extension function matr(M, ind1, ind2, ind3) and implement this extension function in another, conventional PL.
@Peymankh: As for your saying that you want a multi-dimensional array "in XSLT" vs "in XML", you must know that: 1. Any XSLT stylesheet is a well-formed XML document; 2. One can use any XML trees as part of an XSLT stylesheet; 3. One can access additional XML documents in an XSLT transformation, using the standard XSLT function 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.
Thanks for the edit, but the thing is I need to implement this based on the CYK algorithm since it's a course work!
@Peymankh: If you must implement this algorithm, then you have to use the data structures available only in XPath and use them to emulate multi-dimensional matrices. Ask them if extension functions are allowed to represent the multi-dimensional matrices. If extension functions aren't allowed, then you have to keep your data in XML trees like the one provided in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.