Reputation: 163
I have an incoming XML where i am having <
instead of <
can we replace all the incoming <
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>
<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>
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
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
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 </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
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( "<", "<" );
Upvotes: 1