shaiksha
shaiksha

Reputation: 1003

i want to replace > with > for some of my nodes in my xml using xslt

I need to output a soap xml request in wihch for some of the nodes < should be replaced with &lt; and > with &gt; how to do this using xslt my output should be something like below

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<env:Body><Create xmlns="http://jerseytelecom.com/">
<requestXml>
&lt;ISD_XMLGateway&gt;
&lt;Entity&gt;HLR_ALC&lt;/Entity&gt;
&lt;Origin&gt;Comverse One&lt;/Origin&gt;
&lt;Log_Level&gt;0&lt;/Log_Level&gt;
&lt;Params&gt;&lt;Param Name=&quot;HLR_System&quot; Value=&quot;JT&quot;/&gt;
&lt;Param Name=&quot;HLR_ALC_Command&quot; Value=&quot;Send_HLR_Command&quot;/&gt;
&lt;Param Name=&quot;HLR_Command&quot; Value=&quot;CRESBX:MSIN=112210231,MODEL=MODEL001,SNBSV=7797242727-TEL;&quot;/&gt;
&lt;/Params&gt;
&lt;/ISD_XMLGateway&gt;
</requestXml></Create></env:Body></env:Envelope>

Upvotes: 0

Views: 1662

Answers (2)

JLRishe
JLRishe

Reputation: 101680

I believe that this should work for escaping XML in XSLT 1.0:

  <xsl:template match="*" mode="escape">
    <xsl:variable name="currentNode" select="." />
    <xsl:value-of select="concat('&lt;', name(.))"/>
    <xsl:apply-templates select="@*" mode="escape"/>
    <xsl:for-each select="namespace::*[name() != 'xml'][not(. = $currentNode/../namespace::*)]">
      <xsl:call-template name="EscapeNamespace">
        <xsl:with-param name="namespace" select="." />
      </xsl:call-template>
    </xsl:for-each>
    <xsl:value-of select="'&gt;'"/>
    <xsl:apply-templates select="node()" mode="escape" />
    <xsl:value-of select="concat('&lt;/', local-name(.), '&gt;')"/>
  </xsl:template>

  <xsl:template match="@*" mode="escape">
    <xsl:value-of select="concat(' ', name(.), '=&quot;')"/>
    <xsl:call-template name="EscapeText">
      <xsl:with-param name="text" select="." />
    </xsl:call-template>
    <xsl:value-of select="'&quot;'"/>
  </xsl:template>

  <xsl:template match="text()" mode="escape">
    <xsl:call-template name="EscapeText">
      <xsl:with-param name="text" select="." />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="EscapeNamespace">
    <xsl:param name="namespace" />

    <xsl:variable name="prefix">
      <xsl:choose>
        <xsl:when test="name($namespace) = ''">
          <xsl:value-of select="'xmlns'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat('xmlns:',name($namespace))" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:value-of select="concat(' ', $prefix, '=&quot;')"/>
    <xsl:call-template name="EscapeText">
      <xsl:with-param name="text" select="$namespace" />
    </xsl:call-template>
    <xsl:value-of select="'&quot;'"/>
  </xsl:template>

  <xsl:variable name="EntitiesRaw">
    <entity value="&quot;" escaped="&amp;quot;" />
    <entity value="&amp;" escaped="&amp;amp;" />
    <entity value="&lt;" escaped="&amp;lt;" />
    <entity value="&gt;" escaped="&amp;gt;" />
    <entity value="&apos;" escaped="&amp;apos;" />
  </xsl:variable>
  <xsl:variable name="Entities" select="msxsl:node-set($EntitiesRaw)" />

  <xsl:template name="EscapeText">
    <xsl:param name="text" />

    <xsl:variable name="foundEntity" select="$Entities/entity[contains($text, @value)]" />
    <xsl:choose>
      <xsl:when test="$foundEntity">
        <xsl:call-template name="EscapeText">
          <xsl:with-param name="text" select="substring-before($text, $foundEntity/@value)" />
        </xsl:call-template>
        <xsl:value-of select="$foundEntity/@escaped" />
        <xsl:call-template name="EscapeText">
          <xsl:with-param name="text" select="substring-after($text, $foundEntity/@value)" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

You would simply build up the XML you want to escape in a variable, and then run it through the templates:

  <xsl:template match="something">
    <requestXml>
    <xsl:variable name="requestXml">
      <ISD_XMLGateway>
        <Entity><xsl:value-of select="2 + 5" /></Entity>
        <Origin>5 &lt; 6 and 7 > 2</Origin>
        <xsl:apply-templates select="otherStuff" />
        <ElementWithAttributes a="10" b="12" />
      </ISD_XMLGateway>
    </xsl:variable>

    <xsl:apply-templates select="msxsl:node-set($requestXml)" mode="escape" />
    </requestXml>
  </xsl:template>

The above uses msxsl:node-set() in two places, but I believe that most non-MS XSL engines have an equivalent for that function.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163322

Well, XSLT never gets to see the "<" and ">" characters in your XML markup, because it doesn't work on lexical XML, it works on a tree representation of the XML. So you've formulated the question incorrectly. What you actually want to do is to serialize a tree to lexical XML and then insert that lexical XML as a string into your result tree (following which the serializer will convert the "<" and ">" characters in that lexical XML into "& lt;" and "& gt;").

There's no built-in serialize() function to do this in XSLT 2.0, but recent Saxon releases (commercial editions only) have support for the XSLT 3.0 serialize() function.

Upvotes: 1

Related Questions