XML : XML to XSLT - Getting each character and converting into tags

I have the below XML as input.

  <elements>      <element>          <column23>HNC141</column23>      </element>      <element>          <column23>HNC14P3</column23>      </element>  </elements>    

and my desired output is below. first three character will be parent department after that every 1 character is need to be set as code. and in parent ref i need to concat the department with "." ... for first three character there is no parent ref

  <Department>                  <Code>HNC</Code>                  <FreeString10>HNC</FreeString10>                  <ParentRef/>              </Department>              <Department>                    <Code>1</Code>                  <FreeString10>HNC1</FreeString10>                  <ParentRef>HNC</ParentRef>              </Department>              <Department>                    <Code>4</Code>                  <FreeString10>HNC14</FreeString10>                  <ParentRef>HNC.1</ParentRef>              </Department>              <Department>                    <Code>1</Code>                  <FreeString10>HNC141</FreeString10>                  <ParentRef>HNC.1.4</ParentRef>              </Department>    <Department>                <Code>HNC</Code>              <FreeString10>HNC</FreeString10>              <ParentRef/>          </Department>          <Department>                <Code>1</Code>              <FreeString10>HNC1</FreeString10>              <ParentRef>HNC</ParentRef>          </Department>          <Department>                <Code>4</Code>              <FreeString10>HNC14</FreeString10>              <ParentRef>HNC.1</ParentRef>          </Department>          <Department>                <Code>1</Code>              <FreeString10>HNC141</FreeString10>              <ParentRef>HNC.1.4</ParentRef>          </Department>          <Department>                <Code>P</Code>              <FreeString10>HNC14P</FreeString10>              <ParentRef>HNC.1.4</ParentRef>          </Department>          <Department>                <Code>3</Code>              <FreeString10>HNC14P3</FreeString10>              <ParentRef>HNC.1.4.P</ParentRef>          </Department>    

I am using this xslt for transformation

  <?xml version="1.0" encoding="utf-8"?>  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="fn"      xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" exclude-result-prefixes="xs fn">      <xsl:output method="xml" indent="yes"/>      <xsl:template match="/">          <businessobjects>          <xsl:for-each select="elements/element">              <Department>                  <Name>                      <xsl:value-of select="column23"/>                        <xsl:call-template name="for-each-character">                                              <xsl:with-param name="data" select="column23"/>                      </xsl:call-template>                  </Name>              </Department>          </xsl:for-each>          </businessobjects>      </xsl:template>        <xsl:template name="for-each-character">                          <xsl:param name="data"/>          <xsl:if test="string-length($data) &gt; 0">              <someTags>                                              <xsl:value-of select="substring($data,1,3)"/>              </someTags>              <ParentRef><xsl:value-of select="substring($data,1,3)"/></ParentRef>              <xsl:call-template name="for-each-character">                  <xsl:with-param name="data" select="substring($data,2)"/>              </xsl:call-template>          </xsl:if>      </xsl:template>      </xsl:stylesheet>    

help to get the desire output.

No comments:

Post a Comment