CallXML 3.0 Development GuideHome  |  Frameset Home

  F: XPath  |  TOC  |  XPath Expressions: Part 2  

XPath Expressions

Within the XPath context, the term 'axis' defines a node, or node-set that is relative in some way to the current node. Think of an XML document in a visual context, in that an XML document is a 'tree' structure: the parent nodes that encompass the child nodes can be seen as branches, (or even the trunk of the tree), whereas a child node can be considered a 'branch', or a 'leaf' of the parent nodes. Using a particular axis command allows you with an easy method to navigate from one node to the next using only relative, (and thus, non-specific) commands.  Be aware that there are many, many different ways to select a particular node or node-set when using Xpath. In some circumstances, using a 'longhand' axis command makes the most sense to a particular developer, while other folks prefer to use the more abbreviated 'shorthand' commands. To further confuse things, using a simple Node Address command will sometimes be the best fit...there are several 'right' ways to construct your XPath axis commands; it all depends on the developers preferences and sensibilities. A full listing of the supported axis commands and syntaxes is defined below, and illustrated with a full-blown example that you can base your applications on. Note that the voxeo implementation of Xpath does not support all 1.0 features, commands, and functions. As such use these guides as your primary resource for all things Xpath when working with the CallXML3.0 markup.


XPath Axes



To illustrate the XPath Axes listed above, we present a specific example that shows how you can use all these commands in a simple CallXML3.0 application. Do note that if you decide to copy/paste this code, and map it to an application, you won't hear anything at all. All of the XPath data is output in <log> statements that you will only be able to see when using the Voxeo Application Debugger.


BasicExpressionTest.xml


<?xml version="1.0" encoding="UTF-8"?>

<callxml version="3.0">



<!-- fetch our XML document, and assign it to a locally scoped CallXML variable -->
<fetch value="CD_Listing.xml" var="myFetch" type="xml"/>

  <block label="Axes" value="$myFetch;">

  <!-- use the 'child' command to select the value of any child elements of the first 'disc' element

-->
    <assign var="child" expr="list(//disc[1]/child::*/text())"/>
    <log>*** CHILD: $child; ***</log>
    <wait value="1s"/>

  <!-- use the 'descendant' command to select the child elements of the first 'disc' element  -->
    <assign var="descendant" expr="list(//disc[1]/descendant::*/text())"/>
    <log>*** DESCENDANT: $descendant; ***</log> 
    <wait value="1s"/>

  <!-- use the 'parent' command to list the parent elements of the 'RSrating' element -->
    <assign var="parent" expr="list(//RSrating/parent::*/text())"/>
    <log>***  PARENT: $parent; ***</log> 
    <wait value="1s"/>

  <!-- use the 'ancestor' command to list the parents, grandparents, etc. of the 'RSrating' element-->
    <assign var="ancestor" expr="list(//RSrating/ancestor::*/text())"/>
    <log>***  ANCESTOR: $ancestor; ***</log> 
    <wait value="1s"/>

  <!-- use the 'following-sibling' command to list the sibling elements -->
  <!-- that occur after the first 'RSrating' element -->
    <assign var="followSib" expr="list(//RSrating[1]/following-sibling::*/text())"/>
    <log>***  FOLLOWING-SIBLING: $followSib; ***</log> 
    <wait value="1s"/>

  <!-- use the 'preceding-sibling' command to list the siblings -->
  <!-- that occur before the 'Spinrating' element-->
    <assign var="precedingSib" expr="list(//Spinrating/preceding-sibling::*/text())"/>
    <log>***  PRECEDING-SIBLING: $precedingSib; ***</log> 
    <wait value="1s"/>

  <!-- use the 'following' command to list the elements that come after the 'disc' element -->
    <assign var="follow" expr="list(//disc/following::*/text())"/>
    <log>***  FOLLOWING: $follow; ***</log> 
    <wait value="1s"/>

  <!-- use the 'precede' command to list the elements that occur before the third 'disc' element -->
    <assign var="precede" expr="list(//disc[3]/following::*/text())"/>
    <log>***  PRECEDING: $precede; ***</log> 
    <wait value="1s"/>


  <!-- use the 'self' command to list the current node -->
    <assign var="self" expr="list(//disc[4]/self::*/text())"/>
    <log>***  SELF: $self; ***</log> 
    <wait value="1s"/>


  <!-- use the 'ancestor-or-self' command to list all parent, grandparent, etc -->
  <!-- elements, as well as the currently selected node -->
    <assign var="ancSelf" expr="list(//RSrating/ancestor-or-self::*/text())"/>
    <log>***  ANCESTOR-OR-SELF: $ancSelf; ***</log> 
    <wait value="1s"/>
 
  </block>

</callxml>



CD_Listing.xml


<?xml version="1.0" encoding="UTF-8"?>

<mynodes>

  <axes>

  <disc artist="Neutral Milk Hotel">
    In the Aeroplane over the Sea
    <RSrating>9.6</RSrating>
    <Spinrating>8.9</Spinrating>
  </disc>

  <disc artist="Justin Timberlake">
    Justified
    <RSrating>2.1</RSrating>
  <!-- and that is pretty generous... -->
  </disc>

  <disc artist="Maria Rita">
    Vero
    <RSrating>8.3</RSrating>
    <Spinrating>9.1</Spinrating>
  </disc>

  <disc artist="William Shatner">Has Been
    <RSrating>4.8</RSrating>
  </disc>

  </axes>

</mynodes>



Download It!

If you'd rather just download the Xpath example files rather than typing them out yourself, then you must be government-employed. In any case, we aim to please, so you can grab the source files for the above code below, and upload it to your hosting server, (or ours), to give it a test run on the CallXML3.0 platform.

  CallXML 3.0 source code




  ANNOTATIONS: EXISTING POSTS
0 posts - click the button below to add a note to this page

login
  F: XPath  |  TOC  |  XPath Expressions: Part 2  

© 2008 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site