XML to XML Transformation using XSLT 
May 19th, 2009, 11:10 AM
| | Newbie | | Join Date: May 2009
Posts: 5
| |
I am having an XML like below, - <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, - <SECT1>
-
<TITLE>Title1</TITLE>
-
<PARA>Line1<BR/>Line2<BR/>Line3<BR/>Line4<BR/>Line5</PARA>
-
</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
| 
May 19th, 2009, 11:20 AM
|  | 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 (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 19th, 2009, 11:55 AM
| | Newbie | | Join Date: May 2009
Posts: 5
| | | re: XML to XML Transformation using XSLT
My XSLT looks like below, - <xsl:template match="/">
-
<xsl:apply-templates select="*"/>
-
</xsl:template>
-
<xsl:template match="node()">
-
<xsl:text>
-
</xsl:text>
-
<xsl:copy>
-
<xsl:apply-templates select="node()"/>
-
</xsl:copy>
-
</xsl:template>
Problem is it doesnt transform the xml in the required format. Is there anything wrong in the XSL?
it transforms like below, - <SECT1><TITLE>
-
Title1</TITLE><PARA>
-
Line1
-
<BR />
-
Line2
-
<BR />
-
Line3
-
<BR />
-
Line4
-
<BR />
-
Line5</PARA></SECT1>
Last edited by Dormilich; May 19th, 2009 at 12:26 PM.
Reason: Please use [code] tags when posting code
| 
May 19th, 2009, 12:49 PM
|  | 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 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): - <xsl:template match="node()">
-
<xsl:if test="child::text()">
-
<xsl:text> </xsl:text>
-
</xsl:if>
-
<xsl:copy>
-
<xsl:apply-templates select="node()"/>
-
</xsl:copy>
-
<xsl:if test="child::text() and preceding-sibling::node()">
-
<xsl:text> </xsl:text>
-
</xsl:if>
-
</xsl:template>
| 
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 - <?xml version="1.0" encoding="UTF-8" ?>
-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-
<xsl:output method="xml" indent="yes"/>
-
<xsl:template match="/">
-
<xsl:if test="child::text()">
-
<xsl:text>
-
</xsl:text>
-
</xsl:if>
-
<xsl:apply-templates select="*"/>
-
<xsl:if test="child::text() and preceding-sibling::node()">
-
<xsl:text>
-
</xsl:text>
-
</xsl:if>
-
</xsl:template>
-
<xsl:template match="node()">
-
<xsl:copy>
-
<xsl:apply-templates select="node()"/>
-
</xsl:copy>
-
</xsl:template>
-
-
</xsl:stylesheet>
xML - <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
| 
May 19th, 2009, 02:50 PM
|  | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|