Forum Moderators: open

Message Too Old, No Replies

Search For An Author In A Book With Authors

search for and author in a book with multiple authors

         

Charlie Chan

4:06 am on Apr 3, 2005 (gmt 0)

10+ Year Member



I created a XML page about my books. Here is a part of the pattern used for it.
...................................
<data>
<item>
<title>JavaScript Unleashed</title>
<edition>Third Edition</edition>
<copyright>2000</copyright>
<author>Richard Wagner</author>
<author>R. Allen Wyke</author>
<publisher>SAMS</publisher>
<isbn>0-672-31763-X</isbn>
<lccn>99-63993</lccn>
<price>$49.99 USD</price>
<subject>JavaScript Scripting Language - Much of this is about how to embed JavaScript into HTML:
client-side, server-side, and Microsoft Active Server Pages (ASP).</subject>
</item>

<item>
<title>1001 Visual Basic Programmer's Tips</title>
<edition>First Edition</edition>
<copyright>1997</copyright>
<author>Kris Jamsa, Ph.D.</author>
<author>Lars Klander</author>
<publisher>Jamsa Press</publisher>
<isbn>1-884133-56-8</isbn>
<lccn></lccn>
<price>$54.95 USD</price>
<subject>Visual Basic 5 Programming Language - It is for the first time VB Programmer
or the expert. It covers manipulating ActiverX controls, documents,
libraries, and servers. Talks about VBScript.</subject>
</item>

<item>
<title>HTlML 4</title>
<edition>Second Edition</edition>
<copyright>1999</copyright>
<author>Deborah S. Ray</author>
<author>Eric J. Ray</author>
<publisher>SYBEX</publisher>
<isbn>0-7821-2523-9</isbn>
<lccn>99-661301</lccn>
<price>$34.99 USD</price>
<subject>HyperText Markup Language 4 - "... your one-stop comprehensive guide to HyperText
Markup Language?"</subject>
</item>

<item>
<title>Beginning Visual C++ 6</title>
<edition>Third Edition</edition>
<copyright>1998</copyright>
<author>Ivor Horton</author>
<publisher>Wrox Press Ltd.</publisher>
<isbn>1-861000-88-X</isbn>
<lccn></lccn>
<price>$49.99 USD</price>
<subject>Visual C++ 6 - from beginner level to building
an actual, though limited, working Windows program using
Microsoft Foundation Classes. It is a difficult book!</subject>
</item>

<item>
<title>Learn to Program with C++</title>
<edition>First Edition</edition>
<copyright>2002</copyright>
<author>John Smiley</author>
<publisher>M cGraw-Hill/Osborne</publisher>
<isbn>0-07-222535-1</isbn>
<lccn></lccn>
<price>$29.99 USD</price>
<subject>C++ Programming Language - This an excellent book for the true begin to start learning C++.</subject>
</item>
</data>
............................................

I can sort by subject and title. That is easy. What I can not do is sort by author when a book has multiple authors. I want to display the author as he/she is sorted then display: the title, authors, publisher, etc. Here is what I have and all it displays is the first table which diplays the sortation mode and provides links to display by subject or title.
............................................
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />

<xsl:param name="aBook" select="data/item" />
<xsl:variable name="aAuthor" select="data/item/author" />

<xsl:template match="/">
<html>
<body>
<table>
<!-- The is the Sortation Option section. -->
<tr><td align="left"> Books Sorted by Author </td><td align="left"> Sort By: </td><td><a href="My_Books_title.xml" > Title </a>
</td><td> or </td><td><a href="My_Books_main.xml"> Subject </a></td></tr>
</table>

<xsl:for-each select="data/item/author">
<xsl:for-each select="item">
<xsl:for-each select="author">
<xsl:if test="aAuthor" />
<table border="4" bordercolor="#888888" >
<xsl:apply-templates select="." />
</table>
</xsl:for-each>
</xsl:for-each>

</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template match="/item">
<xsl:for-each select="author">
<tr><td>AUTHOR/s</td><td><xsl:value-of select="." /></td></tr>
</xsl:for-each>
<tr bgcolor="beige"><td>TITLE</td><xsl:element name="td"><xsl:value-of select="title" /></xsl:element></tr>
<tr><td>EDITION</td><td><xsl:value-of select="edition" /></td></tr>
<tr><td>PUBLISHER</td><td><xsl:value-of select="publisher" /></td></tr>
<tr><td>ISBN</td><td><xsl:value-of select="isbn" /></td></tr>
<tr><td>LCCN</td><td><xsl:value-of select="lccn" /></td></tr>
<tr><td>PRICE</td><td><xsl:value-of select="price" /></td></tr>
<tr><td>SUBJECT</td><td><p><xsl:value-of select="subject" /></p></td></tr>

</xsl:template>

</xsl:stylesheet>
..................................

Would someone please help a newbie out with this problem!

johnl

9:30 am on Apr 5, 2005 (gmt 0)

10+ Year Member



hi,

though i am not sure how exactly you want your output to display, you might start with the following approach:


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="data/item/author">
<xsl:sort select="."/>
<table border="4" bordercolor="#888888" >
<tr><td>AUTHOR</td><td><xsl:value-of select="." /></td></tr>
<tr bgcolor="beige"><td>TITLE</td>
<td><xsl:value-of select="parent::item/title"/></td></tr>
<!-- fill in other properties here -->
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

of course, given your xml-data, the authors will be sorted by surname.

Charlie Chan

12:30 am on Apr 7, 2005 (gmt 0)

10+ Year Member



Thank you John. It works. Now all I need to do is develope a good understanding of how it works.

johnl

12:38 pm on Apr 8, 2005 (gmt 0)

10+ Year Member



hi again,

you might perhaps also try the following, which i wrote after considering your question a bit longer ..


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//item">
<xsl:sort select="author"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match='author'>
<table border="4" bordercolor="#888888" >
<tr><td>AUTHOR's</td><td><span style='color: red;'>
<xsl:value-of select="." /></span>
<xsl:variable name="sortme" select="."/>
<xsl:for-each select="parent::item/author">
<xsl:if test=".!= $sortme">
<xsl:text>, </xsl:text>
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</td></tr>
<tr bgcolor="beige">
<td>TITLE</td>
<td><xsl:value-of select="parent::item/title"/></td>
</tr>
<tr><td>ISBN</td>
<td><xsl:value-of select="parent::item/isbn"/></td></tr>
<!-- fill in other properties here -->
</table>
</xsl:template>

</xsl:stylesheet>

Charlie Chan

11:30 pm on Apr 8, 2005 (gmt 0)

10+ Year Member



I have worked on three approaches to displaying the sorted authors. Here are the first two:

My_Books_author.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:styTR="my_bookstyle.css">
<xsl:output method="html" indent="yes" />

<xsl:param name="aAuthor" select="data/item/author" />

<xsl:template match="/">
<html>
<body>
<table>
<!-- The is the Sortation Option section. -->
<tr><td align="left"> Books Sorted by Author </td><td align="left"> Sort By: </td><td><a href="My_Books_title.xml" > Title </a>
</td><td> or </td><td><a href="My_Books_main.xml"> Subject </a></td></tr>
</table>

<xsl:for-each select="data/item/author">
<xsl:sort select="normalize-space(author)" />
<table>
<tr bgcolor="#999920" width="30%"><td width="30%"><xsl:value-of select="author" /> <xsl:value-of select="."/> </td> </tr>
</table>
<xsl:call-template name="aTable">
<xsl:with-param name="aAuthor" />
</xsl:call-template>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="aTable" match="/item">
<table border="4" bordercolor="#888888" >
<tr bgcolor="beige"><td>TITLE</td><xsl:element name="td"><xsl:value-of select="parent::item/title" /></xsl:element></tr>
<xsl:for-each select="parent::item/author">
<tr><td>AUTHOR/s</td><td><xsl:value-of select="." /></td></tr>
</xsl:for-each>
<tr><td>EDITION</td><td><xsl:value-of select="parent::item/edition" /></td></tr>
<tr><td>PUBLISHER</td><td><xsl:value-of select="parent::item/publisher" /></td></tr>
<tr><td>ISBN</td><td><xsl:value-of select="parent::item/isbn" /></td></tr>
<tr><td>LCCN</td><td><xsl:value-of select="parent::item/lccn" /></td></tr>
<tr><td>PRICE</td><td><xsl:value-of select="parent::item/price" /></td></tr>
<tr><td>SUBJECT</td><td><p><xsl:value-of select="parent::item/subject" /></p></td></tr>
</table>
</xsl:template>
</xsl:stylesheet>

My_Books_author2.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html" indent="yes" />

<xsl:template match="/">
<html>
<link rel="stylesheet" type="text/css" href="my_bookstyle.css" />
<body>
<table>
<!-- The is the Sortation Option section. -->
<tr><td align="left"> Books Sorted by Author </td><td align="left"> Sort By: </td><td><a href="My_Books_title.xml" > Title </a>
</td><td> or </td><td><a href="My_Books_main.xml"> Subject </a></td></tr>
</table>

<xsl:for-each select="data/item/author">
<xsl:sort select="." />
<table border="4" bordercolor="#888888" >
<xsl:element name="tr"><xsl:element name="td"> AUTHOR </xsl:element>
<xsl:element name="td"><xsl:value-of select="." /></xsl:element></xsl:element>
<xsl:element name="tr"><xsl:element name="td">TITLE</xsl:element>
<xsl:element name="td"><xsl:value-of select="parent::item/title" /></xsl:element></xsl:element>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Here is the third version. It is a work in progress as I an trying to reference a css file and apply a background color to the table row for the book title. I have been playing with a namespace and with an attribute. So far, I have been unsuccessful in applying a background color to the title row using a .css file name my_bookstyle.css. Here are the two files I am trying to use:

My_Books_author3.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />

<xsl:template match="/">
<style type="text/css">
my_bookstyle.css
</style>
</xsl:template>

<xsl:template match="/">
<html>
<link rel="stylesheet" type="text/css" href="my_bookstyle.css" />
<body>
<table>
<!-- The is the Sortation Option section. -->
<tr><td align="left"> Books Sorted by Author </td><td align="left"> Sort By: </td><td><a href="My_Books_title.xml" > Title </a>
</td><td> or </td><td><a href="My_Books_main.xml"> Subject </a></td></tr>
</table>

<xsl:for-each select="data/item/author">
<xsl:sort select="normalize-space(author)" />
<table>
<tr bgcolor="#999920" width="30%"><td width="30%"><xsl:value-of select="author" /> <xsl:value-of select="."/> </td>
</tr>
</table>
<xsl:call-template name="aTable">
<xsl:with-param name="aAuthor" />
</xsl:call-template>
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template name="aTable" match="/item">
<xsl:element name="table">
<!-- this works <xsl:element name="tr"><xsl:attribute name="bgcolor">beige</xsl:attribute> -->
<xsl:element name="tr"><xsl:attribute namespace="my_bookstyle.css" name="class"><xsl:value-of select="@highlight" /></xsl:attribute>
<xsl:element name="td">TITLE</xsl:element><xsl:element name="td"><xsl:value-of select="parent::item/title" /></xsl:element></xsl:element>
<xsl:for-each select="parent::item/author">
<xsl:element name="tr"><xsl:element name="td">AUTHOR/s</xsl:element><xsl:element name="td"><xsl:value-of select="." /></xsl:element></xsl:element>
</xsl:for-each>
<xsl:element name="tr"><xsl:element name="td">EDITION</xsl:element><xsl:element name="td"><xsl:value-of select="parent::item/edition" /></xsl:element></xsl:element>
<xsl:element name="tr"><xsl:element name="td">PUBLISHER</xsl:element><xsl:element name="td"><xsl:value-of select="parent::item/publisher" /></xsl:element></xsl:element>
<xsl:element name="tr"><xsl:element name="td">ISBN</xsl:element><xsl:element name="td"><xsl:value-of select="parent::item/isbn" /></xsl:element></xsl:element>
<xsl:element name="tr"><xsl:element name="td">LCCN</xsl:element><xsl:element name="td"><xsl:value-of select="parent::item/lccn" /></xsl:element></xsl:element>
<xsl:element name="tr"><xsl:element name="td">PRICE</xsl:element><xsl:element name="td"><xsl:value-of select="parent::item/price" /></xsl:element></xsl:element>
<xsl:element name="tr"><xsl:element name="td">SUBJECT</xsl:element><xsl:element name="td"><xsl:element name="p"><xsl:value-of select="parent::item/subject" /></xsl:element></xsl:element></xsl:element>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

my_bookstyle.css
body { font-size: 12pt; font-family: Arial, Helvetica, sans-serif;}
A:link { color: #000000; text-decoration: underline;}
A:visited { color: #336699; text-decoration: underline;}
A:hover { color: #CC9900; text-decoration: underline;}
table {width: 60%; }
tr.highlight {background-color:beige;}
highlight {background-color:beige;}