Make empty elements with XSLT

I’ve been working with a lot of XSLT recently related to our BLDoc project. In the case of one of the translations, I had a need to create a resulting document that used only empty XML elements, such as:

<element />

However, no matter what I did the XSLT always produced elements like this:

<element></element>

Technically, the above two are equivalent according to XML guidelines. However, there are two situations where one might prefer the first over the second. (1), it saves bandwidth in large documents and (2) not all XML handling engines are really XML compliant.

In my case, I’m dealing with Flash MX 2004 help content which is not an XML compliant parser. It throws errors when you have closing tags in the custom actions file.

A search of Google Groups turned up about a thousand responses as usual, most of which politely said “it doesn’t matter, they’re the same thing, and if the XML parser you’re working with thinks they’re different then it’s not a compliant parser.” Unfortunately, I’m stuck with the parser built into Flash MX 2004 which is non-compliant.

I finally came across one helpful post that suggested creating the elements as text instead of using real tags.

<xsl:text disable-output-escaping="yes">&lt;identifier text="</xsl:text><xsl:value-of select="@name" /><xsl:text disable-output-escaping="yes">" /&gt;</xsl:text>

Which worked great.

This helpful answer actually came from an official support address at Microsoft. Kudos to them for answering the question and holding off on the theoretical slander.

One caveat is that it’s real easy to create invalid XML documents when creating elements from text–you have to make sure they’re well formed and all content is entity encoded.

5 thoughts on “Make empty elements with XSLT

  1. Hey Sam,

    Add an element with its ‘method’ attribute set to ‘xml’, as a child of your main stylesheet element…

    That should do it for you!

  2. Hey Sam,

    Add an element with its ‘method’ attribute set to ‘xml’, as a child of your main stylesheet element…

    That should do it for you without having to resort to creating your empty elements as text.

  3. Hey Sam,

    Okay, so your commenting system doesn’t like < and > Let’s try again…

    Add an <xsl:output /> element with its ‘method’ attribute set to ‘xml’, as a child of your main stylesheet element…

    <xsl:stylesheet version=”1.0″ …>
    <xsl:output method=”xml” />

    </xsl:stylesheet>

    That should do it for you without having to resort to creating your empty elements as text.

  4. Yup, have the output tag in there, it still creates opening and closing tags. I think it has to do with the fact that I am creating elements with <xsl:element> which requires opening and closing tags in the XSLT in order to create dynamic attributes–it must contain the <xsl:attribute> tag.

  5. It’s been a while since this thread was added to (by anything other than spambots), but I’ll add that I was having the same problem.

    Empty elements with attributes were being given a closing tag, rather than the compact single, closed tag.

    In the end, adding the following attribute to the xsl:stylesheet element worked for me,

    xmlns=”http://www.w3.org/1999/xhtml”

    This, in conjunction with an xsl:output element with method=”xhtml”, produces single, closed tags with a space before the forward slash (because of compatibility problems that some old browsers have), which is exactly as I need it.

    I’m using Saxon-9b, by the way.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">