I have a Xalan Java extension which returns a DocumentFragment.
In my XSLT, I invoke it with something like:
<xsl:copy-of select="java:org.foo.myMethod($a, $b)" />
The problem is that where the document fragment contains a text node containing an entity, for example “ ”, this is being inserted as &#160;
Note that I do need to return a DocumentFragment, not a string, because that text node is just part of a tree of XML being returned.
I’m working around this issue as follows:
In the Java code:
Element amp = document.createElement("amp");
xhtmlBlock.appendChild(amp);
Text t = document.createTextNode("#160;");
amp.appendChild(t);
In the XSLT:
<xsl:apply-templates select="java:org.foo.myMethod($a, $b)" mode="amp-workaround" />
<xsl:template match="@*|node()" mode="amp-workaround">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="amp-workaround" />
</xsl:copy>
</xsl:template>
<xsl:template match="amp" mode="amp-workaround">
<xsl:text disable-output-escaping="yes">&</xsl:text><xsl:value-of select="."/>
</xsl:template>
Beware: this workaround only works in certain cases, which I’m still trying to pin down.
Is this a known issue in Xalan 2.7.1?
Is there a better approach (still using Java extensions which return DocumentFragment)?
I don’t know Xalan particularly, but it looks to me as if your Java code is not creating a text node containing an entity or character reference, it is creating a text node containing the characters (&, #, 1, 6, 0, ;).
Wouldn’t it be simplest just to include the character 160 in the returned string: