Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
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 September 4th, 2003, 07:17 PM
rev rev is offline
Registered User
 
Join Date: Sep 2003
Location: , , Germany.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default putting xml data in a html <table>

i am new to XSLT and I am experiencing a quite tricky problem.
i have a xml file that is structured as shown below:

<booklist>
<book>
<title>Title of the book <subtitle>Subtitle of the book </subtitle></title>
</book></booklist>


I want to list the titles of all books in a table. Title and subtitle of the book should reside in the same cell (<td>). The title should be bold (<b>) but the subtitle should not be emphasized. That's the XSLT file for it. The problem is that the <b> tag for the title does also affect the subtitle because it is a child of it. Can anybody tell me how I have to change my XSLT? Or is not possible to create the table in the manner I want to do it?

A second question: Does it in general make sense to use a hierarchy like that

<title>Title of the book <subtitle>Subtitle of the book </subtitle></title>

in an XML file (i.e. to have elements that can contain text and child elements.) or would it be better to make these elements siblings or use attributes.


Hope I could make myself understood - otherwise just ask.
thanx for anybody who is taking the time to write a response.

cu,
thomas



THIS IS MY (NOT PROPERLY WORKING) XSL(T) FILE:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="booklist">
        <html>
            <head/>
            <body><table border="1">
                <xsl:apply-templates/>
            </table></body>
        </html>
    </xsl:template>


<xsl:template match="book">
 <tr><td> <xsl:apply-templates select="title"/></td> </tr>
</xsl:template>

<xsl:template match="title">
    <b><xsl:value-of select="."/></b> <br></br>
</xsl:template>

<xsl:template match="subtitle">
<xsl:value-of select="subtitle"/>
</xsl:template>

Reply With Quote
  #2 (permalink)  
Old September 5th, 2003, 04:03 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

To answer your 2nd question first, personally I can't see any reason to embed the subtitle inside the title - it will just cause all sorts of problems. I would change it to:
<booklist>
<book>
<title>Title of the book</title>
<subtitle>Subtitle of the book </subtitle>
</book>
</booklist>

If you make this change you will find that your XSLT is not tricky at all :)

rgds
Phil
Reply With Quote
  #3 (permalink)  
Old September 5th, 2003, 04:47 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I agree about the structure, you'd be better off having them as siblings. As to the XSLT try this:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="booklist">
        <html>
            <head/>
            <body><table border="1">
                <xsl:apply-templates select="book"/> 
            </table></body>
        </html>
    </xsl:template>

<xsl:template match="book">     
 <tr><td> <xsl:apply-templates select="title"/></td> </tr>
</xsl:template>

<xsl:template match="title">
    <b><xsl:value-of select="."/></b> <br/>
       <xsl:value-of select="subtitle"/> 
</xsl:template>

--

Joe
Reply With Quote
  #4 (permalink)  
Old September 5th, 2003, 08:24 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here's the rule of thumb that I go by. I found this in some documentation or white paper or something discussing XML structure.

When do I use a node?: When there will be more than one occurrence of a particular entity (in your case book(s)).
When do I use an attribute?: When the entity is a property of it's parent item. One exception I make is if said entity is going to be very large or some other special case (CDATA).

So you should probably structure your XML like so:
Code:
<booklist>
    <book title="Title of the book" subtitle="Subtitle of the book" />
    [n... <book />]
</booklist>
Obviously, my guidelines are only a suggestion, but regardless of that, you should always stay away from having a node which contains both inner text and child nodes.

Peter
Reply With Quote
  #5 (permalink)  
Old September 10th, 2003, 08:49 AM
rev rev is offline
Registered User
 
Join Date: Sep 2003
Location: , , Germany.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for your suggestions.
i have changed the structure of my xml. much easier to handle now.

rgds,
thomas

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
Putting and input in <STDIN> ZORCH Perl 1 June 14th, 2007 09:58 AM
How to insert <br> in a table with this xml/xsl? andigra XSLT 1 September 22nd, 2006 07:01 AM
Regular Expression to remove <table> </table> tags mathalete CSS Cascading Style Sheets 2 January 23rd, 2006 01:59 PM
putting <element> round group of same elements jefke XSLT 0 May 24th, 2004 10:18 AM
XML List -> HTML table JPMRaptor XSLT 1 November 14th, 2003 10:19 PM



All times are GMT -4. The time now is 01:33 PM.


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