Thursday, January 13, 2011

XSLT for indentifying line feed character

A line feed character is nothing but a new line character. I had a requirement to break a text in one of fields in input xml received by a bpel process. I tried using substring-before function with checking if the value contains '&xa'. However, the soa server does not seem to recognize this.

I understood Substring wouldn't work for line feed characters. The xslt chunk in this post will remove the line feed character and extract the text. Consider the input xml:
<Emp_No>100</Emp_No>
<Emp_Name>SS</Emp_Name>
<Details>First Employee
Second Employee
Third Employee</Details>

This xml has to be transformed to:
<Details1>First Employee</Details1>
<Details2>Second Employee</Details2>
<Details3>Third Employee</Details3>

Use the XSLT below to do the transformation:
<xsl:variable name="Temp">
<xsl:value-of select="translate(/InputXml/Details, '&#xA;', '|')"/>
</xsl:variable>
<Details1>
<xsl:value-of select="substring-before($Temp,'|')"/>
</Details1>
<Details2>
<xsl:value-of select="substring-before(substring-after($Temp,'|'),'|')"/>
</Details2>
<Details3>
<xsl:value-of select="substring-after(substring-after($Temp,'|'),'|')"/>
</Details3>


Cheers,
-AR


No comments:

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License