2

I need to do transformation from PCDATA in XML into another XML. Can any one help me on this?

I need to map the line of business, site-location and policy number from the request to the corresponding field in response.

Input XML containing PCDATA which is escaped XML:

<ns2:Response xmlns:ns2="http://webservices.company.com/">
    <return>&lt;ibdoc&gt;&lt;result lob='15' env_def='SR_JBOSS_DEV' gen_date='2017-01-18 12:11:30 AM' ibdoc_version='3.1' engine_type='JBoss' site_location='S396AT092'&gt;
    &lt;program parent_id='1001' program_id='1' program_ver='1' package_date='2017-01-05T13:06:01' status='PASS' gen_type='0' region_format='en-US' from_cache='true'&gt;
        &lt;c i='0' d='Policy'&gt;
            &lt;m i='POLICY NUMBER' d='POLICY NUMBER' v='1000001'/&gt;
        &lt;/c&gt;
    &lt;/program&gt;
    &lt;stats&gt;
        &lt;start_time&gt;2017-01-18 12:11:30:0191 AM&lt;/start_time&gt;
        &lt;stop_time&gt;2017-01-18 12:11:30:0226 AM&lt;/stop_time&gt;
        &lt;running_time&gt;35&lt;/running_time&gt;
        &lt;xml_walking&gt;31&lt;/xml_walking&gt;
    &lt;/stats&gt;
&lt;/result&gt;&lt;/ibdoc&gt;</return>
</ns2:Response>

XSLT:

<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"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <ns2:custom-response xmlns:ns2="http://esbservices.company.com/">
        </ns2:custom-response>
    </xsl:template>
</xsl:stylesheet>

Expected Output XML:

<ns2:custom-response xmlns:ns2="http://esbservices.company.com/">
    <line-of-business>15</line-of-business>
    <site>S396AT092</site>
    <policy>
        <policy-nr>1000001</policy-nr>
    </policy>
</ns2:custom-response>
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Ravi
  • 1,247
  • 4
  • 15
  • 35
  • 1
    Which CDATA section? There's none in your Input XML – Thomas Weller Jan 18 '17 at 08:39
  • Hi Thomas,The content inside return tag is CDATA section < and > are getting converted to < and > respectively.I understand it is valid cdata section as per the link https://www.soapui.org/functional-testing/working-with-cdata.html – Ravi Jan 18 '17 at 08:42
  • 1
    IMHO that's PCDATA then, because P is for *parsed*. That text must be parsed to replace the entities. See http://stackoverflow.com/a/11624583/480982 – Thomas Weller Jan 18 '17 at 08:44
  • Can i do the replacement in same xslt as i do the transformation?? – Ravi Jan 18 '17 at 08:45
  • Sorry, I don't know. I think it's an interesting question, but you shoud [edit] it and include the XSLT that you have as a starting point. It will show what knowledge you have and what you have tried. It will also show that you understand the rules of the site, which is "we're not a free coding service". – Thomas Weller Jan 18 '17 at 08:48
  • Thanks Thomas,I agree but i just wanted to understand if i need to convert the PCDATA to CDATA and then from CDATA to XML? – Ravi Jan 18 '17 at 08:50
  • Yes, so please give others the basic XSLT which they can copy/paste as a starting point. It should at least transform ns2:Response into ns:custom-response and change the namespace. This will reduce the effort building an answer. – Thomas Weller Jan 18 '17 at 08:56
  • Thanks Thomas For your input,i had created a basic xslt to create root element,i dont know how to parse PCDATA to CDATA.I will do some research and keep you updated. – Ravi Jan 18 '17 at 09:04
  • 1
    Ok, you got my upvote now. Like this, the question seems complete. – Thomas Weller Jan 18 '17 at 09:15
  • 2
    Which XSLT processor can you use? Recent versions of Saxon 9 or of Altova support the XPath 3.0 function `parse-xml` https://www.w3.org/TR/xpath-functions-30/#func-parse-xml which allows you to parse a string with XML into a document and then you can write templates as usual to transform it. – Martin Honnen Jan 18 '17 at 10:56
  • @user2761267 this is interesting. It would be very helpful if you can name the XSLT processor you are using. – Madeyedexter Jan 18 '17 at 11:57
  • @Madeyedexter iam using Apache camel 2.10 and JDK 1.7, xslt component is used to transform the xml message using xslt. it uses default java xml parsing mechanism.http://camel.apache.org/xslt.html. – Ravi Jan 19 '17 at 07:08

1 Answers1

1

Using XSLT 3.0 and the XPath 3.0 parse-xml function (as supported in Saxon 9, Altova XMLSpy, oXygen) you can use

<xsl:template match="/*">
    <ns2:custom-response xmlns:ns2="http://esbservices.company.com/">
        <xsl:variable name="return-doc" select="parse-xml(return)"/>
        <xsl:apply-templates select="$return-doc/ibdoc/result"/>
    </ns2:custom-response>        
</xsl:template>

<xsl:template match="result">
    <line-of-business>
        <xsl:value-of select="@lob"/>
    </line-of-business>
    <site>
        <xsl:value-of select="@site_location"/>
    </site>
    <policy>
        <policy-nr>
            <xsl:value-of select=".//c[@d = 'Policy']/m[@d = 'POLICY NUMBER']/@v"/>
        </policy-nr>
    </policy>   
</xsl:template>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110