Torna alla pagina di Tecnologie Web
:: Laboratorio 3 ::
Consideriamo il seguente file xml checkbook.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <checkbook> <deposit type="direct-deposit"> <payor>Bob's Bolts</payor> <amount>987.32</amount> <date>21-6-2009</date> <description category="income">Paycheck</description> </deposit> <payment type="check" number="980"> <payee>Kimora's Sports Equipment</payee> <amount>132.77</amount> <date>23-6-2009</date> <description category="entertainment">kendo equipment</description> </payment> <payment type="atm"> <amount>40.00</amount> <date>24-6-2009</date> <description category="cash">pocket money</description> </payment> <payment type="debit"> <payee>Lone Star Cafe</payee> <amount>36.86</amount> <date>26-6-2009</date> <description category="food">lunch with Greg</description> </payment> <payment type="check" number="981"> <payee>Wild Oats Market</payee> <amount>47.28</amount> <date>29-6-2009</date> <description category="food">groceries</description> </payment> <payment type="debit"> <payee>Barnes and Noble</payee> <amount>58.79</amount> <date>30-6-2009</date> <description category="work">O'Reilly Books</description> </payment> <payment type="check" number="982"> <payee>Old Man Ferguson</payee> <amount>800.00</amount> <date>31-6-2009</date> <description category="misc">a 3-legged antique credenza that once belonged to Alfred Hitchcock</description> </payment> </checkbook>
Creiamo un file xsl per formattare l’output del file xml. Prima di tutto, creiamo un template che imposti la struttura della pagina HTML.
<xsl:template match="checkbook"> <html> <head/> <body> <!-- page content goes here --> </body> </html> </xsl:template>
Colleghiamo il file xml al file checkbook.xsl con l’istruzione.
<?xml-stylesheet type="text/xsl" href="checkbook.xsl"?>
Aggiungiamo una sezione che riassuma le entrate (income).
<xsl:template match="checkbook"> <html> <head/> <body> <!-- income information --> <h3> <xsl:text>Income from </xsl:text> <xsl:value-of select="child::*[1]/date"/> <xsl:text> until </xsl:text> <xsl:value-of select="child::*[last()]/date"/> <xsl:text>:</xsl:text> </h3> <xsl:apply-templates select="deposit"/> </body> </html> </xsl:template>
Aggiungiamo una sezione per descrivere le uscite dal conto, ordinando le transazioni avvenute in ordine decrescente rispetto al valore amount.
<xsl:template match="checkbook"> <html> <head/> <body> <!-- income information --> <h3> <xsl:text>Income from </xsl:text> <xsl:value-of select="child::*[1]/date"/> <xsl:text> until </xsl:text> <xsl:value-of select="child::*[last()]/date"/> <xsl:text>:</xsl:text> </h3> <xsl:apply-templates select="deposit"/> <!-- payment information --> <h3> <xsl:text>Expenditures from </xsl:text> <xsl:value-of select="child::*[1]/date"/> <xsl:text> until </xsl:text> <xsl:value-of select="child::*[last()]/date"/> <xsl:text>, ranked from highest to lowest:</xsl:text> </h3> <xsl:apply-templates select="payment"> <xsl:sort data-type="number" order="descending" select="amount"/> </xsl:apply-templates> </body> </html> </xsl:template>
Infine, visualizziamo il bilancio del conto. Sono necessari due termini sum(): uno per i pagamenti totali e uno per le entrate totali. Per chiarire se l’utente è in debito o credito, coloriamo il risultato e visualizziamo un warning se negativo.
<xsl:template match="checkbook"> <html> <head/> <body> <!-- income information --> <h3> <xsl:text>Income from </xsl:text> <xsl:value-of select="child::*[1]/date"/> <xsl:text> until </xsl:text> <xsl:value-of select="child::*[last()]/date"/> <xsl:text>:</xsl:text> </h3> <xsl:apply-templates select="deposit"/> <!-- payment information --> <h3> <xsl:text>Expenditures from </xsl:text> <xsl:value-of select="child::*[1]/date"/> <xsl:text> until </xsl:text> <xsl:value-of select="child::*[last()]/date"/> <xsl:text>, ranked from highest to lowest:</xsl:text> </h3> <xsl:apply-templates select="payment"> <xsl:sort data-type="number" order="descending" select="amount"/> </xsl:apply-templates> <h3>Balance</h3> <p> <xsl:text>Your balance as of </xsl:text> <xsl:value-of select="child::*[last()]/date"/> <xsl:text> is </xsl:text> <tt><b> <xsl:choose> <xsl:when test="sum( payment/amount ) > sum( deposit/amount )"> <font color="red"> <xsl:text>$</xsl:text> <xsl:value-of select="sum( deposit/amount ) - sum( payment/amount )"/> </font> </xsl:when> <xsl:otherwise> <font color="blue"> <xsl:text>$</xsl:text> <xsl:value-of select="sum( deposit/amount ) - sum( payment/amount )"/> </font> </xsl:otherwise> </xsl:choose> </b></tt> </p> <xsl:if test="sum( payment/amount )> sum( deposit/amount )"> <p> <font color="red"> <xsl:text>DANGER! Deposit money quick!</xsl:text> </font> </p> </xsl:if> </body> </html> </xsl:template>
Verificare come viene visualizzato adesso il contenuto del file xml. Modificare il file xml in modo che non ci sia debito per vedere come viene visualizzato il risultato.
A questo punto, occorre aggiungere delle regole per gestire i pagamenti (payment) e i depositi (deposit).
Attenzione! Notare che nel documento xml, il pagamento con type=”atm” non ha la descrizione del payee. Dunque per questo caso serve una regola speciale, leggermente diversa da quella per tutti gli altri pagamenti, che potrebbe essere la seguente:
<xsl:template match="payment[@type='atm']"> <p> <xsl:value-of select="position()"/> <xsl:text>. On </xsl:text> <xsl:value-of select="date"/> <xsl:text>, you withdrew </xsl:text> <tt><b> <xsl:text>$</xsl:text> <xsl:value-of select="amount"/> </b></tt> <xsl:text> from an ATM for </xsl:text> <xsl:value-of select="description"/> <xsl:text>.</xsl:text> </p> </xsl:template>
Completare il file xsl aggiungendo i template per payment e deposit, in modo che l’output finale sia come quello mostrato in figura.
SOLUZIONE
Le due parti mancanti che devono trattare payment e deposit saranno:
PAYMENT:
<xsl:template match="deposit"> <p> <xsl:value-of select="position()"/> <xsl:text>. On </xsl:text> <xsl:value-of select="date"/> <xsl:text>,</xsl:text> <tt> <b> <xsl:text>$</xsl:text> <xsl:value-of select="amount"/> </b> </tt> <xsl:text>was deposited into your account by</xsl:text> <i> <xsl:value-of select="payor"/> </i> <xsl:text>.</xsl:text> </p> </xsl:template>
DEPOSIT:
<xsl:template match="payment"> <p> <xsl:value-of select="position()"/> <xsl:text>. On </xsl:text> <xsl:value-of select="date"/> <xsl:text>, you paid </xsl:text> <tt> <b> <xsl:text>$</xsl:text> <xsl:value-of select="amount"/> </b> </tt> <xsl:text> to </xsl:text> <i> <xsl:value-of select="payee"/> </i> <xsl:text>for </xsl:text> <xsl:value-of select="description"/> </p> </xsl:template>
Considerate il seguente codice:
<?xml version="1.0"?> <agenzia> <viaggio codice="0001" tipo="sano"> <tappa> <luogo>Moena</luogo> <giorni>1</giorni> </tappa> <tappa> <luogo>Merano</luogo> <giorni>3</giorni> </tappa> <tappa> <luogo>Bolzano</luogo> <giorni>3</giorni> </tappa> <tappa> <luogo>Moena</luogo> <giorni>1</giorni> </tappa> <durata>8</durata> <costo>1000</costo> <descrizione>Monti, Arrampicate e Passeggiate</descrizione> </viaggio> <viaggio codice="0002" tipo="avventuroso"> <tappa> <luogo>Harlem</luogo> <giorni>1</giorni> </tappa> <tappa> <luogo>Caracas</luogo> <giorni>1</giorni> </tappa> <tappa> <luogo>Sing Sing</luogo> <giorni>2</giorni> </tappa> <durata>4</durata> <costo>321</costo> <descrizione>Rischio e Pericolo</descrizione> </viaggio> <viaggio codice="0003" tipo="oriente"> <tappa> <luogo>Filippine</luogo> <giorni>4</giorni> </tappa> <tappa> <luogo>Indonesia</luogo> <giorni>2</giorni> </tappa> <tappa> <luogo>Sing Sing</luogo> <giorni>4</giorni> </tappa> <durata>4</durata> <costo>1254</costo> <descrizione>Bellezze orientali</descrizione> </viaggio> <viaggio codice="0004" tipo="culturale"> <tappa> <luogo>Parma</luogo> <giorni>2</giorni> </tappa> <tappa> <luogo>Cesena</luogo> <giorni>1</giorni> </tappa> <tappa> <luogo>Bertinoro</luogo> <giorni>2</giorni> </tappa> <tappa> <luogo>Cento</luogo> <giorni>2</giorni> </tappa> <durata>7</durata> <costo>3412</costo> <descrizione>Gastronomia nostrana</descrizione> </viaggio> <prenotazione codice="0004" > <cliente> <cognome>Ghini</cognome> <nome>Vittorio</nome> </cliente> <data>11</data> </prenotazione> <prenotazione codice="0001" > <cliente> <cognome>Ghini</cognome> <nome>Vittorio</nome> </cliente> <data>19</data> </prenotazione> <prenotazione codice="0002" > <cliente> <cognome>Sicuro</cognome> <nome>Laura</nome> </cliente> <data>5</data> </prenotazione> <prenotazione codice="0003" > <cliente> <cognome>Clinton</cognome> <nome>Bill</nome> </cliente> <data>6</data> </prenotazione> <prenotazione codice="0003" > <cliente> <cognome>Flowers</cognome> <nome>Jennifer</nome> </cliente> <data>6</data> </prenotazione> <prenotazione codice="0003" > <cliente> <cognome>Jackson</cognome> <nome>Michael</nome> </cliente> <data>6</data> </prenotazione> </agenzia>
Scrivere delle espressioni Xpath per trovare:
SOLUZIONE
[@
tipo="avventuroso"]
Considerate il seguente pezzo di codice.
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Eseguire una XQuery per:
SOLUZIONE