[thelist] XSL / XPath
Hassan Schroeder
hassan at webtuitive.com
Tue Nov 4 10:19:27 CST 2003
Drew Shiel wrote:
> <headline>
> <furtherdetails>
> <event id="248914" eventtypeid="94" betlive="0"
> bettingtype="Outright" numrunners="20">2003 World Cup - Outright</event>
> <runner id="847157" handicap="0" currentprice="1.25"
> fractionprice="5/4 ">New Zealand</runner>
> <runner id="847158" handicap="0" currentprice="1.5"
> fractionprice="3/2 ">England</runner>
> <runner id="847159" handicap="0" currentprice="5"
> fractionprice="5/1 ">Australia</runner>
> <runner id="847160" handicap="0" currentprice="12"
> fractionprice="12/1 ">France</runner>
> </furtherdetails>
> </headline>
>
> I'm trying to work out the XSL/XPath stuff to say "Take the first two
> runner nodes and do THIS with them", where THIS is dropping some of the
> values into a HTML table.
Saving your sample data as `wtf.xml`, here's a quick JSP/JSTL snippet
to illustrate. This static approach is quick'n'dirty and obviously
doesn't give you the flexibility to, say, grab the top /three/ by just
changing a variable. That said,
<c:set var="doc"><c:import url="wtf.xml"/></c:set> <!-- load the data -->
<x:parse xml="${doc}" var="xml"/> <!-- parse the data -->
<table rules="all"> <!-- output the data -->
<tr>
<td><x:out select="$xml/headline/furtherdetails/runner[1]/text()"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[1]/@id"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[1]/@handicap"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[1]/@currentprice"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[1]/@fractionprice"/></td>
</tr>
<tr>
<td><x:out select="$xml/headline/furtherdetails/runner[2]/text()"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[2]/@id"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[2]/@handicap"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[2]/@currentprice"/></td>
<td><x:out select="$xml/headline/furtherdetails/runner[2]/@fractionprice"/></td>
</table>
<c:set>,<c:import>,<x:parse>,<x:out> are JSP taglibs markup; you'll
have to adapt the XPath statements to your language of choice.
HTH!
--
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design === (+1) 408-938-0567 === http://webtuitive.com
dream. code.
More information about the thelist
mailing list