| Skip over navigation | ATTSATTS is an XSLT meta-stylesheet for assigning default attribute values to an XML document. ATTS is intended for XML applications lacking a convenient method, such as a DTD (document type definition), for specifying attribute defaults. Since the attributes that control architectural processing are usually fixed, ATTS can be used to supply their values. ATTS is therefore handy for DTD-less applications using APEX. Use atts as follows:
Consider the following default attribute value specification: <atts:attributes xmlns:atts="http://www.nist.gov/atts">
<e>
<a1>hello</a1>
<a2>world</a2>
</e>
</atts:attributes>
Processing this default attribute value specification using the atts.xsl transform generates this XSLT stylesheet. Now consider the following XML document: <?xml version="1.0"> <e a2="everybody"/> Processing the XML document with the generated stylesheet assigns default attribute values as follows: <?xml version="1.0" encoding="utf-8"?> <e a1="hello" a2="everybody"/> Notice that attribute a1 gets its default value, but attribute a2 retains its value from the original XML document. A more complex example is included in the XSLToolbox distribution and can be run using the Ant build file atts/examples/build.xml. ATTS has an optional context attribute for differentiating between context-sensitive elements. The value specified for context should be an XPath expression referring to the element's containing element. For example, consider the following XML document in a file called foo.xml: <e> <e/> </e> Suppose that the top-level element e has a default attribute a1, and the nested element e has default attributes a1 and a2. Now consider the following default attribute value specification: <atts:attributes xmlns:atts="http://www.nist.gov/atts">
<e>
<a1>default for attribute a1 of top level element e</a1>
</e>
<e atts:context="e">
<a1>default for attribute a1 of nested element e</a1>
<a2>default for attribute a2 of nested element e</a2>
</e>
</atts:attributes>
Applying atts.xsl to this default attribute value specification and applying the resulting XSLT transform to foo.xml produces the following: <?xml version="1.0" encoding="utf-8"?>
<e a1="default for attribute a1 of top level element e">
<e a1="default for attribute a1 of nested element e"
a2="default for attribute a2 of nested element e"/>
</e>
The output transform generated by ATTS can handle default attribute value specifications with Xlinks only because the atts.xsl transform hard-codes the Xlink namespace declaration into the output. If your default attribute value specification declares namespaces other than the Xlink namespace, you will need to either add these namespace declarations to the output transform yourself or hard-code the namespace declarations into atts.xsl. If anyone knows of a way to get atts to automatically declare namespaces in the output transform that are declared in the default attribute value specification, I would certainly like to hear about it. |