Skip to main content link. Accesskey S
  • Translate Page ▼
  • Anonymous
  • Log on
  • Help
  • IBM logo
  • IBM Forms wiki
  • All Wikis
  • Home
  • Community Articles
  • Product Documentation
  • Learning Center


Search

Advanced Search

Categories

Tag Cloud

  • 3.0
  • 3.5
  • 3.5.1
  • 4.0
  • 4.0.0.1
  • 4.0.0.2
  • 8.0
  • accessibility
  • action item
  • API
  • app dev
  • beta
  • button item
  • C
  • choices
  • clustering
  • color
  • COM
  • compute
  • computes
  • configuring
  • date functions
  • dates
  • defining
  • demonstration
  • demos
  • deployment
  • deployment scenario
  • Designer
  • designer_media
  • designing
  • dev guide
  • developing
  • differences between Webform Server and Viewer
  • display elements
  • education
  • Enablement
  • enterprise
  • error messages
  • events
  • field
  • field item
  • FileNet
  • filtering
  • form
  • forms
  • FSP
  • functions
  • getting_started
  • Help
  • IBM Forms
  • images
  • installing
  • integrating
  • integrating, 4.0.0.1
  • items
  • iWidget
  • JAVA
  • language
  • layout
  • lf_intro
  • math functions
  • methods
  • new_user
  • options
  • P8
  • performance
  • pipelines
  • pipes
  • printing
  • programming
  • references
  • resources
  • samples
  • signature item
  • signatures
  • slider item
  • spec
  • string functions
  • strings
  • StrJava
  • submissions
  • table
  • text
  • training
  • Turbo
  • turbo_media
  • tutorials
  • utility functions
  • video
  • VideoFest
  • Viewer
  • viewer package
  • Webform Server
  • WebformServer
  • XFDL
  • XForms
  • XForms functions
  • XForms model
  • XPath
InformationInformation
You are currently viewing machine translated content. IBM translation might be available. Click IBM Translated Product Documentation to see what is available.X


Home > XForms Samples > The Expressive Power of XForms Binds and XPath
Rate this article 1 starRate this article 2 starsRate this article 3 starsRate this article 4 starsRate this article 5 stars

The Expressive Power of XForms Binds and XPath 

expanded Abstract
collapsed Abstract
No abstract provided.

The goal of this sample is to illustrate some of the expressive power of XForms binds and how an XPath nodeset in a bind is used at runtime to evaluate the result of a bind.

In this example the goal is to count the number of occurrences of specific text entries in a homogeneous set of XML elements. By homogeneous I just mean that each element has the same tag name and they are all siblings of each other. Like this:


<xforms:instance id="formData" xmlns="">
   <data>
      <fruits>
         <item>apple</item>
         <item>orange</item>
         <item>banana</item>
         <item>apple</item>
         <item>apple</item>
         <item>banana</item>
     </fruits>
   </data>
</xforms:instance>


Our goal is to count up each fruit type and store the result in the following nodes (I will refer to them as the "counting buckets"):


<xforms:instance id="variables" xmlns="">
   <data>
      <counts>
         <apples value="apple"></apples>
         <oranges value="orange"></oranges>
         <bananas value="banana"></bananas>
      </counts>
   </data>
</xforms:instance>


So how do we do this? Well, to get started, we need to know about the XPath "count" function, which will tell us how many elements there are in a given nodeset. We can use an XForms bind for each of one of the counting buckets, which counts up that specific type of fruit, like this:


<xforms:bind calculate="count(instance('formData')/fruits/item[. = 'apple'])" nodeset="instance('variables')/counts/apples"/>
<xforms:bind calculate="count(instance('formData')/fruits/item[. = 'orange'])" nodeset="instance('variables')/counts/oranges"/>
<xforms:bind calculate="count(instance('formData')/fruits/item[. = 'banana'])" nodeset="instance('variables')/counts/bananas"/>


The "calculate" model item property (MIP) will evaluate the XPath we give it and store the output in the nodeset. Each of these binds counts up all the elements whose item is one of the fruit types. Ok, so that works, but wait, we could make use of the attribute on the "counting bucket" elements to indicate what they are counting by just referring to it in the bind nodeset, this will let us avoid hard coding the item that is being counted in the bind logic and keep that information in the data. To do this, we need to know about the "current()" function, an XForms function that will give us back the current nodeset context. When we use "current()" in a bind model item property (like calculate) we get back the node in the bind's nodeset that is currently being evaluated.

So if we change the binds like so:


<xforms:bind calculate="count(instance('formData')/fruits/item[. = current()/@value])" nodeset="instance('variables')/counts/apples"/>
<xforms:bind calculate="count(instance('formData')/fruits/item[. = current()/@value])" nodeset="instance('variables')/counts/oranges"/>
<xforms:bind calculate="count(instance('formData')/fruits/item[. = current()/@value])" nodeset="instance('variables')/counts/bananas"/>


We get the same functionality, but we've leveraged the value attribute of the counting buckets. Note that the "." in the XPath gives you the element referenced just to the left of the predicate, while the current() function gives you the element in the nodeset.


Now hold on to your hats, we're going to take it a step further! Once we make the adjustment above, note that the contents of the calculate is identical in all 3 binds. What this means is that we can combine the three binds together as long as we can make a nodeset that points to all the counting buckets. As it happens, we can make that nodeset, in one of two ways:

The "|" operator in XPath computes the union of its operands, which must be nodesets. We can use it like so:
instance('variables')/counts/apples | instance('variables')/counts/oranges | instance('variables')/counts/bananas

or, since these counting bucket elements all happen to be siblings of each other and have no other extraneous siblings we want to avoid selecting, we can use the "*" which is a wildcard, like so:
instance('variables')/counts/*

Which brings us to this one bind that does all we need:

<xforms:bind calculate="count(instance('formData')/fruits/item[. = current()/@value])" nodeset="instance('variables')/counts/*"/>


This bind provides an additional degree of flexibility, because if you think about it, if you need to add another item to be counted up (say, pears) you can do this without touching the bind, all you need to do is add a new "counting bucket".

Why it works: when you create a bind with a nodeset that selects more than one node (as our wildcard "*" XPath does), the bind will get evaluated for EACH of the elements that are in that nodeset. And for each element, relative references which might be in the bind's model item properties will be evaluated relative to that element (in this case our use of "current()" in the calculate MIP).

Attached you'll find a sample form which demonstrates the final bind.

expanded Article information
collapsed Article information
Category:
XForms Samples, Samples,
Tags:
binds, nodeset, XForms

This Version: Version 4 December 2, 2010 6:16:20 PM by Jack Mitchell  IBMer
   
expanded Attachments (1)
collapsed Attachments (1)

 


File TypeSizeFile NameCreated On
application/octet-stream 4 KB Counting Items Example.xfdl 3/3/10 4:54 PM
expanded Versions (4)
collapsed Versions (4)
expanded Version Comparison
collapsed Version Comparison
     
Version Date Changed by               Summary of changes
This version (4) Dec 2, 2010 6:16:20 PM Jack Mitchell  
3 Mar 4, 2010 7:51:28 PM Ryan Nordman  
2 Mar 4, 2010 12:54:31 PM Ryan Nordman  
1 Mar 3, 2010 5:05:51 PM Ryan Nordman  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Tip: When linking to articles use the original title, not the edited title. The alias for the link can be the edited title.
Go ElsewhereStay ConnectedSubscribe to RSSHelpAbout
  • All Lotus and WebSphere Portal wikis
  • IBM developerWorks
  • IBM Software support
  • Lotus Technical Information and Education Team Blog
  • Lotus Tech Info on Twitter
  • Lotus Tech Info on Facebook
  • Lotus product forums
  • Lotus Tech Info blog
  • IBM Collaboration Solutions
  • Recently added feedRecently added
  • Recently edited feedRecently edited
  • Recently added comments feedRecently Added Comments
  • Wiki Help
  • Forgot user name/password
  • Wiki design feedback
  • Content feedback
  • About the wiki
  • About IBM
  • Privacy
  • Contact IBM
  • IBM Terms of use
  • Wiki terms of use
Return to English
Arabic
Chinese Simplified
Chinese Traditional
French
German
Italian
Japanese
Korean
Portuguese
Russian
Spanish