Reputation: 1003
I need to output a soap xml request in wihch for some of the nodes <
should be replaced with <
and >
with >
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>
<ISD_XMLGateway>
<Entity>HLR_ALC</Entity>
<Origin>Comverse One</Origin>
<Log_Level>0</Log_Level>
<Params><Param Name="HLR_System" Value="JT"/>
<Param Name="HLR_ALC_Command" Value="Send_HLR_Command"/>
<Param Name="HLR_Command" Value="CRESBX:MSIN=112210231,MODEL=MODEL001,SNBSV=7797242727-TEL;"/>
</Params>
</ISD_XMLGateway>
</requestXml></Create></env:Body></env:Envelope>
Upvotes: 0
Views: 1662
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('<', 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="'>'"/>
<xsl:apply-templates select="node()" mode="escape" />
<xsl:value-of select="concat('</', local-name(.), '>')"/>
</xsl:template>
<xsl:template match="@*" mode="escape">
<xsl:value-of select="concat(' ', name(.), '="')"/>
<xsl:call-template name="EscapeText">
<xsl:with-param name="text" select="." />
</xsl:call-template>
<xsl:value-of select="'"'"/>
</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, '="')"/>
<xsl:call-template name="EscapeText">
<xsl:with-param name="text" select="$namespace" />
</xsl:call-template>
<xsl:value-of select="'"'"/>
</xsl:template>
<xsl:variable name="EntitiesRaw">
<entity value=""" escaped="&quot;" />
<entity value="&" escaped="&amp;" />
<entity value="<" escaped="&lt;" />
<entity value=">" escaped="&gt;" />
<entity value="'" escaped="&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 < 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
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