Connecting Tech Pros Worldwide Help | Site Map

XML to XML Transformation using XSLT

  #1  
Old May 19th, 2009, 11:10 AM
Newbie
 
Join Date: May 2009
Posts: 5
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.

Last edited by Dormilich; May 19th, 2009 at 11:15 AM. Reason: Please use [code] tags when posting code
  #2  
Old May 19th, 2009, 11:20 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: XML to XML Transformation using XSLT


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)
  #3  
Old May 19th, 2009, 11:55 AM
Newbie
 
Join Date: May 2009
Posts: 5

re: XML to XML Transformation using XSLT


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>

Last edited by Dormilich; May 19th, 2009 at 12:26 PM. Reason: Please use [code] tags when posting code
  #4  
Old May 19th, 2009, 12:49 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: XML to XML Transformation using XSLT


Quote:
Originally Posted by anand18101984 View Post
Problem is it doesnt transform the xml in the required format. Is there anything wrong in the XSL?
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>
  #5  
Old May 19th, 2009, 02:37 PM
Newbie
 
Join Date: May 2009
Posts: 5

re: XML to XML Transformation using XSLT


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>

Last edited by Dormilich; May 19th, 2009 at 02:47 PM. Reason: Please use [code] tags when posting code
  #6  
Old May 19th, 2009, 02:50 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: XML to XML Transformation using XSLT


try putting the if-statements in the second template.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to preserve XML tags after xml to html transformation using xslt malathib answers 18 March 12th, 2009 04:03 PM
XML-to-XML XSLT transformation using an intermediate file. pivote answers 7 September 20th, 2007 04:57 AM
XSLT XML-HTML Transformation using grouping Kevin Brown answers 3 July 20th, 2005 08:36 AM
XML to XML Transformation Will answers 8 July 20th, 2005 07:28 AM