XSLT 2 David Massey MBIB4140 21-9-2017
Demo html.xsl mondial.xsl
<mondial> <country> <name>albania</name> <population measured="est." year="1950">1214489</population> <population measured="est." year="1960">1618829</population> <city id="cty-albania-tirane" country="al"> <name>tirana</name> <name>tirane</name> <latitude>41.33</latitude> <longitude>19.82</longitude> <elevation>110</elevation> <population year="1987">192000</population> <population year="1990" measured="estimate">244153</population> <population year="2011" measured="census">418495</population> </city> </country> </mondial>
Attributter Skriv ut navn og ID for hver storby <xsl:template match="/"> <xsl:for-each select="mondial/country/city"> <xsl:text>navn: </xsl:text><xsl:value-of select="name" /> <xsl:text> Id: </xsl:text><xsl:value-of select="@id" /> <xsl:text> </xsl:text> </xsl:for-each>
Prøv selv Opprett mondial2.xsl Koble XSLT-stilarket til mondial-europe-small.xml Legg inn reglene fra forrige lysarket Endre stilarket slik at listen sorteres alfabetisk etter storbyens navn
Attributter Legg til befolkningstallet fra 2011 <xsl:template match="/"> <xsl:for-each select="mondial/country/city"> <xsl:text>navn: </xsl:text><xsl:value-of select="name" /> <xsl:text> Id: </xsl:text><xsl:value-of select="@id" /> <xsl:text> Befolkningstall: </xsl:text><xsl:value-of select="population[@year = '2011']" /> <xsl:text> </xsl:text> </xsl:for-each>
Prøv selv Legg til oppdateringen fra forrige lysarket i mondial2.xsl Endre listen slik at den sorteres etter befolkningstall fra 2011
Templates "A rule in a XSLT stylesheet that defines how part of an XML document should be transformed." Tidwell, s. 940
Templates <xsl:template match="/"> <xsl:for-each select="mondial/country/city"> <xsl:text>navn: </xsl:text><xsl:value-of select="name" /> <xsl:text> </xsl:text> </xsl:for-each>
<xsl:apply-templates /> "Instructs the XSLT processor to apply the appropriate templates to a node-set or sequence. [...] Select (Optional attribute): Contains an XPath expression that selects nodes to which templates should be applied." Tidwell, s. 372
<xsl:apply-templates /> <xsl:template match="/"> <xsl:apply-templates /> <xsl:template match="country"> <xsl:value-of select="name" /> <xsl:template match="/"> <xsl:apply-templates select="mondial/country" /> <xsl:template match="country"> <xsl:value-of select="name" />
Prøv selv Opprett mondrial3.xsl Prøv begge metoder fra forrige lysarket
Prøv selv Hva skjer hvis du endrer mondial3.xsl til disse? Igjen, prøv begge metoder. <xsl:template match="/"> <xsl:apply-templates /> <xsl:template match= city"> <xsl:value-of select="name" /> <xsl:template match="/"> <xsl:apply-templates select="mondial/country/city" /> <xsl:template match= city"> <xsl:value-of select="name" />
Flere templates <xsl:template match="/"> <xsl:apply-templates select="mondial/country" /> <xsl:apply-templates select="mondial/country/city" /> <xsl:template match="country"> <xsl:text> </xsl:text> <xsl:template match="city"> <xsl:text> </xsl:text> <xsl:template match="/"> <xsl:apply-templates select="mondial/country" /> <xsl:template match="country"> <xsl:text> </xsl:text> <xsl:apply-templates select="city" /> <xsl:template match="city"> <xsl:text> </xsl:text>
Flere templates <xsl:template match="/"> <xsl:apply-templates select="mondial/country" /> <xsl:apply-templates select="mondial/country/city" /> <xsl:template match="country"> <xsl:text> </xsl:text> <xsl:template match="city"> <xsl:text> </xsl:text>
Flere templates <xsl:template match="/"> <xsl:apply-templates select="mondial/country" /> <xsl:template match="country"> <xsl:text> </xsl:text> <xsl:apply-templates select="city" /> <xsl:template match="city"> <xsl:text> </xsl:text>
Prøv selv Inspirert av forrige lysarket oppdater mondial3.xsl Stilarket skal skrive ut alle storbyene som tilhører et land i hver sin HTML-tabell. Se skjermdumpet til høyre Tabellene skal inneholder to kolonner navn og befolkningstall. Bruk befolkningstallet fra 2011.
Sortere etter navn på land <xsl:template match="/"> <html> <h1>storbyene</h1> <xsl:apply-templates select="mondial/country"> <xsl:sort select="name" /> </xsl:apply-templates> </html>
Prøv selv Endre mondial3.xsl slik at landene sorteres alfabetisk etter navn. Se forrige lysarket
Endre formatet på befolkningstallet <xsl:template match="city"> <tr> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="format-number(population[@year = '2011'], '###,###')" /></td> </tr> Mer info: https://www.w3schools.com/xml/func_formatnumber.asp
Prøv selv Endre mondial3.xsl slik at befolkningstallet formateres. Se forrige lysarket
Fjern tomme tabeller <xsl:template match="country"> <h2><xsl:value-of select="name" /></h2> <xsl:if test="count(city) > 0"> <table> <xsl:apply-templates select="city" /> </table> </xsl:if>
Mer XPath position() last() <xsl:for-each select="mondial/country/city"> <xsl:text>{ "type":"feature", "geometry": { "type":"point", "coordinates": [</xsl:text> <xsl:value-of select="longitude" /> <xsl:text>, </xsl:text> <xsl:value-of select="latitude" /> <xsl:text>] </xsl:text> <xsl:text>}, "properties": { "name": "</xsl:text> <xsl:value-of select="name" /> <xsl:text>" }</xsl:text> <xsl:if test="position()!= last()"> <xsl:text>,</xsl:text> </xsl:if> </xsl:for-each>
Prøv selv <xsl:template match="city"> <xsl:choose> <xsl:when test="position() mod 2 = 1"> <tr bgcolor="#ffe4c4"> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="format-number(population[@year = '2011'], '###,###')" /></td> </tr> </xsl:when> <xsl:otherwise> <tr> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="format-number(population[@year = '2011'], '###,###')" /></td> </tr> </xsl:otherwise> </xsl:choose> Endre city-template i mondial3.xsl Flere fargekoder: https://www.w3schools.com/colors/colors_names.asp
Prøv selv Oppgaven tar utgangspunkt i Deichmanske bibliotekets anbefalingstjenesten: http://anbefalinger.deichman.no/ Les mer om tjenesten her: http://digital.deichman.no/blog/2013/07/10/bokanbefalinger-er-idrift-bli-med-du-ogsa/ Jeg har hentet noen anmeldelser vha av tjenestens API: http://anbefalinger.deichman.no/api/reviews?limit=5 Resultatet er data i JSON-formatet Hent resultatet (bokanbefalinger.json) fra: http://edu.hioa.no/mbib4140/h17/ressurser/json/ Forts.
Prøv selv Gjør om JSON til XML vha av tjenesten: http://www.utilitiesonline.info/xmltojson/ Kopier resultatet inn i XML Copy Editor og lagre dokumentet som bokanbefalinger.xml Resultatet av konvertering er ikke en velformet XML-dokument. Gjør de nødvendige endringer slik at bokanbefalinger.xml er velformet Opprett bokanbefalinger.xsl som gjør om bokanbefalinger.xml til bokanbefalinger.html Her er det ingen fasit. Bruk XSLT for å presenterere dataene i en nettleser på best mulig måte for sluttbrukerne Forts.
Prøv selv Hjelp til markeringsspråket HTML finnes her: https://www.w3schools.com/html/default.asp F.eks.: oversfrifter (h1- h6), lister (ol, ul, li), avsnitt (p), tabeller (table, tr, th, td), osv. bokanbefalinger.xml inneholder nettadressen til forsidebilder i cover_url-elementet. Dette bør være en del av presentasjonen. HTML elementet for bilder er img. Les mer her: https://www.w3schools.com/html/html_images.asp <xsl:attribute> brukes for å legge til attributter til en element i XSLT. Se eksempler 2 og 3 for inspirasjon: https://www.w3schools.com/xml/ref_xsl_el_attribute.asp
Neste gang Tirsdag 26. september. Rom: P35 PI458 XML workshop XML, XSD, XPath, XQuery og XSLT