Doottu Biju
Doottu Biju

Reputation: 5

Remove XML declaration and replace all LT & GT with < and > inside CDATA using XSLT

Below is my input XML

<?xml version="1.0" encoding="UTF-8"?>
<ImportUsers>
  <username>User_Test</username>
  <password>Pass_Test</password>
  <guid>Guid_Test</guid>
  <data>&lt;![CDATA[&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;UploadUser&gt;
                    &lt;User&gt;
                        &lt;EmployeeID&gt;TEST&lt;/EmployeeID&gt;
                        &lt;FirstName&gt;TEST&lt;/FirstName&gt;                      
                    &lt;/User&gt;
          

&lt;/UploadUser&gt;]]&gt;</data>
</ImportUsers>

I want to replace XML tag "<?xml version="1.0" encoding="UTF-8"?&gt" inside CDATA and also replace all lt & GT in the xml with < & >.

I want to achieve this with XSLT version 1.0

Expected output

<?xml version="1.0" encoding="UTF-8"?>
<ImportUsers>
  <username>User_Test</username>
  <password>Pass_Test</password>
  <guid>Guid_Test</guid>
  <data><![CDATA[
          <UploadUser>
                    <User>
                        <EmployeeID>TEST</EmployeeID>
                        <FirstName>TEST</FirstName>                   
                         </User>
            </UploadUser>
            ]]></data>
</ImportUsers>

I tried below XSLT but the XML declaration is not replacec

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" />
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="data">
            <xsl:copy>
            <xsl:value-of select="text()" disable-output-escaping="yes" />
            </xsl:copy>
        </xsl:template>
        
</xsl:stylesheet>

Upvotes: 0

Views: 305

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

Your input is a fine mess. Try perhaps something like:

XSLT 1.0

<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" cdata-section-elements="data"/>
<xsl:strip-space elements="*"/>

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

 <xsl:template match="data">
    <xsl:copy>
        <xsl:value-of select="substring-before(substring-after(., 'encoding=&quot;UTF-8&quot;?>'), ']]&gt;')"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This extracts the substring between the end of the (escaped) XML declaration and the (escaped) closing of the CDATA section, and places it inside a valid CDATA section.

Upvotes: 1

Related Questions