473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML to XML Transformation using XSLT

5 New Member
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 8478
Dormilich
8,658 Recognized Expert Moderator Expert
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
anand18101984
5 New Member
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,658 Recognized Expert Moderator Expert
@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
anand18101984
5 New Member
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,658 Recognized Expert Moderator Expert
try putting the if-statements in the second template.
May 19 '09 #6

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

Similar topics

1
3376
by: Igor | last post by:
Is there any way to resort and xml document using xslt based on element position. For example if I have xml like this: <root> <element> 1st thing </element> <element> 2nd thing </element> <element> 3rd thing </element> </root> would it be possible using xslt only to reverse it into:
3
2424
by: Kevin Brown | last post by:
Is there anyway to generate this type of resulting HTML table from this XML using XSLT? Basically I need to be able to consult 2 trees of data to generate the HTML, but I have not been able to figure out how to do so. There is supposed to be a way of using templates and the key function to do grouping, but can it be done with 2 different...
0
1843
by: =?Utf-8?B?UHJhc2hhbnQgQw==?= | last post by:
-- Regards, Prashant C
7
2644
by: pivote | last post by:
I'm trying to transform a XML file to another XML file. The first file must describe what I want to say in a more abstract way than the second. But in order to do the transformation, I need an intermediate XML file which helps to find that more detailed description needed for the output file. I was wondering if this could be done using XSLT...
3
2492
by: WideBoy | last post by:
Hi, I have a software generated schema which creates a few empty elements, e.g. <xsd:sequence/>. I would like to be able to delete these elements from the schema post-generation using XSLT. I've tried using the identity pattern to copy the whole schema except these rogue elements but I seem to be getting stuck on how to correctly choose...
6
4331
by: kluge.wolfram | last post by:
Hi, i get stucked on a transformation problem using XSLT. What i need is to copy an XML Tree to an output XML without any automatic changes. Since i used <xsl:copyor <xsl:copy-ofthere occur unwanted side effects. For example i just copied a xml were several namespace declarations are present more than one time. Then the transformation do...
1
4358
by: sakhawn | last post by:
hi all, I need to transform an xml file into a different format (Dublin Core schema) using xslt, file contains different records each record needs to transformed based on a unique ID (in this case its element <guid>) that represents the record. Input file is: <?xml version="1.0" encoding="UTF-8" ?> <ListRecords> <record> <header>...
18
6520
by: malathib | last post by:
Hi, while trasforming xml to html using xslt, i dont want to lose xml tags.I want to preserve it. After transforming to html and displaying in browser and if you are right clicking and viewing the source, xml tags will be there as it is but it will display teh content as html in browser. But after transforming, when i am displaying in text...
0
1684
by: Prashant M | last post by:
Hi All, I had created a XSLT which converts the XML file generated from the system to the excel format. The XSLT has the styles required for this transformation. Now i am told to insert image in one of the excel columns using XSLT. The image is in binary code format in the XML file. Tried to google but could not found the syntaxes for...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.