using attributes

control structure

using attribute-reference (as alias-name)?


You can use using to create an alias for an attribute in the current parsing state. Attributes available in the current parsing state include the attributes of the current element and the attributes of all open elements.

Suppose that you are processing an element called section that has a parent called chapter and the chapter element has an attribute number. You can access the number attribute of the chapter element as follows:

  element "section"
     output attribute "number" of parent

You can use using to simplify the reference to this attribute by enabeling you to refer to it by its attribute name alone (and thus to use the %v format item):

  element "section"
     using attribute "number" of parent
        output "%v(number)"

This is valuable because when an attribute is output using %v, translate rules are applied to it. If it is output using the attribute keyword or is read from the attributes shelf, translate rules are not applied.

Suppose that the section element also has an attribute number. In this case we need to be able to distinguish between the two attribute names. To do this we can add an as clause to using to give the chapter level number attribute another name in the local scope:

  element "section"
     using attribute "number" of parent as chapter-number
        output "%v(chapter-number).%v(number)"  

To further clairify this code, we can use using to give the local number attribute a distinct name also:

  element "section"
     using attribute "number" as section-number
        using attribute "number" of parent as chapter-number
           output "%v(chapter-number).%v(section-number)"  

The scope of the using prefix is the lexical scope to which it applies (either the following action, or a scope defined by a block).

When used with an attribute, the using prefix must include either an element specifier (introduced by of) or an alias (introduced by as), or both.

You can refer to the attribute you are interested in using either the attribute keyword (as in the examples above) or by refering to items on the attributes shelf, as shown below:

  element "section"
     using attributes {"number"} as section-number
        using attributes of parent {"number"} as chapter-number
           output "%v(chapter-number).%v(section-number)"  

You can also use using with the data-attribute keyword and the data-attributes shelf in the same way.

Finally, you can use using to select an item from a list valued attribute:

  element "foo"
     using attribute "bar" [3] as "bar-3"
        output "%v(bar-3)"