469,314 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

XML to XML Transformation using XSLT

I am having an XML like below,

Expand|Select|Wrap|Line Numbers
  1. <SECT1><TITLE>Title1</TITLE><PARA>Line1<BR/>Line2<BR/>Line3<BR/>Line4<BR/>Line5</PARA></SECT1>
I want to convert this into another XML in the following format,
Expand|Select|Wrap|Line Numbers
  1. <SECT1>
  2. <TITLE>Title1</TITLE>
  3. <PARA>Line1<BR/>Line2<BR/>Line3<BR/>Line4<BR/>Line5</PARA>
  4. </SECT1>
Actually, i want to add line break at the end of each element. I tried to convert using XSL:TEXT in XSLT. But it didnt work correctly.

Please provide your suggestions for this.

Thanks for your help.
May 19 '09 #1
5 8345
Dormilich
8,651 Expert Mod 8TB
for line breaks use &#10; (the line break character), but is it really necessary to add the line breaks (although it lokks better to the human eye)?

the next problem you'll have is deciding, where to put the line break (to distinguish between element children and text children might get more work than is worth)
May 19 '09 #2
My XSLT looks like below,

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/">
  2.         <xsl:apply-templates select="*"/>
  3.     </xsl:template>
  4.     <xsl:template match="node()">
  5.         <xsl:text>
  6. </xsl:text>
  7.         <xsl:copy>
  8.             <xsl:apply-templates select="node()"/>
  9.         </xsl:copy>
  10.     </xsl:template>
Problem is it doesnt transform the xml in the required format. Is there anything wrong in the XSL?

it transforms like below,
Expand|Select|Wrap|Line Numbers
  1. <SECT1><TITLE>
  2. Title1</TITLE><PARA>
  3. Line1
  4. <BR />
  5. Line2
  6. <BR />
  7. Line3
  8. <BR />
  9. Line4
  10. <BR />
  11. Line5</PARA></SECT1>
May 19 '09 #3
Dormilich
8,651 Expert Mod 8TB
@anand18101984
no, but with the logic behind it. (as I mentioned, you need several node-type tests to determine, when to put in a break)

got this after some experimenting (actually I'm rather surprised to get it working so fast):
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="node()">
  2.        <xsl:if test="child::text()">
  3.               <xsl:text>&#10;</xsl:text>
  4.        </xsl:if>
  5.        <xsl:copy>
  6.             <xsl:apply-templates select="node()"/>
  7.         </xsl:copy>
  8.        <xsl:if test="child::text() and preceding-sibling::node()">
  9.               <xsl:text>&#10;</xsl:text>
  10.        </xsl:if>
  11. </xsl:template>
May 19 '09 #4
Thanks for your response. Unfortunately, this didnt work out for me. I have given my full XSLT and XML document below.

XSLT
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.     <xsl:output method="xml" indent="yes"/>
  4.     <xsl:template match="/">
  5.         <xsl:if test="child::text()">
  6.             <xsl:text>
  7. </xsl:text>
  8.         </xsl:if>
  9.         <xsl:apply-templates select="*"/>
  10.         <xsl:if test="child::text() and preceding-sibling::node()">
  11.             <xsl:text>
  12. </xsl:text>
  13.         </xsl:if>
  14.     </xsl:template>
  15.     <xsl:template match="node()">
  16.         <xsl:copy>
  17.             <xsl:apply-templates select="node()"/>
  18.         </xsl:copy>
  19.     </xsl:template>
  20.  
  21. </xsl:stylesheet>
xML
Expand|Select|Wrap|Line Numbers
  1. <SECT1><TITLE>Corporate Structure</TITLE><PARA> Abbott Laboratories<BR />100 Abbott Park Road<BR />Abbott Park<BR />Illinois 60064-6400 </PARA></SECT1>
May 19 '09 #5
Dormilich
8,651 Expert Mod 8TB
try putting the if-statements in the second template.
May 19 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Kevin Brown | last post: by
reply views Thread by =?Utf-8?B?UHJhc2hhbnQgQw==?= | last post: by
3 posts views Thread by WideBoy | last post: by
6 posts views Thread by kluge.wolfram | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.