1

Probably being stupid, but is there a way to include an escaped apostrophe in an xml attribute using xslt?

As far as I understand, characters like the apostrophe are valid in xml elements, but not in their attributes. As a result, I'm trying to escape ' to ' in the output - but I'm running into a bit of bother.

I've tried all sorts, like using xsl:text/disable-output-escaping, ', CDATA (among others) and the closest I've been able to get is to double escape the ampersand like this: '

But I think this means I'm left with the character for an ampersand and separately, the text apos;, rather than the proper ' I'm after.


Input xml:

<?xml version="1.0" encoding="UTF-8"?>
<boof>
    <moo></moo>
</boof>

Current XSL

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="moo">
        <xsl:element name="moo">
            <xsl:attribute name="att">&amp;apos;</xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:transform>

Current output

<boof>
    <moo att="&amp;apos;"/>
</boof>

Desired output

<boof>
    <moo att="&apos;"/>
</boof>
Mal
  • 13
  • 3
  • You can use apostrophes in attribute values just like any other character, unless you use it as the delimiter for the attribute value instead of double quotes. – Martin Honnen Jan 12 '21 at 14:59
  • Thanks! I think I'd misread some of the material I'd read elsewhere - I thought that apostrophes were plain invalid when in attributes. – Mal Jan 12 '21 at 15:13
  • 1
    In XSLT you can concentrate on getting the structure (of the result tree) right. The XSLT processor will look after making sure that it's serialized correctly. – Michael Kay Jan 12 '21 at 18:26

1 Answers1

1

is there a way to include an escaped apostrophe in an xml attribute using xslt?

No. Whether a character is escaped or not is immaterial.

<moo att="'" /> and <moo att="&apos;" /> are exactly the same thing. They describe identical nodes, once the document is parsed they will be indistinguishable.

If you have code that relies on &apos; being there, that code is broken and that's the code you need to fix.

The following will all produce the same result:

<xsl:template match="moo">
    <xsl:element name="moo">
        <xsl:attribute name="att">&apos;</xsl:attribute>
    </xsl:element>
</xsl:template>

or, for short

<xsl:template match="moo">
    <xsl:element name="moo">
        <xsl:attribute name="att">'</xsl:attribute>
    </xsl:element>
</xsl:template>

or, even shorter

<xsl:template match="moo">
    <moo att="'" />
</xsl:template>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Ah, right that simplifies things a lot then, thanks! There's no specific broken code at the minute, I just wanted to make sure I was outputting valid xml. I'd just seen conflicting information stating that the ' character was invalid, like the first answer [here](https://stackoverflow.com/questions/730133/what-are-invalid-characters-in-xml). I can see now that it's only "strongly advised" when not using characters of the same type. – Mal Jan 12 '21 at 15:07
  • @Mal The `'` character is valid anywhere, except in attributes that are enclosed in single quotes (`att='...'...'` encodes `...'...`). The same applies to the `"` character (`att="..."..."` encodes `..."...`). The escape sequences `'` and `"` are only really necessary in these circumstances. But of course they work everywhere, so you would not invalidate your document by using `'` every time you want to refer to a single quote. The XSLT processor decides whether it uses the literal or the escaped variant in attributes. It won't mess this up. – Tomalak Jan 12 '21 at 15:14