The datapath Attribute

Now that you understand XPath basics, you are ready to apply your knowledge in OpenLaszlo. Various tags posses the datapath attribute that takes an XPath expression.

Let’s see the datapath attribute in action. Consider the LZX application in Listing 8.2.

Listing 8.2. Binding XML data
<canvas height="200" width="500">
    <dataset name="myData">
        <library>
            <book available="true">
                <isbn>1234567890</isbn>
                <title>In the Middle of the Night</title>
                <author>Jane Volvic</author>
                <pageCount>256</pageCount>
                <price>19.95</price>
            </book>
            <book available="false">
                <isbn>1234567893</isbn>
                <title>The Man who Never Grows Old</title>
                <author>Anthony Hophop</author>
                <pageCount>400</pageCount>
                <price>29.95</price>
            </book>
            <book available="true">
                <isbn>1234567894</isbn>
                <title>No Excuses</title>
                <author>Sherma Shaun</author>
                <pageCount>160</pageCount>
                <price>9.95</price>
            </book>
        </library>
    </dataset>
    <view layout="axis:y">
        <text width="200"
       datapath="myData:/library/book[1]/title/text()"/>
        <text width="200"
       datapath="myData:/library/book[2]/title/text()"/>
        <text width="200"
       datapath="myData:/library/book[3]/title/text()"/>
    </view>
</canvas>

You use the dataset tag to embed XML data. To bind data, use the datapath attribute, passing an XPath expression. The format for the datapath attribute value is as follows:

dataset:xpathExpression

Here, dataset is the name of the dataset tag that contains the data. You also need to call the text function to print the returned value.

You can run the LZX application in Listing 8.2 by invoking this URL:

http://localhost:8080/lps-4.0.x/app08/datasetTest1.lzx

The result is shown in Figure 8.1.

Figure 8.1. Data binding in OpenLaszlo


Before we go further exploring more of OpenLaszlo’s data binding features, let’s first discuss a relevant issue that you will need when working with data: the separation between data (XML documents) and business logic (LZX application).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset