1

Input XML is this:

<input>
  <foo>John&apos;s bar</foo>
  <bar>test</bar>
  <foobar>testing</foobar>
</input>

After XSL transformation:

<input>
  <foo>John's bar</foo>
  <bar>this_test</bar>
</input>

But the legacy system expects:

<foo>John&apos;s bar</foo>

not <foo>John's bar</foo>

So I want to retain the value under <foo> as is rather than let XSLT parse it.

I tried using <xsl:output method="text"/> but with no luck of success..

I think XML itself when loaded gets parsed and XSLT just outputs as is.. If that's true I atleast want to escape it and make &apos; irrespective of whether it was &apose or ' in the input XML.

XSLT that I tried is this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="bar">
    <xsl:copy>
        <xsl:text>this_</xsl:text>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
<xsl:template match="foobar"/>
</xsl:stylesheet>
Enthusiastic
  • 549
  • 4
  • 8
  • 23

1 Answers1

1

If you are limited to XSLT 1.0, use disable-output-escaping="yes". This attribute can be used on xsl:text and xsl:value-of elements and it is deprecated in XSLT 2.0.

Stylesheet (XSLT 1.0)

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <xsl:variable name="vApos">'</xsl:variable>
   <xsl:variable name="vAmp">&amp;</xsl:variable>

   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="node()"/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="bar">
       <xsl:copy>
           <xsl:text>this_</xsl:text>
           <xsl:apply-templates/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="foobar"/>

   <xsl:template match="foo">
      <xsl:variable name="rep">
         <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="replace" select="$vApos" />
            <xsl:with-param name="with" select="concat($vAmp,'apos;')"/>
         </xsl:call-template>
      </xsl:variable>
      <xsl:copy>
         <xsl:value-of select="$rep" disable-output-escaping="yes"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$with"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

The XSLT 1.0 solution makes use of advice given by Dimitre Novatchev here and Mads Hansen's answer here.

The XSLT 2.0 solution is more elegant, use a character-map to control the serialization of output. Make sure you escape the ampersand character as well (&amp;apos; instead of &apos;).

Stylesheet (XSLT 2.0)

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

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes" use-character-maps="apo"/>
   <xsl:strip-space elements="*"/>

   <xsl:character-map name="apo">
      <xsl:output-character character="&apos;" string="&amp;apos;"/>
   </xsl:character-map> 

   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="node()"/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="bar">
       <xsl:copy>
           <xsl:text>this_</xsl:text>
           <xsl:apply-templates/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="foobar"/>

</xsl:stylesheet>

Output (Saxon 9.5 for 2.0, Xalan 2.7.1 for 1.0)

<?xml version="1.0" encoding="UTF-8"?>
<input>
   <foo>John&apos;s bar</foo>
   <bar>this_test</bar>
</input>
Community
  • 1
  • 1
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75