aspose file tools*
The moose likes XML and Related Technologies and the fly likes Newbie problem with XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Newbie problem with XSLT" Watch "Newbie problem with XSLT" New topic
Author

Newbie problem with XSLT

Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
Hey all,

First off, as stated in the subject line, I'm a newbie to XML and XSLT.

Background:

folder aptly named "images" containing thumbnails of book cover art
bookcatalog.xml - list of each book I own with various info about them
bookcatalog.xsl - rules for dynamically generating the page using .xml file

Here's what I'm trying to do:

I'm trying to generate a dynamic XHTML page—with tabular data—based on two other files ('bookcatalog.xml' and 'bookcatalog.xsl'). I can get it to display the information, but it still needs some fine tuning. I'm hoping that someone can help or point me in the right direction.

Here are the problems I'm having:

  • When more then one author exists it won't display any author only blank bullets.
  • When more than one genre exists it won't display any genre only blank bullets.
  • I can't get it to display the book cover images.
  • The table cells don't expand/collapse to fit content they stay fixed width and wrap instead.
  • The table itself stays at a fixed width of 100% of browser window.


  • Here's the template I used to make the .xml file so you can get the general idea of the format of the .xml file:



    And here's what I got for the .xsl file:



    Any suggestions? What can I do to fix it? Are the fixed-widths normal for the cells and table? Can you not change the width attributes?

    TIA,

    Brian
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    Most of your questions are because you missed the basic process for using XSLT to generate HTML, namely:

    1. Decide what HTML you want to generate.
    2. Write XSLT code to generate it.

    You seem to have a number of complaints about step 2, but I think that's because you haven't really done step 1 properly. And it doesn't help somebody trying to answer you that you have posted neither the HTML you want to see, nor the HTML you actually do see.
    Brian LaRue
    Ranch Hand

    Joined: Feb 01, 2006
    Posts: 45
    Isn't the HTML that I want to generate contained in the .xsl file that I posted? I mentioned that I want tabular data (a.k.a a table) didn't I?

    Another weird thing has happened, when I view the .xml file on my local machine, it works fine (i.e. it generates XHTML). However, when you view it while posted online it just displays the raw .xml/.xsl data, not the generated XHTML. In order to show you what HTML I see, I tried uploading the .xml and .xsl files to the net so I could post a hyperlink for your viewing pleasure.

    Anyways, here's what HTML I'm trying to generate:



    I hope I've explained myself well enough this time.

    TIA again,

    Brian
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    No, the HTML you want to produce is not contained in that XSL file. The XSL file is a program for producing HTML. You seem to be confused by the fact that when you view the XML in certain browsers, they will take notice of the processing instruction you have there (<?xml-stylesheet ... ?>) and actually apply the stylesheet, so they display the generated HTML. That's just a feature of browsers.

    Anyway, look at this:An xsl:for-each element sets the context node to what it's selecting, in this case an author element in your source document. Then your xsl:value-of element looks for children of that author element that can be found via "author/first_name" etc. There's aren't any because your author element does not have any author elements as children. You want this instead:Now, if you want the author's name to not be in a UL-LI list if there's only one author, you're going to need an if-statement. The xsl:if element doesn't have an "else" clause so you need an xsl:choose element. Something like this:Hopefully that will get you going in the right direction.
    [ March 31, 2006: Message edited by: Paul Clapham ]
    Brian LaRue
    Ranch Hand

    Joined: Feb 01, 2006
    Posts: 45
    Ok, I understand that an .xsl file is instructrions for how you want to generate (a.k.a parse?) the .xml data into an (X)HTML page right? As is a .css file instructions for how you want HTML to format your page.

    Why is it that I can view the .xml file locally and it recognizes the .xsl stylesheet and when it's posted online it will not even if they're in the same directory?

    Thanks for the tip(s) on the author part of it. Now I understand that it was looking for "author/author/first_name". I got it fixed and decided to go without the bulleted list afterall. I went this direction:



    However, when I apply that same logic for my genre category I don't get the same result. Is it because Author has child elements and Genre doesn't?

    Here's what I tried for genre:



    Freakin Stupid 'Smilies'!
    [ March 31, 2006: Message edited by: Brian LaRue ]
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    Originally posted by Brian LaRue:
    Why is it that I can view the .xml file locally and it recognizes the .xsl stylesheet and when it's posted online it will not even if they're in the same directory?
    What do you mean, they are in the same directory? When you posted the XML here, you made it so the URL to access that XML, and some other stuff, was something like http://www.javaranch.com But you didn't arrange it so that your XSL was in the same directory relative to that URL. You couldn't. And even if you could, that URL doesn't point exclusively to your XML, so my browser wouldn't do anything special for it.
    However, when I apply that same logic for my genre category I don't get the same result. Is it because Author has child elements and Genre doesn't?

    Here's what I tried for genre:



    Freakin Stupid 'Smilies'!
    Yes, that's right. Your code there is looking for a "genre" child of the "genre" element, and again there isn't one. When your context node is a "genre" element, you want to output that node itself, not any child of it. Like this:(And thanks for the smilie warning, I had to edit them out the last time too.)
    Brian LaRue
    Ranch Hand

    Joined: Feb 01, 2006
    Posts: 45
    What I meant when I stated that they were in the same directory, I was referring to the same directory on the webserver.

    I was trying to upload both the .xml and .xsl files to the web and then post a hyperlink to the file(s) so you could see exactly what I was. This, I thought, would help you better understand what I was trying to fix.

    I was thinking that the .xml file would be smart enough to begin looking for the .xsl file in the file's same directory on the webserver. I guess I was wrong. I even updated the link to the .xml file to include the full URL of the .xsl file. Hmm..puzzling.

    Thanks for your help on the genre issue. I thought I was on the right track with it. Are there any good websites that have a tutorial for this xml and xslt stuff? I've already gone through the tutorials on w3school and that's how I got this far. Any other recommendations?

    I've also noticed that the   (non-breaking space) is a bit larger then the html  . It almost looks like two spaces instead of only one. Is there a way to fix the amount of space it jumps over? Some of the author's names look like this: "Eric Meyer". Maybe this has something to do with that author not having a middle initial?

    With your help, I have also fixed the author portion and the publish date section. Now, if no middle initial exists it won't print an extra space and comma. Also if the publish date doesn't contain a day of the month it won't print a blank space and comma as well.

    Here's what I did:

    Author:


    and

    Publish Date:


    Now how do I get it to display the cover image in the table's cell? All my images are contained in a folder called 'images' stored on my hard drive in the same directory as the .xml and .xsl files. As shown here:

    c:/book catalog/bookcatalog.xml
    c:/book catalog/bookcatalog.xsl
    c:/book catalog/images/whateverimage.jpg

    Here's the code in the .xsl file to access the image, I'm guessing I have a XPath problem somewhere.



    And in the .xml file it's this:



    Thank you for all your help up to this point. you've been very helpful, informative, and patient. Thanks again..
    [ April 01, 2006: Message edited by: Brian LaRue ]
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    Now you're back to asking questions about HTML again.

    Non-breaking space bigger than space? I don't see you generating any non-breaking space characters so I don't understand the question. But you're probably generating a whole lot of white space. (Normally this doesn't matter.) Have you looked at the generated HTML with a text editor?

    And how do you get it (the HTML) to display the cover image? You figure out what you have to put in your <img src="..."> to make it display the image. Then you write your XSLT so it generates that. (Normally the directory containing the image is irrelevant here, since the eventual user of the HTML is going to be accessing the image via your web server. You need the URL of the image relative to the XML.) Umm... I just noticed that you aren't generating an <img src="..."> element for your image, which would be at least part of the reason you aren't seeing the image.
    Brian LaRue
    Ranch Hand

    Joined: Feb 01, 2006
    Posts: 45
    If I'm not generating any nonbreaking spaces then what's the  ? Is that somethign else? Sorry I thought it would be in my code, when it posted the UBB chops them off and won't display them. Anyway there's one after the Authors first_name and middle_initial. There's also one after the publish date month and day. It almost looks like two spaces instead of one.

    I'm familiar with HTML and the image tags are correct. I'm thinking it's something wrong with my .xsl. Any suggestions? That was my question in the first place, how do you write the XSLT to generate the cover image??? You don't do a to do so?
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    Anyway there's one after the Authors first_name and middle_initial.
    You never posted an example of your source XML that contained those things. And you haven't ever posted the HTML which that XSLT produces, except for a single character that looks like a blank in my browser. So what can I say? I have no idea what your input is, so I have no idea what your output is either.
    I'm familiar with HTML and the image tags are correct.
    They are? But you haven't posted the HTML. Oh wait, you meant the image tags in the source XML document are correct. Now I get it. <xsl:value-of> gets you all the text children of the context node concatenated together. If you want a copy of the context node then <xsl:copy> or <xsl:copy-of> is what you want. Check the XSLT reference at the w3schools site you've been using to decide which one.
    Brian LaRue
    Ranch Hand

    Joined: Feb 01, 2006
    Posts: 45
    Here's what I see so far:

    http://home.earthlink.net/~frijolie/bookcatalog.xml

    It's strange, IE will find and use the XSLT page which will then generate the HTML. However Firefox will only display the raw .xml. Why the difference in implementations on the browsers? Is it because they use different parsers? I have made some changes to the .xsl file and need to upload it later on tonight so you can see the updates. Still some problems with it but not beyond fixing.

    Brian
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    Looks like you have made a lot of progress. My guess as to why Firefox doesn't apply the stylesheet: you used an absolute URL for its href so maybe it thinks there's some kind of cross-site scripting security exposure there. Try it with a relative URL.
     
    wood burning stoves
     
    subject: Newbie problem with XSLT