@context id

From W3C XForms Group Wiki (Public)


Proposal

Add optional ID argument to the XForms 1.1 function context().

Status

Incorporated into XForms 2.0, in review

Motivation

context() accesses only the parent context, not any other ancestor (or other element) context.

For repeat/setvalue, context() accesses the repeat context.

For repeat/repeat/setvalue, or worse yet repeat/repeat/repeat/setvalue, it is impossible to access the grand-parent contexts using context, and authors revert to using index() and position() in predicates. So, the authoring curve becomes steep.

References

See the discussion [1]

Questions

Should we limit the context() ID argument to lexically enclosing elements?

An IDREF would have to be interpreted using the same kind of logic used elsewhere to convert IDREFs in the authored DOM into element references in the run-time "multi-DOM". Selecting the right repeat-item occurrences is based on where the invocation that makes the IDREF is located relative to the element being referenced. That being said, limiting to IDREFs seems too constraining, based on the example use case below. Notice that it refers to a repeat when really it wants to refer to the (unexpressed) child group element of that repeat. We could say someone wanting to do this should express a child group element for the repeat and assign it an ID, but it is a fairly technical thing to ask of authors.

Use Case

The following use case has not yet been proofread or checked.

NOTE: The in-scope evaluation context of the group with id="hotel" is a single hotel element, one from the nodeset of the parent repeat with id="hotels-repeat". The invocation context('hotel') returns the hotel corresponding to the containing group.

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xf="http://www.w3.org/2002/xforms"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <head>
    <title>Hotel requests</title>
    <style type="text/css">
      .heading { display: div; color: blue; font-size: 16pt;}
    </style> 
    <model xmlns="http://www.w3.org/2002/xforms">
      <instance id="hotels" resource="hotels.xml" />
      <instance id="mark-request">
        <data xmlns="">
          <name />
        </data>
      </instance>
      <submission id="mark-status" method="post" resource="/hotel"
		  ref="instance('hotels')/hotel[name=instance('mark-request')/name]"
		  targetref="instance('hotels')/hotel[name=instance('mark-request')/name]"
		  replace="instance" instance="hotels" />
      <instance id="statuses">
	<data xmlns="">
	  <statuses>
	    <status>vacant</status>
	    <status>full</status>
	    <status>closed</status>
	  </statuses>
	</data>
      </instance>
    </model>
  </head>
  <body>
    <h1>Hotels</h1>
    <xf:group>
      <xf:repeat id="status-repeat" nodeset="instance('statuses')/statuses/status">
	<xf:output class="heading" ref="."><xf:label>Status: </xf:label></xf:output>
        <xf:repeat id="hotels-repeat" nodeset="instance('hotels')/hotel[status=current()]">
          <xf:group id="hotel"> 
             <xf:output value="name">
               <xf:label>Name: </xf:label>
             </xf:output>
             <xf:repeat id="set-status-repeat" nodeset="instance('statuses')/statuses/status[context('status-repeat')/status != .]">
               <xf:trigger>
                 <xf:label>Mark <xf:output ref="context('set-status-repeat')" /></xf:label>
                 <xf:action ev:event="DOMActivate">
                   <xf:setvalue ref="instance('mark-request')/name" value="context('hotel')/name" />
                   <xf:send submission="mark-status" />
                 </xf:action>
               </xf:trigger>
             </xf:repeat>
          </xf:group>
        </xf:repeat>
      </xf:repeat>
    </xf:group>
  </body>
</html>

Sample Instance Data

<?xml version="1.0"?>
<hotels>
  <hotel>
    <name>Dew Drop Inn</name>
    <status>vacant</status>
  </hotel>
  <hotel>
    <name>Popular Place</name>
    <status>full</status>
  </hotel>
  <hotel>
    <name>Motel Zero</name>
    <status>vacant</status>
  </hotel>
  <hotel>
    <name>FlyBuzz In</name>
    <status>closed</status>
  </hotel>
</hotels>