Topic
  • 2 replies
  • Latest Post - ‏2018-07-14T10:40:35Z by devpod
devpod
devpod
2 Posts

Pinned topic Extract XML inside CDATA

‏2018-07-12T06:27:55Z | api apic cdata connect datapower dp xml

Hi,

 

I'm receiving a soap response from a provider, and I want to extract ONLY the XML inside the CDATA tags using XSLT. How can I go about doing that?

I need it to be in XML so that I can later use badgerfish to convert it back to JSON. I can't figure out the right XSLT to extract the XML inside the CDATA tags.

 

Incoming Response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:ougWSResponse xmlns:ns2="http://ougwebcomponent.components.oug.osgi.scorex.com/">
         <data_flux>
            <item>data</item>
            <item>OK</item>
         </data_flux>
         <data_flux>
            <item>data</item>
            <item>OK</item>
         </data_flux>
         <data_flux>
            <item>data</item>
            <item>ok</item>
         </data_flux>
         <data_flux>
            <item>data</item>
            <item>ok</item>
         </data_flux>
         <data_flux>
            <item>data</item>
            <item>ok</item>
         </data_flux>
         <data_flux>
            <item>outdata</item>
            <item><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<Response>
<ResponseBody>
<Date>2077-22-12</Date>
<Name>John</Name>
<Gender>M</Gender>
</ResponseBody>
</Response>]]></item>
         </data_flux>
         <data_flux>
            <item>data</item>
            <item>OK</item>
         </data_flux>
         <data_flux>
            <item>data</item>
            <item>OK</item>
         </data_flux>
      </ns2:ougWSResponse>
   </soap:Body>
</soap:Envelope>

 

Expected Output:

<Response>
<ResponseBody>
<Date>2077-22-12</Date>
<Name>John</Name>
<Gender>M</Gender>
</ResponseBody>
</Response>

 

Thanks in advance.

Updated on 2018-07-12T06:30:22Z at 2018-07-12T06:30:22Z by devpod
  • HermannSW
    HermannSW
    8145 Posts
    ACCEPTED ANSWER

    Re: Extract XML inside CDATA

    ‏2018-07-12T08:13:08Z  

    The field contains serialized XML.

    CDATA section is gone after XML parser is done, so XSLT sees only the XML serialized string.

    You can convert to  XML with dp:parse():

    1098💻  xpath++ "dp:parse(//data_flux[item='outdata']/item[2])" devpod.xml 
    <Response>
    <ResponseBody>
    <Date>2077-22-12</Date>
    <Name>John</Name>
    <Gender>M</Gender>
    </ResponseBody>
    </Response>
    1099💻  

     

    Hermann.

    Updated on 2018-07-12T08:13:22Z at 2018-07-12T08:13:22Z by HermannSW
  • HermannSW
    HermannSW
    8145 Posts

    Re: Extract XML inside CDATA

    ‏2018-07-12T08:13:08Z  

    The field contains serialized XML.

    CDATA section is gone after XML parser is done, so XSLT sees only the XML serialized string.

    You can convert to  XML with dp:parse():

    1098💻  xpath++ "dp:parse(//data_flux[item='outdata']/item[2])" devpod.xml 
    <Response>
    <ResponseBody>
    <Date>2077-22-12</Date>
    <Name>John</Name>
    <Gender>M</Gender>
    </ResponseBody>
    </Response>
    1099💻  

     

    Hermann.

    Updated on 2018-07-12T08:13:22Z at 2018-07-12T08:13:22Z by HermannSW
  • devpod
    devpod
    2 Posts

    Re: Extract XML inside CDATA

    ‏2018-07-14T10:40:35Z  
    • HermannSW
    • ‏2018-07-12T08:13:08Z

    The field contains serialized XML.

    CDATA section is gone after XML parser is done, so XSLT sees only the XML serialized string.

    You can convert to  XML with dp:parse():

    1098💻  xpath++ "dp:parse(//data_flux[item='outdata']/item[2])" devpod.xml 
    <Response>
    <ResponseBody>
    <Date>2077-22-12</Date>
    <Name>John</Name>
    <Gender>M</Gender>
    </ResponseBody>
    </Response>
    1099💻  

     

    Hermann.

    Worked like a charm. You're the best, Hermann!