Attribute Handling
Introduction
Attribute values are easy to extract with PTC Arbortext Layout Developer, and have long been used to directly affect formatting properties:
• With element- and context-based tag matching, the attribute values on the current element are extracted for use
• In JavaScript, attribute values are passed as an argument to the JavaScript being run
• Attribute values can be gathered from the fFormatPos object that represents the current tag being formatted
Attribute Values in JavaScript
When an PTC Arbortext Layout Developer JavaScript tag is processed as a result of element tag matching or context matching, all attributes on that element are passed to the JavaScript as a simple hash type argument. The arguments[0].attributes hash is a list of attribute name/value pairs. For example, for the element <tag value="1" name="thisTag" height="24pt"/>, attribute values would be gathered from the arguments[0].attributes array as shown:
var atts = arguments[0].attributes;
var v = atts.value;
var n = atts.name;
var h = atts.height;
It may be advisable to check if the attribute exists or has a value before carrying out this kind of variable declaration. If you are confident that the attribute or value exists, this is a simple and effective approach.
|
In the Arbortext Styler integration, the composition pipeline adds any default attributes on elements based on their DTD values to the XML. Content may include unexpected attributes when PTC Arbortext Layout Developer receives it.
|
Attribute Values Based on fFormatting.streamHierarchy and fFormatPos
When using element name tag matching, you can also access the attribute values applied to the current element with the fFormatPos object that represents the currently formatted element. This is available from the fFormatting.streamHierarchy array. fFormatting.streamHierarchy contains a list of fStreamPos objects, which inherit from fFormatPos, that give you the attribute values.
For example, to get the currently formatted element from fFormatting.streamHierarchy:
var element = formatting.streamHierarchy[0];
This returns an fStreamPos object, which you can then query for the attributes:
var atts = element.attributes;
var v = atts.value;
var n = atts.name;
var h = atts.height;
This output would be generated for the tag element mentioned in Attribute Values in JavaScript.
With fFormatting.streamHierarchy, you can find attributes of any markup in the tree of elements which have been called to reach the current position in the formatting stack.
Using Attribute Values to Specify Formatting Properties
Some content models provide formatting properties in the form of attribute values on particular elements. PTC Arbortext Layout Developer can use these values directly to drive formatting properties. Using the tag described in Attribute Values in JavaScript as a basis, this sample code can set the text height using the tag’s height attribute value:
formatting.currentStyle.height = arguments[0].attributes.height;
PTC Arbortext Layout Developer takes the string 24pt that it extracted from the tag’s height attribute and converts it to an fLength object suitable for the fStyle.height property.