Apr 252011
 

XSLT/XPath 2.0/3.0 are powerful technologies. But sometimes they’ll drive you nuts. A large share of issues falls into the category of “why doesn’t my template match?” The reasons are manifold. Here are some typical traps:

  • not including the namespace prefix
  • typo in the namespace URI
  • processing the document in a different mode than the template is supposed to match in. A very subtle example, accidentally closing the xsl:template start tag, cutting off the existing mode declaration – this actually happened to me:
<xsl:template match="HyperlinkTextSource[…]">
    mode="idml2xml:ConsolidateParagraphStyleRanges-remove-empty" priority="4">
  • other typos (in predicates, element names, modes)
  • skipping intermediate elements, e.g., formulating predicates for <td>s when the template is supposed to match <tr>
  • other templates have higher priority
  • import precedence: this template is an imported one, and there is a matching template of whatever priority in the importing stylesheet, or in a template imported after the template in question
  • logical misconceptions in the predicates
  • if the template is supposed to match a result of a previous transformation: the output of the previous tranformation is not as expected
  • processing some surrounding element with xsl:copy-of instead of xsl:apply-templates
  • not looking at the actual output document, or looking at another part of the document, while your template indeed matched (thanks, @fbuehring)

My tactics of debugging these cases include

Continue reading »