0

I am using XSLT to transform a large XML into CSV and the requirement is to have the last column of CSV the actual XML data in base64 encoded string. I have been successful with transforming to CSV but stuck on the base64 encoding part. I came across this stack overflow answer to encode as base64 string. I am unable to figure out how to select the entire node and pass it as argument to the template call. Below is sample XML, XSL and CSV format.

XML

<Products>
  <Product Name="ABC" Description="Sample description" Price="10.0">
     Additional child nodes here...
  </Product>
  <Product Name="XYZ" Description="Sample description" Price="15.0">
         Additional child nodes here...
  </Product>
  <Product Name="DEF" Description="Sample description" Price="16.0">
         Additional child nodes here...
  </Product>
 </Products>

CSV

Name,Description,Price,....,EncodedXML

ABC,SampleDescription,10.0,....,Base64string
XYZ,SampleDescription,15.0,....,Base64string
DEF,SampleDescription,16.0,....,Base64string

XSL:

<xsl:template match="/">
<xsl:for-each select="//Product">
<xsl:call-template name="b64:encode">
<xsl:with-param name="asciiString" select="."/>
</xsl:call-template>
<xsl:for-each>
</xsl:template>
user320587
  • 1,347
  • 7
  • 29
  • 57
  • What exactly do you mean by "the entire node"? – michael.hor257k Oct 22 '19 at 08:18
  • The product node along with its attributes and all child nodes – user320587 Oct 22 '19 at 13:29
  • That's going to be problematic, esp. in XSLT 1.0. The problem is not the Base64 encoding, but getting the node back as a string. You will need to construct your own serializer for this - see: https://stackoverflow.com/questions/55303127/how-do-i-serialize-output-from-a-node-set-in-xslt-1-0 – michael.hor257k Oct 22 '19 at 13:53
  • @michael.hor257k Can it be done with XSLT2.0? – user320587 Oct 23 '19 at 19:00
  • It can be done in XSLT 1.0, too - It's just a lot of work. It's much easier in XPath/XSLT 3.0, with it's built-in `serialize()` function: https://www.w3.org/TR/xpath-functions-31/#func-serialize – michael.hor257k Oct 23 '19 at 19:48
  • @michael.hor257k What XPath expression should I use to get the node to pass to the serializer function? – user320587 Oct 24 '19 at 21:08
  • If you are in the context of `Product` then you would use `.` (the abbreviated syntax for the context node). – michael.hor257k Oct 24 '19 at 21:32

0 Answers0