Diff for XSLT coding practices in the DITA-OT
Fri, 2009-04-03 20:06 by robander | Fri, 2009-04-03 20:10 by robander | ||
---|---|---|---|
< previous diff | next diff > | ||
Changes to Body | |||
Line 16 | Line 16 | ||
</ul>
| </ul>
| ||
<blockquote>
| <blockquote>
| ||
- | <xsl:template match="*" mode="ditamsg:required-cleanup-in-content"><br />
| + | <xsl:template match="*" mode="ditamsg:required-cleanup-in-content"><br />
|
- | <xsl:call-template name="output-message"><br />
| + | <xsl:call-template name="output-message"><br />
|
- | <xsl:with-param name="msgnum">039</xsl:with-param><br />
| + | <xsl:with-param name="msgnum">039</xsl:with-param><br />
|
- | <xsl:with-param name="msgsev">W</xsl:with-param><br />
| + | <xsl:with-param name="msgsev">W</xsl:with-param><br />
|
- | </xsl:call-template><br />
| + | </xsl:call-template><br />
|
- | </xsl:template><br />
| + | </xsl:template>
|
</blockquote>
| </blockquote>
| ||
- | <br />
| + | <blockquote>
|
- | <ul>
| + | </blockquote>
|
- | <li>
| + | <p>
|
- | <ol>
| + | Keeping messages out of line has many advantages:
|
- | <li>It is easy to selectively override a message for a specialized element</li>
| + | </p>
|
- | <li>It is easy to override a single message if you do not want it to appear (you override the message, not the element that generates it) </li>
| + | <ol>
|
- | <li>It is easier to reuse messages, and the message number is only defined once in a file</li>
| + | <li>It is easy to selectively override a message for a specialized element</li>
|
- | <li>The mode name is self-documenting, indicating what the message is about</li>
| + | <li>It
|
- | <li>In the future we may group message into a single imported file, so messages may be reused between modules</li>
| + | is easy to override a single message if you do not want it to appear
|
- | </ol>
| + | (you override the message, not the element that generates it) </li>
|
- | <ul>
| + | <li>It is easier to reuse messages, and the message number is only defined once in a file</li>
|
- | <li>Keeping messages out of line has many advantages:</li>
| + | <li>The mode name is self-documenting, indicating what the message is about</li>
|
- | </ul>
| + | <li>In the future we may group message into a single imported file, so messages may be reused between modules</li>
|
- | </li>
| + | </ol>
|
- | </ul>
| + | |
Revision of Fri, 2009-04-03 20:10:
XSLT coding practices in the DITA-OT
XSLT Coding practices in the DITA-OT
The core developers on the DITA Open Toolkit generally try to keep several coding practices and design patterns in mind when adding to the toolkit's XSLT code. If you are interested in digging around in the DITA Open Toolkit, or in contributing XSLT patches back to the core toolkit, you may want to keep these practices in mind:
- Always use the specialized form to refer to elements. Never use a literal element name in an XPATH expression. (The one exception is the <dita> element, which does not have a class attribute). For example, instead of matching "table", match "*[contains(@class,' topic/table ')]". It's more verbose and a bit harder to read, but it's what makes specialized processing possible.
- Do not use priority attributes. The toolkit code is written to expect overrides (related to specialization or otherwise). If XSLT for one specialization uses priorities, then all overrides of that module must copy or increase the priority, making management difficult. It is better to simply let XSLT's native import precedence determine priority.
- Always use mode templates in place of named templates. Name templates are only appropriate for basic functions unrelated to the context, such as string operations ("get-extension"); although even basic functions also work as modes. The reason is that it is possible to override mode templates; in addition, it becomes easy to override a particular class of elements. For example, with a template named "add-linking-attributes", you can only replace that template for every element. With match="*" and mode="add-linking-attributes", you can create an override that updates your specialized <xref> but leaves <xref> itself untouched.
- If you are patching code in a manner that replaces existing named templates, do not remove the named template, which may break existing customizations. Instead, the named template should be re-written to call its replacement.
- When mode templates are added to the preprocessing modules, they should contain namespaces. Most of the modules already define these namespaces, so the same pattern should be followed (such as "mappull:" and "topicpull:"). The existing modes and their purpose should be described in a comment at the top of the file.
- New error messages should always be created with a mode template that uses the ditamsg: namespace; the mode should describe the message. For example:<xsl:apply-templates select="." mode="ditamsg:required-cleanup-in-content"/>
- The message itself should be defined at the end of the module:
<xsl:template match="*" mode="ditamsg:required-cleanup-in-content">
<xsl:call-template name="output-message">
<xsl:with-param name="msgnum">039</xsl:with-param>
<xsl:with-param name="msgsev">W</xsl:with-param>
</xsl:call-template>
</xsl:template>
Keeping messages out of line has many advantages:
- It is easy to selectively override a message for a specialized element
- It is easy to override a single message if you do not want it to appear (you override the message, not the element that generates it)
- It is easier to reuse messages, and the message number is only defined once in a file
- The mode name is self-documenting, indicating what the message is about
- In the future we may group message into a single imported file, so messages may be reused between modules