Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 4th, 2003, 07:17 PM
rev rev is offline
Registered User
 
Join Date: Sep 2003
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>

 
Old September 5th, 2003, 04:03 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old September 5th, 2003, 04:47 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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
 
Old September 5th, 2003, 08:24 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old September 10th, 2003, 08:49 AM
rev rev is offline
Registered User
 
Join Date: Sep 2003
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






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.