Skip to main content

Binding data model simple values to zk components

In the previous section, we saw how to declare a sample data model. To bind a component to a data model element, the bind attribute can be used. The bind attribute value must be composed of the data source path to be used, a colon separator, and the xPath element to the data model element.

The data source is selected based on the zk path. Both simple paths (/model1) or full paths (//page1/window1/window2/model2).

The data model element is specified using its XPath. Internally, it's using an Apache commons JXPath version.

So, in order to display a label with the value of the "name" attribute of the XML document "title" entity, set the bind attribute of a new label the value "/mydata:/title/@name", as shown in the following example:

<?xml version = "1.0" encoding = "UTF-8">
<zk>
  <xmlDataSource id = "mydata" src = "/my-data.xml" />
  <label bind = "/mydata:/title/@name" />
</zk>

Mind that this binding is only one way, because the label object does not allow the user to change its content. When using a textbox, checkbox or any other input element, the binding will be bidirectional, allowing the user to change the associated DOM XML file. Anyway, the changes will not be serialized to the original XML file.