Search code examples
xsltxslt-1.0xslt-2.0

change XML declaration to single quote


I have below incoming xml format and for this input XML I just need to add soap envelope.

Input xml

<?xml version="1.0" encoding="UTF-8"?>
<p:msg
    xmlns:p="http://.xml.invoice.com">
    <access>
        <NAME>ABC</NAME>
    </access>
    <requestBody>
        <content>test message</content>
    </requestBody>
</p:msg>

with below simple xslt code i am able to add soap envelope but this declaration needs to be in single quote

XSLT code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <!-- Template to match the root of the XML document -->
    <xsl:template match="/">
        <!-- Start of SOAP Envelope -->
        <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:p="http://.xml.invoice.com">
            <soapenv:Header/>
            <!-- SOAP Body -->
            <soapenv:Body>
                <!-- Copy the original XML payload into the Body -->
                <xsl:copy-of select="/*"/>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

could you pleas help me if there is any way to change xml declaration from single quote to double quote.

Expected output (xml version and encoding should be in single quote.)

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:p="http://.xml.invoice.com">
    <soapenv:Header/>
    <soapenv:Body>
        <p:msg>
            <access>
                <NAME>ABC</NAME>
            </access>
            <requestBody>
                <content>test message</content>
            </requestBody>
        </p:msg>
    </soapenv:Body>
</soapenv:Envelope>

Solution

  • This shouldn't be necessary but if you must have it, you could use a hack like this:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/">
        <xsl:text disable-output-escaping="yes">&lt;?xml version='1.0' encoding='UTF-8'?>
    </xsl:text>
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:p="http://.xml.invoice.com">
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:copy-of select="*"/>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
        
    </xsl:stylesheet>
    

    This is assuming your processor supports disable-output-escaping.