title: Spilt a XML in CDATA into multiple chunks using XSL
body: <p>I have a requirement where I have to insert the SOAP Messages into a DB where the Message field character length is say 200 characters.
  I have to use XSLT to acheive this and I am getting the SoapMessage in a CDATA section.</p>
  
  <p>Below is the SOAP Message that I store in a variable within a CDATA Tag</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v4="http://www.example.com/ServiceBody/V4"&gt;
    &lt;soapenv:Header&gt;
    &lt;/soapenv:Header&gt;
    &lt;soapenv:Body&gt;
      &lt;v4:getBookDetails&gt;
        &lt;v4:Request&gt;
          &lt;catalog&gt;
            &lt;book id="bk101"&gt;
              &lt;author&gt;Gambardella, Matthew&lt;/author&gt;
              &lt;title&gt;XML Developer's Guide&lt;/title&gt;
              &lt;genre&gt;Computer&lt;/genre&gt;
              &lt;price&gt;44.95&lt;/price&gt;
              &lt;publish_date&gt;2000-10-01&lt;/publish_date&gt;
              &lt;description&gt;An in-depth look at creating applications with XML.&lt;/description&gt;
            &lt;/book&gt;
          &lt;/catalog&gt;
        &lt;/v4:Request&gt;
      &lt;/v4:getBookDetails&gt;
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>Below is the XSLT that I have written to get the message into multiple tags so that I can insert it into the DB.</p>
  
  <pre><code>&lt;xsl:stylesheet extension-element-prefixes="date str" exclude-result-prefixes="dp str date" version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:str="http://exslt.org/strings"&gt;
      &lt;xsl:output method="xml"/&gt;
      &lt;xsl:template match="/"&gt;
          &lt;xsl:variable name="cdataVariable"&gt;   
              &lt;xsl:text disable-output-escaping="yes"&gt;&amp;lt;![CDATA[&lt;/xsl:text&gt;
              &lt;xsl:copy-of select="."/&gt;
              &lt;xsl:text disable-output-escaping="yes"&gt;]]&amp;gt;&lt;/xsl:text&gt;
          &lt;/xsl:variable&gt;
          &lt;MessageInChunks&gt;
              &lt;stringLength&gt;&lt;xsl:value-of select="string-length($cdataVariable)"/&gt;&lt;/stringLength&gt;
              &lt;xsl:choose&gt;
                  &lt;xsl:when test="string-length($cdataVariable) &amp;lt; 200"&gt;
                      &lt;Chunks&gt;
                          &lt;xsl:copy-of select="$cdataVariable"/&gt;  
                      &lt;/Chunks&gt;           
                  &lt;/xsl:when&gt;
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:call-template name="splitMessageIntoChunks"&gt;
                          &lt;xsl:with-param name="messageToSplit"&gt;
                          &lt;xsl:copy-of select="$cdataVariable"/&gt;
                          &lt;/xsl:with-param&gt;
                      &lt;/xsl:call-template&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
          &lt;/MessageInChunks&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template name="splitMessageIntoChunks"&gt;
          &lt;xsl:param name="messageToSplit"/&gt;
          &lt;xsl:variable name="chunkSize" select="'200'"/&gt;
          &lt;xsl:if test="string-length($messageToSplit) &gt;0"&gt;
              &lt;Chunks&gt;
                  &lt;xsl:text disable-output-escaping="yes"&gt;&amp;lt;![CDATA[&lt;/xsl:text&gt;
                  &lt;xsl:copy-of select="substring($messageToSplit, 12, $chunkSize)"/&gt;
                  &lt;xsl:text disable-output-escaping="yes"&gt;]]&amp;gt;&lt;/xsl:text&gt;
              &lt;/Chunks&gt;
              &lt;xsl:call-template name="splitMessageIntoChunks"&gt;
                  &lt;xsl:with-param name="messageToSplit" select="substring($messageToSplit, $chunkSize+1)"/&gt;
              &lt;/xsl:call-template&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>But the output that I am getting doesnt contain the elements expect for the values as the output of the substring function is not displaying the elements.</p>
  
  <p>I need something like this.</p>
  
  <pre><code>&lt;MessageInChunks&gt;
  &lt;Chunks&gt;&amp;lt;![CDATA[&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v4="http://www.example.com/ServiceBody/V4"&gt;
    &lt;soapenv:Header&gt;
    &lt;/soapenv:Header&gt;
    &lt;soapenv:Body&gt;
      &lt;v4:getBookDet]]&amp;gt;&lt;/Chunks&gt;
  &lt;Chunks&gt;&amp;lt;![CDATA[ails&gt;
        &lt;v4:Request&gt;
          &lt;catalog&gt;
            &lt;book id="bk101"&gt;
              &lt;author&gt;Gambardella, Matthew&lt;/author&gt;
              &lt;title&gt;XML Developer's Guide&lt;/title&gt;
              &lt;genre&gt;Compu]]&amp;gt;
  &lt;/Chunks&gt;
  &lt;Chunks&gt;&amp;lt;![CDATA[ter&lt;/genre&gt;
              &lt;price&gt;44.95&lt;/price&gt;
              &lt;publish_date&gt;2000-10-01&lt;/publish_date&gt;
              &lt;description&gt;An in-depth look at creating applications with XML.&lt;/description&gt;
          ]]&amp;gt;
  &lt;/Chunks&gt;
  &lt;Chunks&gt;&amp;lt;![CDATA[  &lt;/book&gt;
          &lt;/catalog&gt;
        &lt;/v4:Request&gt;
      &lt;/v4:getBookDetails&gt;
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;]]&amp;gt;
  &lt;/Chunks&gt;
  &lt;/MessageInChunks&gt;
  </code></pre>
  
  <p>Basically what I need is to spilt a XML message into chunks of 200 characters and get it into a CDATA section using XSLT.</p>
  
  <p>Kindly help me with this issue.</p>