XSLT transformations work in IE and Stylus Studio but not in Firefox or Opera

May 10th, 2007 Nick Posted in xml No Comments »

We were developing some stylesheets to convert xml to html in Stylus Studio and had the problem that IE would render them fine, but when we used Firefox/Opera we only saw plain text.

If you are also experiencing this problem you are probably missing the xsl:output element:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>

When the XSL processor sees this instruction it will generate a meta data element similar to the following in the resulting html header, which sets the content type to text/html. This lets the browser know to treat the resulting text as html:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

The problem is - some XSL processors (e.g. Saxon ) appear to generate this meta data item even if the xsl:output is absent - by detecting the html tags in the document. It appears that the processor built into IE has this feature, so IE correctly interprets the html, even without the xsl:output. Firefox, however, does not.



AddThis Social Bookmark Button

XSL To Remove Elements

April 10th, 2007 Nick Posted in xml No Comments »

Recently I was faced with a directory full of XML files containing test data. The tests were all failing due to a couple of unwanted elements within each test file.

The question - how to write an xsl stylesheet to remove the offending elements.
After some head scratching the following script seemed to do the trick

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <!-- Identity transform -->
        <xsl:template match="@*|node()">
                <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
        </xsl:template>

        <xsl:template match="badElement"/>     
       
 </xsl:stylesheet>

This recreates the original xml file exactly, minus any elements called badElement
How does it work?

Well the first part is called an identity transform. This template matches every input node in the source document, (elements attributes and content_, and copies it to the output tree.

The second template matches only on elements called badElement. Clearly, the badElement in the source document will match both the identity transform template and the badElement template. . The XSL specification defines behaviour when a source node matches multiple templates. Essentially, each template has an associated priority. In general, a more specific match gets a higher priority by default, which is why in this case the badElement node is processe by the badElement template rule. This rule does nothing - it has not output and does not contain an apply-templates, effectively supressing its child nodes too. Which is why the badElement and its child nodes are effectively filtered out from the target document.

If in doubt over which template will take priority, it is actually possible to specify a priority value explicitly as an attribute to the template declaration in the xsl stylesheet.


AddThis Social Bookmark Button