Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing xml declaration (<?xml version="1.0" encoding="UTF-8"?>) using XSLT 1.0 inside Cdata

I am getting the response from the SharePoint application like this

Input

 <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <SharepointResponse xmlns="http://test.com.services.generic">
            <Sharepoint_Response>&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   &lt;CopyIntoItemsResult>0&lt;/CopyIntoItemsResult>
   &lt;Results>
      &lt;CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/"/>
   &lt;/Results>
&lt;/CopyIntoItemsResponse></Sharepoint_Response>
        </SharepointResponse>
    </Body>
</Envelope>

I am using this code

Code :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
    <!--Identity template simply copies content forward -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
     <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="*" />
            <xsl:value-of select="text()" disable-output-escaping="yes"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Gives output:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <SharepointResponse xmlns="http://test.com.services.generic">
            <Sharepoint_Response><?xml version="1.0" encoding="UTF-8"?>
<CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <CopyIntoItemsResult>0</CopyIntoItemsResult>
   <Results>
      <CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/enterprise/"/>
   </Results>
</CopyIntoItemsResponse></Sharepoint_Response>
            </SharepointResponse>
        </Body>
    </Envelope>

I am not sure how to remove this <?xml version="1.0" encoding="UTF-8"?>after <Sharepoint_Response>

Expected the output:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <SharepointResponse xmlns="http://test.com.services.generic">
            <Sharepoint_Response>
<CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <CopyIntoItemsResult>0</CopyIntoItemsResult>
   <Results>
      <CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/enterprise/"/>
   </Results>
</CopyIntoItemsResponse></Sharepoint_Response>
            </SharepointResponse>
        </Body>
    </Envelope>
like image 806
Kumar_y Avatar asked Aug 19 '26 19:08

Kumar_y


1 Answers

You could try something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes" encoding="utf-8" />
  <!--Identity template simply copies content forward -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="*" />
      <xsl:value-of select="substring-after(text(),'?&gt;')" disable-output-escaping="yes" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The idea is to use substring-after to get everything after that closing ?> part of the XML declaration. Using that stylesheet gave me the following output:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <Body>
    <SharepointResponse xmlns="http://test.com.services.generic">
      <Sharepoint_Response>
<CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <CopyIntoItemsResult>0</CopyIntoItemsResult>
   <Results>
      <CopyResult ErrorCode="Success" DestinationUrl="http://archivelink.dev.test.com/"/>
   </Results>
</CopyIntoItemsResponse></Sharepoint_Response></SharepointResponse></Body></Envelope>
like image 73
Dan Field Avatar answered Aug 22 '26 07:08

Dan Field



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!