Leo
Leo

Reputation: 5122

Transform XML document to base64 using XSLT 1

I need to transform an XML document into base64 using an XSLT 1 transformation.

I have tried to do this using this template: https://github.com/ilyakharlamov/xslt_base64

Here is my XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:b64="https://github.com/ilyakharlamov/xslt_base64"
  version="1.0">

  <xsl:output method="text" encoding="utf-8" />
  <xsl:strip-space elements="*"/>
  <xsl:include href="base64.xsl"/>
  
  <xsl:template match="/">
  <xsl:call-template name="b64:encode" >
            <xsl:with-param name="asciiString">
              <xsl:copy-of select="." />
            </xsl:with-param>
        </xsl:call-template>
   </xsl:template>

</xsl:stylesheet>

This works but it strips all the XML tags and only encodes the text. I need to actually encode everything (the whole document, as is). Changing the output method to XML does not help.

Upvotes: 0

Views: 1359

Answers (1)

Leo
Leo

Reputation: 5122

Based on Martin Honnen's comments, here is how I did this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:b64="https://github.com/ilyakharlamov/xslt_base64"
  version="1.0">
<xsl:import href="xml-to-string.xsl"/>
  <xsl:output method="text" encoding="utf-8" />
  <xsl:strip-space elements="*"/>
  <xsl:include href="base64.xsl"/>
  
  
  <xsl:template match="/">
  <xsl:call-template name="b64:encode" >
            <xsl:with-param name="asciiString">
              <xsl:apply-templates select="." mode="xml-to-string" />
            </xsl:with-param>
        </xsl:call-template>
   </xsl:template>

</xsl:stylesheet>

using this: http://lenzconsulting.com/xml-to-string/

Upvotes: 1

Related Questions