473,412 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Using XSL to reformat XML by removing nested DIV elements but retaining text

2
Hi all,

I have an XML file which contains a load of nested divs. I'm trying to write some XSL to create a new XML file with all the excess divs removed, but I want to retain the content and place this in paragraph tags.

Here is a snippet of the code:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Content>
  3.     <div>
  4.         <div>this is some content</div>
  5.         <div>
  6.             <div>
  7.                 <div>this is some more content</div>
  8.                 <div>
  9.                     <div>
  10.                         <div>and some more and more and more</div>
  11.                         <div>
  12.                             <div>
  13.                                 <div>more more more more</div>
  14.                             </div>
  15.                         </div>
  16.                     </div>
  17.                 </div>
  18.             </div>
  19.         </div>
  20.     </div>
  21. </Content>
The desired output is:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Content>
  3.     <p>this is some content</p>
  4.     <p>this is some more content</p>
  5.     <p>and some more and more and more</p>
  6.     <p>more more more more</p>
  7. </Content>

Sounds easy, but I don't what is the best way to handle this, could I use a for-each to iterate through the elements, or write a recursive template to handle the divs?

Any help much appreciated.
May 16 '07 #1
3 2835
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN...

Good question, I would try for-each loop, which is easier to handle for me.
(I never used recursive templates, but it may work).
If need more help please post some XSL code to test on.
We don't provide full source code (it's against site rules), just suggestions and code snippets.
May 16 '07 #2
xscape
2
Hi dorinbogdan,

Thank you for the warm welcome!
I think I have solved the problem - as you so rightly suggested, I used a for-each loop:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  3.     <xsl:output method="xml" indent="yes"/>
  4.  
  5.  
  6.     <xsl:template match="/">
  7.         <xsl:element name="Content">
  8.             <xsl:for-each select=".//div">
  9.                 <xsl:if test="count(child::div) = 0">
  10.                     <xsl:call-template name="handle_div">
  11.                         <xsl:with-param name="div_content" select="."/>
  12.                     </xsl:call-template>
  13.                 </xsl:if>
  14.             </xsl:for-each>
  15.         </xsl:element>
  16.     </xsl:template>
  17.  
  18.     <xsl:template name="handle_div">
  19.         <xsl:param name="div_content" />
  20.             <xsl:element name="p">
  21.                 <xsl:value-of select="$div_content"/>
  22.             </xsl:element>
  23.     </xsl:template>
  24.  
  25. </xsl:stylesheet>
The above XSL seems to work, though can you see any problems with this code?

Many thanks.
May 16 '07 #3
dorinbogdan
839 Expert 512MB
Nice code.
If it meets the requirements I have nothing to say, it look compact and well-formed.

God bless you,
Dorin.
May 17 '07 #4

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

Similar topics

7
by: Jukka K. Korpela | last post by:
It occurred to me that it seems to be fairly easy to make visually strong elements turn into normal elements or disappear. The need for such effects arises when we wish to do everything possible to...
10
by: Julian Smith | last post by:
I've been playing with a function that creates an anonymous function by compiling a string parameter, and it seems to work pretty well: def fn( text): exec 'def foo' + text.strip() return foo ...
2
by: kevin_Eld | last post by:
I have the following xml: <message> <envelope> <body key="" value="" /> </envelope> </message> I have associated an xsd to this xml in order to use the XmlDataDocument and give me the
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
2
by: pk | last post by:
Let me start off by mentioning that I'm not a web developer by any means. I welcome any and all tips regarding code cleanliness and would love to hear about any conventions that I'm breaking. I...
1
by: false-name | last post by:
is this a known IE bug/quirk ? a not-very-complex web page with several <div> ( some of those <div> nested ) with navigation by menu links down the left and near the top using CSS a:link and...
1
by: asilverpeach | last post by:
Hey Guys! Found some great scripts here on this topic but have to make to changes to the code that I can't seem to figure out. First, In the following code clicking on the headers shows the...
4
by: Efi Merdler | last post by:
Hi, I have an xml file with the following structure: <Dictionary> .... <Nested> <Entry> .... </Entry> <Entry> ....
5
by: Chelong | last post by:
hey,the follow is the text file content ========================================apple====pear== one Lily 7 0 0 7 7 two Lily 20 20 6.6666 20 8 one Lily 0 10 2.85 4 0 two Lily 22 22 7.33326 2 5 ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.