W3C

XPath Data Model for SCXML

W3C Working Group Note 11 August 2015

This version:
http://www.w3.org/TR/2015/NOTE-scxml-xpath-dm-20150811/
Latest version:
http://www.w3.org/TR/scxml-xpath-dm/
Previous version:
http://www.w3.org/TR/2015/NOTE-xpath-dm-20150806/
Editors:
Jim Barnett, Genesys (Editor-in-Chief)
Rahul Akolkar, IBM
RJ Auburn, Voxeo
Michael Bodell, (until 2012, when at Microsoft)
Daniel C. Burnett, Voxeo
Jerry Carter, (until 2008, when at Nuance)
Scott McGlashan, (until 2011, when at HP)
Torbjörn Lager, Invited Expert
Mark Helbing, (until 2006, when at Nuance)
Rafah Hosn, (until 2008, when at IBM)
T.V. Raman, (until 2005, when at IBM)
Klaus Reifenrath, (until 2006, when at Nuance)
No'am Rosenthal, (until 2009, when at Nokia)
Johan Roxendal, Invited Expert

Abstract

This document describes the XPath Data Model for SCXML [SCXML]. This data model allows SCXML state charts to use XML as their data representation, and to manipulate it with XPath. For more details on data models, see the SCXML specification.

Status of this Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This Note contains the description of the XPath Data Model for SCXML. Prior to this time, the XPath Data Model was part of the [SCXML] specification, but it was removed because we did not receive enough implementation reports for it to be included in the final W3C Recommendation. However there was enough interest in it, including an implementation, that the Working Group decided to publish it as a separate note for archival purposes.

This Note was once published on 6 August 2015 as a First Public Working Group Note. However, after the publication we got comments that the Note's having a shortname of "xpath-dm" was misleading since the Note describes not the whole XPath data model but the XPath data model for SCXML. Therefore this Note is now republished with a clearer shortname of "scxml-xpath-dm" to avoid confusion.

The W3C Membership and other interested parties are invited to review the document and send comments to the Working Group's public mailing list [email protected](archive). See W3C Mailing List and Archive Usage Guidelines.

This document has been produced as part of the Voice Browser Activity (activity statement), following the procedures set out for the W3C Process. The authors of this document are members of the Voice Browser Working Group.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Publication as a Working Group Note does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document is governed by the 14 October 2005 W3C Process Document.

Table of Contents

1 Terminology
2 The XPath Data Model
    2.1 Data Model
    2.2 Conditional Expressions
    2.3 Location Expressions
    2.4 Value Expressions
    2.5 <content>
    2.6 <assign>
    2.7 System Variables
    2.8 Serialization
    2.9 Scripting
    2.10 <foreach>
    2.11 Unsupported Elements

Appendix

A References
    A.1 References


1 Terminology

The key words MUST, MUST NOT, REQUIRED, SHALL, SHALL NOT, SHOULD, SHOULD NOT, RECOMMENDED, MAY, and OPTIONAL in this specification are to be interpreted as described in [RFC 2119].

The terms base URI and relative URI are used in this specification as they are defined in [RFC 2396].

2 The XPath Data Model

The value "xpath" for the 'datamodel' attribute results in an XML data model with XPath used as the expression language. Implementations that support this data model MUST support [XPath 1.0].

2.1 Data Model

The underlying structure of the XPath data model follows the document syntax closely. In particular, the SCXML Processor MUST maintain the XPath data model as an XML document with a root element called <datamodel>. For each <data> element in the document, the SCXML Processor MUST create a child of <datamodel> called <data> with an 'id' attribute whose value is the same as that of the 'id' attribute of the document <data> element. The Processor MUST insert the value of the document <data> element as the child(ren) of the datamodel <data> element. The Processor MUST also bind an XPath variable of the same name to that datamodel <data> element.

If either the 'src' attribute or in-line content is provided in the document <data> element, then if the content (whether fetched or provided in-line) is an XML document or node set, the SCXML Processor MUST insert the XML as the child of the datamodel <data> element. Otherwise the Processor MUST treat the content as a space-normalized string literal and insert it as the child of the datamodel <data> element. If no value is assigned, the SCXML Processor MUST leave the datamodel <data> element empty, as follows:

<data xmlns=""></data>

2.1.1 Scoping

The Processor MUST place all variables in a single global XPath scope. Specifically, the SCXML Processor MUST allow any data element to be accessed from any state. Ordering dependencies between <data> elements are not permitted. In the case of early binding, the SCXML Processor MUST evaluate all <data> elements at initialization time but MAY do so in any order it chooses. When late binding is selected, the SCXML Processor MUST create data model elements at initialization time but MAY do so in any order it chooses. Similarly, the processor MUST assign the specified initial values to data elements only when the state containing them is first entered, but MAY do so in any order it chooses.

Example: Syntax for XPath data model initialization
<scxml version="1.0" datamodel="xpath">
<datamodel>
  <data id="company">
    <about xmlns="">
      <name>Example company</name>
      <website>example.com</website>
      <CEO>John Doe</CEO>
    </about>
  </data>
  <data id="employees" src="http://example.com/employees.xml"/>
  <data id="defaultdata"/>
</datamodel>
</scxml>

2.2 Conditional Expressions

The SCXML Processor MUST accept any XPath 1.0 expression as a conditional expression and MUST convert it into its effective boolean value as described in section 2.4.3 of the [XPath 2.0] specification. (Note that the definition of effective boolean value is taken from the XPath 2.0 specification, even though the XPath datamodel requires support only for expressions as defined in XPath 1.0, and not for the broader range of expressions defined in XPath 2.0.) The following example illustrates this usage.

Example: Use of a boolean expression to determine whether or not a transition is taken.
<state id="errorSwitch" xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <datamodel>
    <data id="time"/>
  </datamodel>
          
  <onentry>
    <assign location="$time" expr="fn:current-dateTime()"/>
  </onentry>
          
  <transition cond="fn:year-from-dateTime($time) > 2009" target="newBehavior"/>
  <transition target="currentBehavior"/>
</state>
       

The SCXML processor MUST define an XPath function 'In()' that takes a stateID as its argument and returns 'true' if and only if that state is in the current state configuration, as described in Conditional Expressions. For examples of the use of this predicate (but in an ECMAScript context), see microwave example.

Function signature: In($stateID as xs:string?) as xs:boolean

Returns an xs:boolean indicating whether or not the state with ID $stateID is one of the currently active states.

2.3 Location Expressions

The SCXML Processor MUST accept any XPath expression as a location expression. The following example illustrates this usage:

Example: Use of the location attribute of the assign to update the count for both Boston and New York.
<state id="errorSwitch">
  <datamodel>
    <data id="cities">
      <list xmlns="">
        <city id="nyc" count="0">New York</city>
        <city id="bos" count="0">Boston</city>
      </list>
    </data>
  </datamodel>

  <onentry>
    <assign location="$cities/list/city[@id='nyc']/@count" expr="1"/>
  </onentry>
</state>

2.4 Value Expressions

The SCXML Processor MUST allow any XPath expression to be used as a value expression. If the result of the value expression is a node-set, the Processor MUST make a deep copy of the subtree rooted at each node. The Processor MAY convert the expression result to a string In contexts where a string is expected.

Example: Copying event data into the local data model for the state.
<state id="processEvent">
  <datamodel>
    <data id="myEventData"/>
  </datamodel>
    
  <onentry>
    <assign location="$myEventData" expr="$_event/data"/>
  </onentry>
</state>

2.5 <content>

When <content> is a child of <donedata>, the Processor MUST interpret its value as defined in 2.7.1 _event.data. When <content> is a child of <send>, the interpretation of its value depends on the Event I/O Processor. When <content> is a child of <invoke>, the interpretation of its value is platform-specific.

2.6 <assign>

Implementations supporting the XPath data model MUST support the following additional attributes for the <assign> element.

Name Required Attribute Constraints Type Default Value Valid Values Description
type false enum replacechildren replacechildren, firstchild, lastchild, previoussibling, nextsibling, replace, delete, addattribute Defines the nature of the insertion to be performed. See below for details.
attr false This attribute must be present if and only if 'type' is 'addattribute'. NMTOKEN none The attribute name to add at the specified location.

The SCXML Processor MUST modify the data model as directed by the 'type' attribute as described below. If it is unable to do so, it MUST place the error error.execution on the internal event queue:

  • replacechildren. Replace all the children at 'location' with the value specified by 'expr'.
  • firstchild. Insert the value specified by 'expr' before all of the children at 'location'.
  • lastchild. Insert the value specified by 'expr' after all of the children at 'location'.
  • previoussibling. Insert the value specified by 'expr' before the node specified by 'location', keeping the same parent.
  • nextsibling. Insert the value specified by 'expr' after the node specified by 'location', keeping the same parent.
  • replace. Replace the node specified by 'location' by the value specified by 'expr'.
  • delete. Delete the node specified by 'location'. ('expr' is ignored.)
  • addattribute. Add an attribute with the name specified by 'attr' and value specified by 'expr' to the node specified by 'location'.

If the specified location contains a node set, the Processor MUST perform the assignment to each node in the set. It MAY do so in any order it chooses, and MUST evaluate 'expr' only once.

Note that in the case of an XML data model, it is not required to assign to the root of a tree (i.e., the "name" value in a <data> tag), since the path expression can reach down into the tree to assign a new value to an internal node. The following examples show various aspects of assignment in the XPath data model. Suppose we have a data model of the following form:

<data id="cart">
  <myCart xmlns="">
    <books>
      <book>
        <title>The Zen Mind</title>
      </book>
      <book>
        <title>Freakonomics</title>
      </book>
    </books>
    <cds>
      <cd name="Something"/>
    </cds>
  </myCart>
</data>

Here is an example of assignment of a string to an element node.

 
<assign location="$cart/myCart/books/book[1]/title"  expr="'My favorite book'"/>

results in

<data id="cart">
  <myCart xmlns="">
    <books>
      <book>
        <title>My favorite book</title>
      </book>
      <book>
        <title>Freakonomics</title>
      </book>
      ...
</data>

Now suppose we assign an XML structure to an element node. The following assignment statement would have the effect of replacing the children of the element "$cart/myCart/books/book[1]" by the XML tree rooted in <bookinfo>.

<assign location="$cart/myCart/books/book[1]">
  <bookinfo xmlns="">
    <isbn>12334455</isbn>
    <author>some author</author>
  </bookinfo>
</assign>

results in

<data id="cart">
  <myCart xmlns="">
    <books>
      <book>
        <bookinfo>
          <isbn>12334455</isbn>
          <author>some author</author>
        </bookinfo>
      </book>
      <book>
        <title>Freakonomics</title>
      </book>
      ...
</data>

Here are examples of legal and illegal assignment to an attribute:

<!-- Legal assignment: -->
<assign location="$cart/myCart/cds/cd/@name" expr"'Something Else'"/>

<!-- Illegal assignment: -->
<assign  location="$cart/myCart/cds/cd/@name" >
  <foo>
    <bar/>
  </foo>
</assign>

Now suppose we assign a string to a nodeset. The following assignment statement would have the effect of replacing the children of each node in the nodeset $cart/myCart/books/book with the string "The Zen Mind":

<assign location="$cart/myCart/books/book" expr="'The Zen Mind'"/>

results in

<data id="cart">
  <myCart xmlns="">
    <books>
      <book>The Zen Mind</book>
      <book>The Zen Mind</book>
    </books>
    ...
</data>

Finally suppose we assign a structure to a nodeset. The following statement would iterate over the elements in the nodeset of <book> elements and replace their children with the <price> structure:

<assign location="$cart/myCart/books/book">
  <price>20.0</price>
</assign>

results in

<data id="cart">
  <myCart xmlns="">
    <books>
      <book>
        <price>20.0</price>
      </book>
      <book>
        <price>20.0</price>
      </book>
    </books>
    ...
</data>

If the evaluation of any of the expressions in an <assign> element causes an error to be raised, evaluation of the element terminates immediately and the <assign> has no effect. For example, the following assignment statement would raise an error because the sample data model we are using does not have an <ISBN> node as a child of <book>:

 
<assign location="$cart/myCart/books/book[1]/ISBN"  expr="'....'"/>

2.7 System Variables

For each system variable defined in System Variables, the SCXML Processor MUST create a data model <data> element with an 'id' attribute whose value is the name of the system variable. The Processor also MUST bind an XPath variable whose name is the name of the system variable to this element. For the _event system variable (i.e., the <data id="_event"> child of <datamodel>), for each of the fields defined in The Internal Structure of Events, the Processor also MUST create an child element whose name is the name of the field. For the _ioprocessors system variable the Processor MUST create an child <processor> element for each Event I/O processor that it supports, with a 'name' attribute whose value is the name of the I/O processor. For the SCXML and BasicHTTP Event I/O processors, the Processor MUST create a <location> child under the child element, assigning the access URI as its text child. For example, in systems that support the BasicHTTP Event I/O Processor, the access URI of the BasicHTTP Event I/O processor can be a accessed as $_ioprocessors/processor[@name="/http://www.w3.org/TR/scxml/#BasicHTTPEventProcessor/"]/location/text().

2.7.1 _event.data

The _event variable's <data> child is populated with content provided by an external event or by <param> or <content>. In some circumstances, the entity that generated the event may have indicated how the data is to be interpreted. In such cases, the Processor SHOULD try to format the data according to those indications. Otherwise, if the SCXML Processor can interpret the content as key-value pairs, then for each unique key, the Processor MUST create a <data> element as a child of _event's <data> child with an 'id' attribute whose value is the name of the key. The Processor MUST insert the value of the key-value pair as the content of this element. In the case of duplicate keys, the behavior is platform-specific. (Note that content specified by <param> or delivered as POST parameters via the BasicHTTP Event I/O Processor consists unambiguously of key-value pairs.) Otherwise (i.e., if the content does not consist of key-value pairs), if the Processor can interpret the content is a valid XML document, the it MUST insert the XML structure as the content of _event's <data> child. Otherwise, the Processor MUST treat the content as a space-normalized string literal and insert it as the content of _event's <data> child.

Suppose as part of executing a state machine named "myName" with a platform-assigned sessionid "12345", we are processing an event with the name "foo.bar" and the following XML payload:

<payload xmlns="">
    <answer>42</answer>
</payload>

Then the underlying XML data model would have the following form (not all the fields of the event are shown):

Example: Illustration of system injected data
  <datamodel>

    <!-- The four data elements below are automatically populated by the system -->

    <data id="_name">myName</data>
    <data id="_sessionid">12345</data>
    <data id="_event">
      <name xmlns="">foo.bar</name>
      <data xmlns="">
        <payload>
          <answer>42</answer>
        </payload>
      </data>
    </data>
    <data id="_ioprocessors">
      <processor name="http://www.w3.org/TR/scxml/#BasicHTTPEventProcessor">
        <location>http://example.com/scxml-http/12345</location>
      </processor>
      <processor name="http://www.w3.org/TR/scxml/#SCXMLEventProcessor">
        <location/>
      </processor>
    </data>

    <!-- Rest of the application / developer-authored
         data model goes here -->

  </datamodel>

As an example, here is a sample transition that accesses the $_sessionid variable in that data model.

<state id="checkSessionid">
  <transition cond="$_sessionid = '12345'" target="nextState"/>
  ...
</state>

2.8 Serialization

In certain circumstances, e.g. when including data in events sent via the BasicHTTP Event I/O Processor, the SCXML Processor is required to serialize data from the XPath data model for transmission to a remote entity. In such cases, the Processor MUST serialize the data as a string.

2.9 Scripting

There is no scripting language.

2.10 <foreach>

In the XPath data model, the legal iterable collections are Node-Sets. The legal values for the 'item' attribute are legal XPath variable names. The iteration order is the order of the underlying Node-set, and goes from an index of 1 by increments of one to an index of count(node-set).

Example: Logging ISBN of all books in bookstore shopping cart
<foreach array="$cart//book" item="book">
<log expr="fn:concat('Cart contains book with ISBN ', + $book/isbn)"/>
</foreach>

2.11 Unsupported Elements

The script element is not supported in the XPath Data Model.

A References

A.1 References

RFC 2119
RFC 2119: Key words for use in RFCs to Indicate Requirement Levels Internet Engineering Task Force. March 1997. (See http://www.ietf.org/rfc/rfc2119.txt.)
RFC 2396
RFC 2396: Uniform Resource Identifiers Internet Engineering Task Force. August 1998. (See http://www.ietf.org/rfc/rfc2396.txt.)
SCXML
State Chart XML (SCXML): State Machine Notation for Control Abstraction. W3C Candidate Recommendation 2015. (See http://www.w3.org/TR/scxml/.)
XPath 1.0
XML Path Language (XPath) 1.0 World Wide Web Consortium. W3C Recommendation, November, 1999. (See http://www.w3.org/TR/xpath/.)
XPath 2.0
XML Path Language (XPath) 2.0 World Wide Web Consortium. W3C Recommendation, December, 2010. (See http://www.w3.org/TR/xpath20/.)