Reputation: 5
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><![CDATA[<?xml version="1.0" encoding="UTF-8"?><UploadUser>
<User>
<EmployeeID>TEST</EmployeeID>
<FirstName>TEST</FirstName>
</User>
</UploadUser>]]></data>
</ImportUsers>
I want to replace XML tag "<?xml version="1.0" encoding="UTF-8"?>" 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
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="UTF-8"?>'), ']]>')"/>
</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