user5458829
user5458829

Reputation: 163

Replacing < with < using XSLT

I have an incoming XML where i am having &lt; instead of < can we replace all the incoming &lt; with < tag using XSLT. Can somebody let me know how can we achieve that.

Input:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:pr xmlns:ns1="http://example.com">
            <ns1:dis>
                &lt;TP memberID="121897679001" FirstName="Tom" LastName="regen" auId="42424234">
                    &lt;TPV>
                        &lt;KAN>
                            &lt;sur sType="POI" cDate="09022017" MDate="09022017" eDate="05182003">
                                &lt;question id="9" desc="afa">
                                    &lt;ans id="0" desc="des">09172017</ans>
                                &lt;/question>                              
                            &lt;/sur>
                        &lt;/KAN>
                    &lt;/TPV>
                &lt;/TP>
            </ns1:dis>
        </ns1:pr>
    </soapenv:Body>
</soapenv:Envelope>

Expected Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:pr xmlns:ns1="http://example.com">
            <ns1:dis>
                <TP memberID="121897679001" FirstName="Tom" LastName="regen" auId="42424234">
                    <TPV>
                        <KAN>
                            <sur sType="POI" cDate="09022017" MDate="09022017" eDate="05182003">
                                <question id="9" desc="afa">
                                    <ans id="0" desc="des">09172017</ans>
                                </question>                             
                            </sur>
                        </KAN>
                    </TPV>
                </TP>
            </ns1:dis>
        </ns1:pr>
    </soapenv:Body>
</soapenv:Envelope>

Upvotes: 1

Views: 2636

Answers (3)

YaP
YaP

Reputation: 377

I just found the easiest would be to use disable-output-escaping="yes", like:

<xsl:value-of select="." disable-output-escaping="yes"/>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167516

Assuming the contents of that ns1:dis element is a properly escaped XML fragment (your snippet is not, because the </ans> would need to be &lt;/ans>) you can as of today use XSLT 3.0 and the parse-xml-fragment function (https://www.w3.org/TR/xpath-functions-31/#func-parse-xml-fragment) as follows:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="dis" xpath-default-namespace="http://example.com">
        <xsl:copy>
            <xsl:copy-of select="parse-xml-fragment(.)"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Should work with Saxon 9.8 (all editions) or 9.7 (PE or EE) and with current releases of Altova XMLSpy/Raptor as with any other XSLT 3.0 implementations.

Upvotes: 2

SomeDude
SomeDude

Reputation: 14228

Your xml is not valid because </ans> is not started with <ans>, I don't believe you can apply XSLT on it.

If your language is Java, you can use replaceAll like:

inputXml = inputXml.replaceAll( "&lt;", "<" );

Upvotes: 1

Related Questions