Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 28th, 2008, 07:54 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I wrap a <p> around text that contains <i>?

I have some source text that is a child of the <body> tag and which has an <i> tag in it. I want to put a <p> around this text but XSLT puts the <p></p> around the text before the <i></i> and then the <p></p> after the <i></i>, so it is making the <i> a child of the <body> instead of a child of the <p>?
Any help with this would be appreciated.

Reply With Quote
  #2 (permalink)  
Old February 28th, 2008, 08:07 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT does whatever you ask it to. Your code is wrong, and if you show us your code then we can tell you where it is wrong.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old February 28th, 2008, 08:25 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, I'm sure my approach is too simplistic but I'm keen to discover more.

My XSLT is below. What I want to do is to copy all source to the output tree but put <p> elements around text that are children of the <body>:

<xsl:template match="/">
   <xsl:apply-templates select="descendant::td[@id='contentTD']" />
</xsl:template>

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()" disable-output-escaping="yes"/>
   </xsl:copy>
</xsl:template>

<xsl:template match="i">
   <em><xsl:apply-templates /></em>
</xsl:template>


<xsl:template match="td/text()">
      <p><xsl:value-of select="." /></p>
</xsl:template>

A sample of the source document is:

<body>
this is a sentence that I <i>really</i> want to become HTML
</body>

and I want it to look like:

<body>
<p>this is a sentence that I <em>really</em> want to become HTML</p>
</body>

but instead I am getting:

<body>
<p>this is a sentence that I</p>
<em>really</em>
<p>want to become HTML</p>
</body>

Thanks for your help

Reply With Quote
  #4 (permalink)  
Old February 29th, 2008, 03:49 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Your xslt keeps mentioning 'td' - I see no 'td' elements in your xml, therefore I'm going to assume you're not actually showing us what your trying to do properly. We're not mind readers.

Given your example xml, this might work:

<xsl:template match="body">
<p>
  <xsl:apply-templates/>
</p>
</xsl:template>

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old February 29th, 2008, 05:36 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you want a <p> around the whole content of the body, do as Sam suggests. If you want something else, we'll need a more detailed spec of your requirements.

And get rid of that disable-output-escaping="yes". You don't need it, and it will cause the stylesheet to produce incorrect output if your data contains special characters that need to be escaped. This facility is for experts only - it's very dangerous in the hands of a beginner (and experts very rarely use it).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old March 5th, 2008, 12:30 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thankyou for your pointers and I appreciate the reponses. Here is a working version of my XSLT and what I am trying to achieve.

I have a collection of badly written HTML that consists of text without surrounding <p></p> elements. I want to put the <p></p> element around each of the paragraphs of text. What I am unable to do is cater for a sentence of text that also contains an <i></i> element surrounding a word of text.

The source file has the following five lines:

<td>
<br/>This is the first sentence
<br/>This is a sentence with some <i>italicized</i> text content
<br/>This is some final text
</td>

I want the output to be:

<td>
<br/>
<p>This is the first sentence</p>
<br/>
<p>This is a sentence with some <em>italicized</em> text content</p>
<br/>
<p>This is some final text</p>
</td>

but instead I get:

<td>
  <br/>
  <p>This is the first sentence</p>
  <br/>
  <p>This is a sentence with some </p>
  <em>italicized</em>
  <p> text content</p>
  <br/>
  <p>This is some final text</p>
</td>

when I apply the following XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
   <xsl:strip-space elements="*" />

   <xsl:template match="/">
         <xsl:apply-templates />
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="i">
      <em><xsl:apply-templates /></em>
   </xsl:template>

   <xsl:template match="td/text()">
      <p><xsl:value-of select="." /></p>
   </xsl:template>
</xsl:stylesheet>

Reply With Quote
  #7 (permalink)  
Old March 5th, 2008, 04:52 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try:

<xsl:for-each-group select="*" group-ending-with="br">
  <p><xsl:copy-of select="current-group()"/></p>
  <br/>
</xsl:for-each-group>

That's XSLT 2.0 syntax, of course. In XSLT 1.0 this involves sibling recursion, which is rather more difficult, but still quite possible.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 8: <asp:image> inside <a> & ext.CSS (pg. 274) epc BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 July 12th, 2008 04:37 AM
create <ul> with alternating class on <li> element Brian Campbell XSLT 2 November 3rd, 2006 06:07 PM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM
<STRONG> vs <B> and <EM> vs <I> anshul HTML Code Clinic 12 September 1st, 2004 05:22 PM



All times are GMT -4. The time now is 02:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.