element (element-name (| element-name)* | #implied)  condition?


You use element rules to process elements in XML or SGML data. For instance, the following rule processes a "price" element:

  element "price"
     output "$%c"

Every element rule must call "%c" or suppress once and only once. The call may occur in a function called from the rule.

You can have more than one element rule for an element, provided only one of them can be selected at any given time:

  element "price" when parent is "widget"
  element "price" when parent is "dohickey"

Note that, unlike find rules, where the first matching rule is fired, even if multiple rules could match the data, element rules require that there always be one and only one selectable rule in all circumstances. Therefore, the following rules will cause an error for a "price" element whose parent is "widget", since either rule could be selected:

  element "price" when parent is "widget"
  element "price" 

You can have a single rule fire for more than one element type by specifying the list of element names in parentheses separated by the or operator |:

  element ("price" | "cost")
     output "$%c"

If you have many elements for which you want the same processing, you can use an element #implied rule:

  element #implied

You can qualify an element #implied rule, just like any other element rule. For instance, you can provide a rule for all the subelements of a paragraph:

  element #implied when parent is "p"

Information about the current parse state

Much of the processing you want to do in an element rule depends on information about the current state of the parse. The following information is available on the current parse state (some of these items do not apply to XML):

  • the current element stack can be viewed using current elements
  • the element qualifiers of element, of parent, of ancestor, of doctype, of open element let you refer to the current element and it parents
  • the tests element is, parent is, ancestor is, preparent is let you test the identity of the current element and its parents
  • the test previous is lets you test the identity of the preceding element
  • the test last subelement is lets you test the identity of the preceding subelement
  • open element is lets you test if an element is open
  • name of returns the name of the specified element
  • the format item "%q" returns the name of the current element
  • attribute returns the value of a specified attribute
  • the attributes shelf contains the names and values of all the attributes of the specified element
  • usemap is lets you determine if the current element has a short reference map
  • content is lets you determine the type of content model the current element has
  • occurrence returns the number of consecutive occurrences of the current element type
  • children returns the total number of children of a specified element seen so far
  • status is lets you determine why an element was allowed in a content model

The above keywords can be combined in all the meaningful combinations, so that you can say, for example:

  output attribute "type" of parent of ancestor "listitem"

Related Syntax