seeker
seeker

Reputation: 6991

Writing a XSL document for a given XML,cant figure out how to address attributes

I'm a newbie to XML and XSLT. Ive been trying to write a XSL. But cant figure out how to address the attributes in the xml document in my xsl.

Here's my sample xml.

  <Books>
  <Book Cover="Paper back">
<Isbn>AS-1-4652-05128-2</Isbn>
<Title>Advanced Computing Theory</Title>
<Author>
  <Name>
    <First>John</First>
    <Last>Grisham</Last>
  </Name>
  <Contact Office="str1234">
    <Phone>782-999-1212</Phone>
  </Contact>
</Author>
<Publisher>Kendall Hunt</Publisher>
<Year Edition="2">
  <Year>1980</Year>
</Year>

And here's my XSL doc:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    version="1.0">
    <xsl:template match="/">
        <html> <body>
            <h1>Books</h1>
            <table border="1">
                <tr bgcolor="yellow">
                        <td><b>ISBN </b></td>
                        <td><b>Title</b></td>
                        <td><b>Author</b></td>
                        <td><b>Publisher</b></td>
                        <td><b>Year and Edition</b></td>
                </tr>
    <xsl:for-each select="Books/Book">
            <xsl:sort select="Title" />
            <tr style="font-size: 10pt; font-family: verdana">
                <td><xsl:value-of select="Isbn"/></td>
                <td><xsl:value-of select="Title"/></td>
                <xsl:for-each select="Author">
                <td><xsl:value-of select="First"/>&nbsp;<xsl:value-of select="Last"/> &nbsp;<xsl:value-of select="Phone"/> &nbsp; <xsl:value-of select="@Office"/> </td><!-- Problem here-->
                </xsl:for-each>
                <td><xsl:value-of select="Publisher"/></td>
                <td><xsl:value-of select="Year"/>&nbsp;<xsl:value-of select="@Edition"/> </td>!-- Problem here-->
            </tr>
    </xsl:for-each>
                </table>
    </body> </html>
    </xsl:template>
</xsl:stylesheet>

And the rendered (relevant)HTML is as follows :

 <tr style="font-size: 10pt; font-family: verdana">
            <td>AS-1-4652-05128-2</td>
            <td>Advanced Computing Theory</td>
            <td></td><!--Empty-->
            <td>Kendall Hunt</td>
            <td>
               1980
               <!--Empty, No edition!-->
            </td>
         </tr>

I know Im missing a small detail, but cant quite figure out what it is!

Upvotes: 0

Views: 61

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122374

<td><xsl:value-of select="First"/>&nbsp;<xsl:value-of select="Last"/> &nbsp;<xsl:value-of select="Phone"/> &nbsp; <xsl:value-of select="@Office"/> </td>

First and Last are under the Name element, so these need to be Name/First and Name/Last respectively. Similarly, Phone and @Office are under Contact so should be Contact/Phone and Contact/@Office. Or if the same author might have more than one Contact then you probably need to consider another for-each or template.

You have a similar problem with

<td><xsl:value-of select="Year"/>&nbsp;<xsl:value-of select="@Edition"/> </td>

where Edition is an attribute of Year, so you need Year/@Edition.

Upvotes: 2

Related Questions