Force output of quotes around empty string value in xsl attribute

Is there a way to force the quote output of an attribute with an empty string value?
So that:
<xsl:attribute name="alt" select="''"/>

will result in:

Can it be done with some xsl output parameter or post-processor?
Or is there a specific select formula that could give me empty double quotes as attribute value?

First of all, what you are passing into the select is currently an empty string, as you quotes just mark a string object. In XSLT, an empty string is (simplified) basically a falsy value, and thus won’t be a part of the output.

<xsl:attribute name="alt" select="''"/>
will result in an empty string = empty alt-attribute

<xsl:attribute name="alt" select="'something'"/>
will result in outputting

As I am assuming you just want an empty alt attribute to prevent screen readers from reading the image filename/path, it might be that simply outputting a string with a single whitespace will be sufficient as well.

<xsl:attribute name="alt" select="' '"/>
<xsl:attribute name="alt" select="'&nbsp;'"/>

If this is not your use-case (and if I am remembering correctly), you need to output HTML entities if you want to output quotes. In this case, &apos; could perhaps do it.

That is:
<xsl:attribute name="alt" select="'&apos;&apos;'"/>
(possibly with a space between)

1 Like

I don’t think @it_vegard is answering the original question, although his answer is correct if you want to insert quotes into an attribute (I would personally insert a variable containing an unescaped quote symbol).

I think @ksawery is really asking how to output an HTML5 boolean attribute, like the code snippet on the line below:
<img alt />

In that case, the only way I know you can achieve that using the output methods provided by XSLT is by unescaping a text string, by using the following code:
<xsl:text disable-output-escaping="yes">&lt;img alt /&gt;</xsl:text>

The W3 spec provides an alternative solution for boolean attributes:

If the [boolean] attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute’s canonical name, with no leading or trailing whitespace.


The spec contains the following examples:

  1. <input type="checkbox" checked name="cheese" disabled />
  2. <input type="checkbox" checked="checked" name="cheese" disabled="disabled" />
    Both are valid HTML5, but only the second example is possible in XSLT unless you want to resort to unescaped text like in my proposed solution above.

In other words, for boolean attributes just make sure that the attribute value matches the attribute name.

However, the example in the OP of an alt attribute on an img tag concerns me. Alt attributes are not boolean attributes! They are always required on img tags, and the value must either be a string containing descriptive text, or an empty string <img alt="">

While the W3 spec does provide some guidelines for when empty alt attributes in chapter, their wording is very general and vague.
I would instead follow the much more real-world-oriented guidelines given by WebAIM for alternate text on images. Their examples are extremely well chosen, and provides good guidelines for common use cases.

1 Like

Thanks for the answers, I wanted to make sure that the empty alt attribute is rendered in HTML as a empty string, not an boolean flag attribute, which is interpreted as such by Chrome for example (in dev -> elements), although the source contains the empty double quotes. It seems that empty string gives the desired behavior for screen readers. I had reports that a space in quotes can cause some quirky problems with some software, so it’s better to trim white-space on the alt attribute.
<xsl:attribute name="alt" select="''"/>
Gives the desired alt="" in html output it seems, but wanted to know if there is a setting/variable that can ensure that it will be rendered as empty string.