473,396 Members | 1,886 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,396 software developers and data experts.

Wrap an element if it starts with text

I have a series of <entry> elements whose entire contents I want to wrap in a <p> element.

For example, here is before:

<entry>This is a raw entry that needs a p element.</entry>

Here is after:

<entry><p>This is a raw entry that needs a p element.</p></entry>

I want to ignore <entry> elements that already have a top-level child, like this:

<entry><note>I don't want to add p to these.</note><entry>

I also want to ignore <entry> elements that just consist of a space, like this:

<entry> </entry>

With my stylesheet and XPath statement below, everything works fine. However, it doesn't handle elements like these, that also need to have their contents wrapped:

<entry>This contains a nested <note>child</note> and I need these wrapped as well.</entry>

I want to get this:

<entry><p>This contains a nested <note>child</note> and I need these wrapped as well.</p></entry>

Any pointers or suggestions would be appreciated. Thank you very much.

Rick


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:template match="node() | @*">
  4.     <xsl:copy>
  5.        <xsl:apply-templates select="@* | node()"/>
  6.     </xsl:copy>
  7.   </xsl:template>
  8.   <xsl:template match="//node()[parent::entry[not(*)][.!=' ']]">
  9.     <xsl:element name="p">
  10.       <xsl:copy>
  11.          <xsl:apply-templates select="@* | node()"/>
  12.       </xsl:copy>
  13.     </xsl:element>
  14.   </xsl:template>
  15. </xsl:stylesheet>
  16.  
Nov 22 '11 #1
0 1334

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

Similar topics

2
by: Eric Smith | last post by:
I'm writing a DTD for a simulator to save the state of the simulated machine, such as register and memory contents. In this particular application, it is not expected that the generated XML will...
7
by: Roderik | last post by:
Hi, When I use <pre> inside my <td> cells, the text won't flow to a new line and the table gets to large in width. This will appear in Firefox this way, Internet Explorer will wrap the lines,...
8
by: sehiser | last post by:
Hello, I've been reading up on xpath and I am able to access elements with it. I haven't been able to figure one thing out though. How would I use XPath to select an element where the text...
1
by: Keithb | last post by:
I have a GridView control with two item template columns, each containing a label control. One of the label controls wraps on long text strings, increasing the height of the row. The other one does...
2
by: =?Utf-8?B?S2V2aW4=?= | last post by:
I have an XML node that has both attributes and text. I am having difficulty getting the xmlreader to deal with this. Sample XML Node: <HomePhoneNumber Attribute1="Y" Attribute2="Y"...
6
by: Victor | last post by:
Hi all i have a css question about how to wrap a long text by using css. for example I have a very long text like "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" there...
2
by: prashantkisanpatil | last post by:
Hi All, I am trying to wrap a japanese text in Python, by the following code. if len(message) 54: message = message.decode("UTF8") strlist = textwrap.wrap(message,54) After this I am...
0
by: Alan Johnson | last post by:
I am trying to specify that an element may contain simple content (an integer), or complex content (a sequence of two other elements), but not both. The listMember element in the following XML...
3
by: christianlott1 | last post by:
I needed this for address labels that wouldn't wrap. Couldn't find the function so I made one. Splits at space. Function cvtWordWrap(Apl As String, Leng As Long) As String Dim i As Long If...
1
by: bushnellweb | last post by:
Is it just a simple css or is it more complicated with javascript, etc. I want to limit a description input to 500 characters. <input type="text"> makes it easy with maxlength="500" but the text...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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...
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,...

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.