Reputation: 27
i have a source xml as below
<rest-adapter-response>
<metadata>
<status>success</status>
</metadata>
<status-line>
<code>200</code>
<reason>OK</reason>
</status-line>
<header-lines>
<Cache-Control>private, max-age=0</Cache-Control>
<Transfer-Encoding>chunked</Transfer-Encoding>
<Content-Type>application/octet-stream</Content-Type>
<Expires>Thu, 25 Apr 2019 08:51:55 GMT</Expires>
<Last-Modified>Fri, 10 May 2019 08:51:55 GMT</Last-Modified>
<Server>Microsoft-IIS/10.0</Server>
<X-SharePointHealthScore>1</X-SharePointHealthScore>
<X-SP-SERVERSTATE>ReadOnly=0</X-SP-SERVERSTATE>
<DATASERVICEVERSION>3.0</DATASERVICEVERSION>
<X-Download-Options>noopen</X-Download-Options>
<Content-Disposition>attachment</Content-Disposition>
<SPClientServiceRequestDuration>224</SPClientServiceRequestDuration>
<X-AspNet-Version>4.0.30319</X-AspNet-Version>
<SPRequestGuid>de31db9e-70cb-8000-7fba-6c3e85d9c810</SPRequestGuid>
<request-id>de31db9e-70cb-8000-7fba-6c3e85d9c810</request-id>
<MS-CV>ntsx3stwAIB/umw+hdnIEA.0</MS-CV>
<Strict-Transport-Security>max-age=31536000</Strict-Transport-Security>
<X-FRAME-OPTIONS>SAMEORIGIN</X-FRAME-OPTIONS>
<X-Powered-By>ASP.NET</X-Powered-By>
<MicrosoftSharePointTeamServices>16.0.0.8824</MicrosoftSharePointTeamServices>
<X-Content-Type-Options>nosniff</X-Content-Type-Options>
<X-MS-InvokeApp>1; RequireReadOnly</X-MS-InvokeApp>
<P3P>CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"</P3P>
<Date>Fri, 10 May 2019 08:51:55 GMT</Date>
</header-lines>
<message-body>
<non-xml-data-response>COA,COA Acct Desc,Acct Prefix,Revaluation Acct,Mapping Changes - Additions( A ) Deletions ( D ) Changes ( C ),MJE,OIM Recon,Comments for difference:10000274,"Citibank, Operating, USD, 31165975",1000,10009999,A,,X,10000374,"Citibank, Clearing, USD, 31165975",1000,10009999,A,,X,10006604,"HSBC, Operating, SAR, SA0345000000003179660002",1000,10009999,A,,X,10006605,"Citibank, Operating, ZAR, 0202099009",1000,10009999,A,,X,123,,,456,,,,</non-xml-data-response>
</message-body>
</rest-adapter-response>
above XML is the response of a share point web service which tried to read a csv file and gave a response like this. as you can see in the above response xml , csv data did come but inside one xmltag called <message-body>
, and also lost the new line after every row format!!
now i need to recreate the csv !!. and the worst part is , in the tool where i receive this format i have capability to write xslt and xml ! no hosting language code or libraries could be used. also only xslt 1.0.
there is a question like this question on creating csv from xml , but this is bit different from my requirement . am just learning xslt and xpath , can any one help me in this ?
below is the requested output : click here to view the csv format
Upvotes: 0
Views: 136
Reputation: 116959
If one makes the assumption that the header row is terminated by a colon and that there are 7 values in each data row*, then it's possible to use the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/rest-adapter-response">
<xsl:variable name="csv" select="message-body/non-xml-data-response" />
<!-- header -->
<xsl:value-of select="substring-before($csv, ':')" />
<xsl:text>: </xsl:text>
<!-- data -->
<xsl:call-template name="restore-csv">
<xsl:with-param name="text" select="substring-after($csv, ':')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="restore-csv">
<xsl:param name="text"/>
<xsl:param name="i" select="1"/>
<xsl:choose>
<xsl:when test="contains($text, ',')">
<xsl:variable name="value">
<xsl:choose>
<xsl:when test="starts-with($text, '"')">
<xsl:text>"</xsl:text>
<xsl:value-of select="substring-before(substring-after($text, '"'), '"')"/>
<xsl:text>"</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($text, ',')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- output -->
<xsl:value-of select="$value"/>
<xsl:choose>
<xsl:when test="$i mod 7 = 0">
<xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>,</xsl:text>
</xsl:otherwise>
</xsl:choose>
<!-- recursive call -->
<xsl:call-template name="restore-csv">
<xsl:with-param name="text">
<xsl:choose>
<xsl:when test="starts-with($text, '"')">
<xsl:value-of select="substring-after(substring-after($text, '"'), '",')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after($text, ',')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, the result will be:
Result
COA,COA Acct Desc,Acct Prefix,Revaluation Acct,Mapping Changes - Additions( A ) Deletions ( D ) Changes ( C ),MJE,OIM Recon,Comments for difference:
10000274,"Citibank, Operating, USD, 31165975",1000,10009999,A,,X
10000374,"Citibank, Clearing, USD, 31165975",1000,10009999,A,,X
10006604,"HSBC, Operating, SAR, SA0345000000003179660002",1000,10009999,A,,X
10006605,"Citibank, Operating, ZAR, 0202099009",1000,10009999,A,,X
123,,,456,,,
This may need more work to handle possible escaped double-quotes within quoted values.
--
(*) The strange thing here is that there are 8 values in the header row, but only 7 in the data rows.
Upvotes: 1