Thursday, September 04, 2008

XLST and Namespace Pain

OK, imagine you have this XML:

<?xml version = "1.0" encoding = "ISO-8859-1"?>
<alert xmlns = "urn:oasis:names:tc:emergency:cap:1.1">
  <info>
   <category>Met</category>
  </info>
<alert>

How do you refer to the "category" tag in XPath for XSLT, as it is in the "urn:oasis:names..." namespace?

  1. Ignore the namespace, and do something like:  

    <xsl:apply-templates select ="//*[local-name()='category']" />

  2. Define the namespace in the stylesheet tag and use the full path:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:cap = "urn:oasis:names:tc:emergency:cap:1.1">
    ...
    <xsl:apply-templates select ="cap:alert/cap:info/cap:category" />


  3. Or define the namespace and use a simplified path:

    <xsl:apply-templates select ="//cap:category" />


It took me a long time to work this out....

1 comment:

Anonymous said...

Awesome... thanks :)