1

We have an input XML message that contains an encoded string in an element. The requirement is that we want to create an output XML message based on the length of that string. The length of the expected output element is limited to 10 characters and there is no limit on the number of times that element can repeat.

Example:

For example, in the below message you can see that we have an element EncodedString which contains a string with the length of 100 characters. And in the output, we want to have the EncodedStringValue element 10 times because of the maximum limit of 10 characters per element declaration. Please note that in the actual scenario the limit of characters we can have may vary from 10.

Input XML

<?xml version="1.0" encoding="UTF-8"?>
      <Message xmlns="http://ws.apache.org/ns/synapse">
         <MessageID>365</MessageID>
         <EncodedString>PHJvb3Q6SW52b2ljZSB4bWxuczpyb290PSJ1cm46b2FzaXM6bmFtZXM6c3BlY2lmaWNhdGlvbjp1Ymw6c2NoZW1hOnhzZDpJbnZv</EncodedString>
    </Message>

Expected Output XML

<?xml version="1.0" encoding="UTF-8"?>
  <Message xmlns="http://ws.apache.org/ns/synapse">
     <MessageID>365</MessageID>
     <EncodedStringValue>PHJvb3Q6SW</EncodedStringValue>
     <EncodedStringValue>52b2ljZSB4</EncodedStringValue> 
     <EncodedStringValue>bWxuczpyb2</EncodedStringValue>
     <EncodedStringValue>90PSJ1cm46</EncodedStringValue>
     <EncodedStringValue>b2FzaXM6bm</EncodedStringValue>
     <EncodedStringValue>FtZXM6c3Bl</EncodedStringValue>
     <EncodedStringValue>Y2lmaWNhdG</EncodedStringValue>
     <EncodedStringValue>lvbjp1Ymw6</EncodedStringValue>
     <EncodedStringValue>c2NoZW1hOn</EncodedStringValue>
     <EncodedStringValue>hzZDpJbnZv</EncodedStringValue>
</Message>

Question?

Is it possible to achieve the above-mentioned requirement by using XSLT? IF yes then how?

2 Answers 2

2

In XSLT 2.0 same can perform as below:

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://ws.apache.org/ns/synapse"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <Message>
            <xsl:apply-templates select="node()"/>
        </Message>
    </xsl:template>
    <xsl:template match="MessageID">
        <xsl:copy-of select="."></xsl:copy-of>
    </xsl:template>
    <xsl:template match="EncodedString">
        <xsl:variable name="rep">
            <xsl:value-of select="replace(.,'([0-9A-z]{1,10})','$1 ')"/>
        </xsl:variable>
        <xsl:for-each select="tokenize($rep,' ')[position()!=last()]">
            <EncodedStringValue>
                <xsl:value-of select="."/>
            </EncodedStringValue>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/gVhDDyA/4

Sign up to request clarification or add additional context in comments.

Comments

2

Split up the data with string functions, for instance in XSLT with analyze-string:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xpath-default-namespace="http://ws.apache.org/ns/synapse"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">

  <xsl:output indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="EncodedString">
      <xsl:apply-templates select="analyze-string(., '.{1,10}')/*:match">
          <xsl:with-param name="name" select="node-name()"/>
      </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="*:match">
      <xsl:param name="name"/>
      <xsl:element name="{$name}" namespace="{namespace-uri-from-QName($name)}">{.}</xsl:element>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/6pS26ms

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.