This wasn't very pretty; I was in a burning hurry to get these files online, so I did the first thing that came into my head that worked. Here's the XSLT file for the first two examples.
1 <?xml version="1.0"?> 2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 3 version="1.0"> 4 5 <xsl:output encoding="ISO8859-1" method="text"/> 6 7 <xsl:template match="text()"/> 8 9 <xsl:template match="street-list/street[2]/name[position()=1]"> 10 <xsl:for-each select="following-sibling::metro"> 11 <xsl:text>Metro: </xsl:text><xsl:value-of select="."/><xsl:text> 12 </xsl:text> 13 </xsl:for-each> 14 <xsl:text>======== 15 </xsl:text> 16 17 18 <xsl:for-each select="following::metro"> 19 <xsl:text>Metro: </xsl:text><xsl:value-of select="."/><xsl:text> 20 </xsl:text> 21 </xsl:for-each> 22 <xsl:text>======== 23 </xsl:text> 24 25 </xsl:template> 26 </xsl:stylesheet>
This tells XSLT to produce a simple text file rather than an XML or HTML file. While in this case I'm producing a text file just because I don't want to have to fool around with output of nicely-formatted HTML, this example does point out that you can use XSLT to convert an XML file to plain text suitable for emailing or importing into a word processor.
This is truly brute force. I set the context node by creating
a template that matches the first <name>
node within the second <street>
node within any
<streets>
element, of which there's only one,
since it's the root element.
Note that I use a shortcut notation to find the second
<street>
element, and use the full notation for
the <name>
.
Now that the context node is established, I do an
<xsl:for-each>
with the select
attribute set to each XPath expression I want to test. Inside
the <xsl:for-each>
I produce some simple
output that lets me know which node was selected. I use
<xsl:text>
to get control over whitespace. This
is especially important since I'm producing a text file, not an
HTML file, where the extra whitespace would be ignored.
A tool for showing XPath in action, XPath Visualizer, is available at http://www.vbxml.com/xpathvisualizer/default.asp. It is for Windows only.
A Java-based XPath visualizer is available at ftp://catcode.com/pub/xpath_visualizer_1_0_1.tar.gz.