Question

I am currently running in some trouble when trying to output a certain XML tag containing the Ampersand sign (&).

So more concrete, when I try to output the following tag. I get an error

<fullname>Ben & Jerry</fullname>

Using the following tag however runs just fine

<fullname>Ben and Jerry</fullname>

I have tried it with the following code

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

And I also tried

<xsl:template match="fullname">
    <xsl:value-of-select="." disable-output-escaping="Yes"/>
</xsl:template>

Both resulted in an error. The only way how I get it to work is by using CDATA like this

<fullname><![CDATA[Ben & Jerry]]></fullname>

However, I have no control over the XML files I receive, and as such this is not a viable option. Is there something I can do within the XSLT to circumvent/fix this problem?

Thanks!

Was it helpful?

Solution

Ampersand can't appear as itself in well-formed XML. Your example should be

<fullname>Ben &amp; Jerry</fullname>

I don't think you'll be able to get around this with any XSLT processor. You need to fix whatever generates the XML so it is well-formed.

OTHER TIPS

So more concrete, when I try to output the following tag. I get an error

<fullname>Ben & Jerry</fullname>

Actually, this isn't a tag. It's a start tag followed by some invalid content followed by an end tag.

You can't output invalid content using XSLT. If you have to output something that isn't XML, you'll need to use a non-XML tool to do it. And you're more likely to get advice on a non-XML forum.

What you haven't made clear is exactly what your input and output are. You say you are trying to output invalid XML, but you don't say what the input is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top