|
Red Squirrel Reflections
Dave Hoover explores the psychology of software development
|
|
Fri, 14 Jan 2005Quotes Manager Project -- Day 1 An ongoing pet project blog...OK, that wasn't very fun. I grabbed the XSLT specfication and wrestled my style sheet into shape. I've got it looking exactly the way my quotes page does now. Hoo-boy. Here's the stylesheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="book_url" select="'http://amazon.com/ISBN'"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="quotes/quote">
<p>
<xsl:value-of select="text"/>
<xsl:text>--</xsl:text>
<xsl:if test="quoter">
<a href="{quoter/@url}"><xsl:value-of select="quoter"/></a> in
</xsl:if>
<xsl:choose>
<xsl:when test="source/@isbn">
<a href="{$book_url}/{source/@isbn}">
<i><xsl:value-of select="source"/></i>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="source"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>, </xsl:text>
<xsl:if test="source/@page">
p. <xsl:value-of select="source/@page"/>,
</xsl:if>
<xsl:for-each select="author">
<xsl:choose>
<xsl:when test="@url">
<a href="{@url}"><xsl:apply-templates/></a>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="position()=last()-1"> and </xsl:when>
<xsl:when test="not(position()=last())">, </xsl:when>
</xsl:choose>
</xsl:for-each>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here is the data I'm using. It has most of the quote variations that I can remember:
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="sample.xsl"?> <quotes> <quote> <text> We in the software industry are working with a more or less invisible product, yet this very invisibility only heightens our need for feedback. </text> <quoter url="http://www.geraldmweinberg.com/">Gerald Weinberg</quoter> <source isbn="0932633447" page="xiii">Project Retrospectives</source> <author url="http://www.retrospectives.com/">Norm Kerth</author> </quote> <quote> <text> By building tests <i>before</i> you implement the code, you get to try out the interface before you commit to it. </text> <source isbn="020161622X" page="192">The Pragmatic Programmer</source> <author url="http://www.pragmaticprogrammer.com/">Dave Thomas</author> <author url="http://www.pragmaticprogrammer.com/">Andy Hunt</author> </quote> <quote> <text> For everyone who exalts himself will be humbled, and he who humbles himself will be exalted. </text> <source>Luke 18:14b</source> </quote> <quote> <text> Programming is the Great Game. It consumes you, body and soul. When you're caught up in it, nothing else matters. </text> <source url="http://www.apocalypse.org/pub/u/kjc/cool/Card.on.Software.html">How Software Companies Die</source> <author url="http://www.hatrack.com/osc/about.shtml">Orson Scott Card</author> </quote> <quote> <text> It's never the size of the step that a person takes that counts, but its direction. </text> <source isbn="0393700984">Narrative Means to Therapeutic Ends</source> <author>Michael White</author> <author>David Epston</author> </quote> <quote> <text> Having a plan isn't everything, but planning is. </text> <source isbn="0201708426">Extreme Programming Installed</source> <author url="http://www.xprogramming.com/">Ron Jeffries</author> <author url="http://c2.com/cgi/wiki?AnnAnderson">Ann Anderson</author> <author url="http://c2.com/cgi/wiki?ChetHendrickson">Chet Hendrickson</author> </quote> </quotes>Time for a check in. OK, so what's next? Let's take a look at the original plan...
Reading about JAXP Transformation Packages... I learned that Java 5.0 ships with JAXP 1.3, so I don't need to download anything. OK, that was a straightforward spike.
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
public class TransformSample {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("sample.xml");
StreamSource stylesource = new StreamSource(new File("sample.xsl"));
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
Source source = new DOMSource(document);
Result result = new StreamResult(System.out);
transformer.transform(source, result);
}
}
[/projects/quotes] permanent link Quote Manager Project -- Day O The beginning of a pet project blog...I love to read. As I read, I am constantly underlining, writing in the margin and transcribing quotes onto scratch paper. When I finish a book, I read through the quotes I've collected. I choose my favorites and transcribe them into my quotes page. I've been doing this since I started learning how to develop software. Actually, I started the habit before I was a programmer, but the web page thing didn't start until after I learned HTML. My quotes page has grown pretty hefty, and I would like to develop some software to help me manage it. Last year I read Ron Jeffries' Extreme Programming Adventures in C#. I liked Ron's idea of keeping a public journal as he developed software. I am going to do something similar. "...the best way to create is by imitation, refinement and combination." --Object-Oriented Software Construction, Second Edition, Bertrand MeyerRon's "adventures" were learning a new language (C#) while developing an XML editor. I'm already learning a new language right now (C++), so I think I'll stick with one that I'm familiar with (Java). But I will try out a technology (XSL) that's new to me, though it's not new to many. Here's the plan:
So where to start, where to start... What aspect of the project do I know the least about? XSL. I'll start there. I'll start with creating a sample XML file. No, wait. Source control first. I just imported what I've written so far into Subversion. OK, now I'm ready to create my XML sample. <quotes> <quote> <text> We in the software industry are working with a more or less invisible product, yet this very invisibility only heightens our need for feedback. </text> <quoter url="http://www.geraldmweinberg.com/">Gerald Weinberg</quoter> <source isbn="0932633447" page="xiii">Project Retrospectives</source> <author url="http://www.retrospectives.com/">Norm Kerth</author> </quote> </quotes>OK, checked that in. What's next? XSL-ify the XML into something resembling the way I want my quotes page to look. I printed out an XSL tutorial from W3Schools, which should be helpful. Looks like the first thing I need to do is define an XSL style sheet... <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <xsl:for-each select="quotes/quote"> <p><xsl:value-of select="text"/></p> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>I'll start with that. I need to tell my XML document that it should be rendered with my style sheet. Looks like adding these lines to the top of my XML file should do it... <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="sample.xsl"?>Let's see what that looks like in Firefox. Looks pretty boring, but just what I expected. Jerry's quote.
I'm going to add another quote to make sure that I'm going to keep building my style sheet until it looks close enough for now. Stumbling forward, wishing I had web access right now. Looks like my battery is going to run out. I guess this stuff will need to wait until tomorrow... |