32

Hi i am generating a xml by appying the xsl to a xml input. I need the output without this part "<?xml version="1.0" encoding="utf-16"?>"

input--xml

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CreateResponse xmlns="http://jerseytelecom.com/">
    <CreateResult>
        <ISD_XMLGateway>
            <Entity>RIM_BPS</Entity>
         </ISD_XMLGateway>
    </CreateResult>
   </CreateResponse>
</soap:Body>
</soap:Envelope> 

my xsl

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:JT="http://jerseytelecom.com/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="JT">
         <xsl:output method="xml" indent="yes"/>
         <xsl:template match="/">
           <xsl:element name="Entity">
            <xsl:value-of select="soap:Envelope/soap:Body/JT:CreateResponse/JT:CreateResult/JT:ISD_XMLGateway/JT:Entity"/>  
            </xsl:element>
            </xsl:template>
            </xsl:stylesheet>

Current output

   <?xml version="1.0" encoding="utf-16"?>
    <Entity>RIM_BPS</Entity>

Expected Output

    <Entity>RIM_BPS</Entity>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
shaiksha
  • 993
  • 5
  • 17
  • 35
  • 2
    Why? All valid XML documents must start with an XML declaration. – SLaks Feb 10 '13 at 17:57
  • @SLaks, I'm guessing some 'erm not so compliant and to be frank poor implementation. – Tony Hopkinson Feb 10 '13 at 18:02
  • 2
    @SLaks: The XML declaration is optional in XML files however: [Tip: Always use an XML declaration](http://www.ibm.com/developerworks/xml/library/x-tipdecl/index.html) -- (it is not a *must* but a *should*) – hakre Feb 10 '13 at 18:43
  • Why is the encoding UTF-16 in your output? Omitting the XML declaration might require you to have it as UTF-8. Please share the related code so it's more clear which system you're using. – hakre Feb 10 '13 at 18:45
  • 1
    @SLaks and hakre - There must be an XML declaration if it's an XML 1.1 file. If the declaration is not included, the file can only be XML 1.0. http://www.w3.org/TR/xml11/#sec-prolog-dtd and http://www.w3.org/TR/REC-xml/#sec-prolog-dtd – Daniel Haley Feb 11 '13 at 01:56
  • @hakre. If it' optional then it's prescnec or lack of it should not affect anything that processes it. – Tony Hopkinson Feb 11 '13 at 08:40
  • 1
    Its appropriate to exclude the declarations where transforms will be assembled later into a larger XML document. – StingyJack Mar 20 '15 at 15:16
  • 1
    @SLaks One reason to want to strip the tag is if you want to output sections of a document, rather than a whole document, and combine them later. – Matthew Mar 28 '17 at 23:04
  • @Matt: Then you should use APIs that output elements rather than documents, and you won't have any problems. – SLaks Apr 06 '17 at 17:03
  • @SLaks it's a mistake to assume that the XML will be consumed by a compliant application, or that requester has any control over the constraints placed on him. – Terrible Tadpole Jun 04 '19 at 05:45

4 Answers4

44

Try adding the omit-xml-declaration="yes" attribute to your xsl:output tag.

It should then read like this:

<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
hakre
  • 193,403
  • 52
  • 435
  • 836
ChrisC
  • 2,461
  • 1
  • 16
  • 25
15

Put this in your xslt

<xsl:output method="xml" omit-xml-declaration="yes"/>

or

at an extreme push

<xsl:output method="text" />

should solve the symptom...

The last one could have significant consequences though depending on the processor.

hakre
  • 193,403
  • 52
  • 435
  • 836
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
5

Use this XSLT to remove encoding=“UTF-8” from xml Document using XSLT.In Cdaata section You can add encoding as your will. Cheers:)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:text disable-output-escaping="yes"><![CDATA[<?xml version="1.0"?>]]></xsl:text>
        <xsl:copy-of select="node()"/>
    </xsl:template>
</xsl:stylesheet>
Tim
  • 41,901
  • 18
  • 127
  • 145
2

This complete transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:JT="http://jerseytelecom.com/" exclude-result-prefixes="soap JT">
<xsl:output omit-xml-declaration="yes" indent="yes"
     encoding="utf-8"/>
 <xsl:template match="/">
  <Entity>
   <xsl:value-of select=
   "soap:Envelope/soap:Body/JT:CreateResponse
              /JT:CreateResult/JT:ISD_XMLGateway/JT:Entity"/>
  </Entity>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CreateResponse xmlns="http://jerseytelecom.com/">
    <CreateResult>
        <ISD_XMLGateway>
            <Entity>RIM_BPS</Entity>
         </ISD_XMLGateway>
    </CreateResult>
   </CreateResponse>
</soap:Body>
</soap:Envelope>

produces the wanted, correct result:

<Entity>RIM_BPS</Entity>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431