Pages

Wednesday, May 29, 2013

Selectively output content using the descendent axis in XSLT

You can select content to output from a data set by using the XSLT descendent axis to key on a component in available tagging. For example, a drug manufacturer may want to output only information about drugs that have listed drug interactions. Each drug topic has an upper-level element called <drug>; drug interactions information is tagged <interactions>.


The programmer can key on the <interactions> element with the descendent axis, like so:

<xsl:template match="drug">
   <xsl:choose>
      <xsl:when test="descendant::interactions">
         <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise/>
   </xsl:choose>
</xsl:template>


What this rule says is that for every <drug> element, test for a nested (descendent) <interactions> element. If the <interactions> element is found anywhere in the document, output the contents (apply templates); otherwise, suppress the <drug> element and all its contents. Using this method, only the <drug> topics containing interactions will be output.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.