0
votes

I need to disable output escaping and remove occurrence of XML definition i.e xml version="1.0" encoding="UTF-8"?>

Input Data:

<ns1:outSystemWSResponse xmlns:ns1='http://abcd.co.za'
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
    <ns1:out>&#60;?xml version="1.0" encoding = "UTF-8"?&#62;&#60;PQR&#62;
        &#60;STU&#62;
        &#60;TEST1&#62;Pen&#60;/TEST1&#62;
        &#60;TEST2&#62;Table&#60;/TEST2&#62;
        &#60;/STU&#62;
        &#60;/PQR&#62;</ns1:out>
</ns1:outSystemWSResponse>

Expected Output

<?xml version="1.0" encoding="UTF-8"?>
<ns1:outSystemWSResponse xmlns:ns1="http://abcd.co.za"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns1:out>
        <PQR>
            <STU>
                <TEST1>Pen</TEST1>
                <TEST2>Table</TEST2>
            </STU>
        </PQR>
    </ns1:out>
</ns1:outSystemWSResponse>

I tried with below XSLT but i am not sure how I can remove, xml version="1.0" encoding="UTF-8"?>. Please help

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="ns:out" xmlns:ns="http://abcd.co.za">
    <xsl:copy>
        <xsl:value-of select="." disable-output-escaping="yes"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
2

2 Answers

0
votes

I agree with Martin Honnen that the best solution is to convert the escaped text to XML, then parse it using a proper XML parser.

If you are limited to XSLT 1.0 or 2.0, that would mean outputting only the contents of ns1:out with the escaping disabled to another file, then processing the resulting file with another stylesheet.

Still, in a pinch you could do:

<xsl:template match="ns:out" xmlns:ns="http://abcd.co.za">
    <xsl:copy>
        <xsl:value-of select="substring-after(., '?>')" disable-output-escaping="yes"/>
    </xsl:copy>
</xsl:template>
0
votes

In this question you need the counterpart of the serialize function, that is the parse-xml function, also available in XPath 3 and therefore XSLT 3:

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

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

  <xsl:template match="ns1:out" xmlns:ns1="http://abcd.co.za">
      <xsl:copy>
          <xsl:apply-templates select="parse-xml(.)"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/a9GPfX

Or check whether there is a processor specific extension to parse XML.