XPath in MobileTogether
The XPath language plays a crucial part in the design of MobileTogether solutions. XPath is used to locate, access, manipulate, generate, and save data in the various data trees used in the design and to define the functioning of different design components. Some important ways in which XPath is used in a MobileTogether design are given below. This overview is intended to give you a broad sense of the flexibility and power of XPath and of how XPath is used in MobileTogether designs.
For more information about XPath, see the XPath 3.1 Recommendation of the W3C, which is the latest version of the language available and is the version supported in MobileTogether Designer. To get you started using a more learning-based approach, see the following:
•Altova's A Gentle Introduction to XPath
•Altova's XPath 3.0 Training
The locator expressions of the XPath language are used to locate nodes in XML trees. A locator expression typically consists of a path that locates the required node. Here are some examples:
•/Company/Office: Locates all Office child elements of the Company element, which is the top-level document node. We know that the Company element is the top-level element because it occurs directly under the root node, which is indicated by the first forward slash.
•/Company/Office: Locates the third Office child element of the Company element.
•/Company/Office/@location: Locates the location attribute of the third Office child element of the Company element.
•//Office[@location='US']: Locates all Office elements that have a location attribute having a value of US.
The list above shows just a few basic locator expressions. There are many more ways in which locator expressions can be constructed.
Operators allow you to apply filters, build conditions, and manipulate selections and data. For example, here are just two operators:
•if (Selection='US') then //Office[@location='US'] else //Office[@location!='US']: This if operator selects US or non-US offices depending on the content of the Selection child element.
•for \$i in //Office return \$i[@location='US']: This for operator returns all Office elements that have a location attribute having a value of US.
XPath functions enable the manipulation, calculation, and generation of data. For example, a function can take a string as input (the function's argument), and convert into lowercase or even remove a part of the string. The XPath functions that can be used in MobileTogether designs are of the following types:
The XPath language contains a large library of built-in functions which enable you to extract data as well as metadata related to the XML tree, and even to generate data. For example:
•count(Office): Returns the number of Office child elements.
•day-from-date("2015-04-26"): Returns the number 26, which is the day part of the function's date argument.
User guides and references for the built-in functions are widely available on the Internet. A full list of the functions can be found in the XPath 3.1 Recommendation of the W3C.
This is a set of of XPath extension functions that Altova is developing to provide developers with more functionality in XPath. There are currently some 60 extension functions, ranging from functions that provide geolocation information to those that convert integers to hexadecimal strings and vice-versa. For example:
•format-geolocation(33.33, -22.22, 2) returns the xs:string "33.33N 22.22W"
•hex-string-to-integer('1') returns 1
Altova extension functions are available for use in all MobileTogether designs. For usage information, see the Altova Extension Functions section in the MobileTogether Designer user manual.
These are XPath extension functions developed by Altova for specific uses in MobileTogether designs. For example:
•mt-has-server-access(10) returns true if server access is possible within the time in seconds that is specified as the argument of the function, false otherwise.
•mt-load-string('MyCourier') returns the localized MyCourier string that is stored in the solution's string pool. The language of the localization is selected automatically according to the language of the mobile device.
MobileTogether extension functions are available for use in all MobileTogether designs. For usage information, see the MobileTogether Extension Functions section in the MobileTogether Designer user manual.
These are XPath extension functions that you, the user, can define in a design for some special purpose you have in mind and for which no suitable function exists in the function libraries listed above. For example, you might want to define a function to convert temperatures between the Celsius and Fahrenheit scales. User-defined functions are defined within a single MobileTogether project and are used in that specific project. How to define such custom functions is described in the User-Defined XPath/XQuery Functions section of the MobileTogether Designer user manual.
Global variables contain information about the client mobile device. For example, there is one variable to indicate the device's type, another to indicate its dimensions, and yet another to indicate the device's current orientation (landscape or portrait), and so on. The values of all these variables are obtained at run-time from the client device as part of standard mobile communication procedures. Variables can then be used in XPath/XQuery expressions. As a result, processing can be specified that is conditional upon a device's inherent static properties (such as size) or its changeable dynamic properties (such as orientation).
MobileTogether's global variables are predefined and are listed in the section Global Variables together with each variable's description and possible values. The example below of the MT_iPad global variable (possible values: true(), false()) shows how global variables are called in XPath expressions. The \$ symbol is used to indicate that what follows is the name of a global variable, which is the usual way to indicate variables in XPath.
if ( \$MT_iPad=true() ) then "Apple" else ""