Connecting Tech Pros Worldwide Forums | Help | Site Map

XML to XML Transformation using XSLT

Newbie
 
Join Date: May 2009
Posts: 5
#1: May 19 '09
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.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 4,300
#2: May 19 '09

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)
Newbie
 
Join Date: May 2009
Posts: 5
#3: May 19 '09

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>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 4,300
#4: May 19 '09

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>
Newbie
 
Join Date: May 2009
Posts: 5
#5: May 19 '09

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>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 4,300
#6: May 19 '09

re: XML to XML Transformation using XSLT


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