350 likes | 539 Views
Oblig 2: The Candy-community. Om XML, XSLT, Sablotron og HTML. Oblig 2: Prototype. Begrenset prototype Teste teknologi: XML, XSLT, Sablotron, HTML Velge og illustrere metadata: Interessant for deres prosjekt Interessant for sosial navigasjon. Argumentere for valg og design.
E N D
Oblig 2: The Candy-community. Om XML, XSLT, Sablotron og HTML.
Oblig 2: Prototype • Begrenset prototype • Teste teknologi: • XML, XSLT, Sablotron, HTML • Velge og illustrere metadata: • Interessant for deres prosjekt • Interessant for sosial navigasjon. • Argumentere for valg og design.
Oblig 2: Grovstruktur • Definer et format og metadata. • Lag en falsk databasefil i XML. • Gjør tre utdrag fra ”databasen” • Transformer utdragene til tre HTML-filer.
The Candy Community • Fiktivt community • Godteri-relatert og sukkersøt musikk. • To typer metadata: • Candy factor • Sugar level
Candy: XML-”database” <?xmlversion="1.0" encoding="UTF-8"?> <songscommunity="candy"> <songcandyfactor="6" sugarlevel="medium"> <title>Freia gele</title> <artist>The Jelloes</artist> </song> <songcandyfactor="3" sugarlevel="high"> <title>Sugar Baby Love</title> <artist>The Rubettes</artist> </song> <songcandyfactor="2" sugarlevel="low"> <title>Honey</title> <artist>Tori Amos</artist> </song> <songcandyfactor="5" sugarlevel="low"> <title>Sweet</title> <artist>JR Ewing</artist> </song> <songcandyfactor="0" sugarlevel="low"> <title>Bittersweet symphony</title> <artist>The Verve</artist> </song> </songs>
Candy: XML-”database” <?xmlversion="1.0" encoding="UTF-8"?> <songscommunity="candy"> <songcandyfactor="6“ sugarlevel="medium"> <title>Freia gele</title> <artist>The Jelloes</artist> </song> … </songs>
Candy: XML-Prolog <?xmlversion="1.0" encoding="UTF-8"?> <songscommunity="candy"> <songcandyfactor="6“ sugarlevel="medium"> <title>Freia gele</title> <artist>The Jelloes</artist> </song> … </songs> Meta-informasjon, XML-versjon og enkoding.
Candy: Toppnivå-elementet <?xmlversion="1.0" encoding="UTF-8"?> <songscommunity="candy"> <songcandyfactor="6“ sugarlevel="medium"> <title>Freia gele</title> <artist>The Jelloes</artist> </song> … </songs> XML kan ha ett og bare ett toppnivå-element
Candy: Song-elementet <?xmlversion="1.0" encoding="UTF-8"?> <songscommunity="candy"> <songcandyfactor="6“ sugarlevel="medium"> <title>Freia gele</title> <artist>The Jelloes</artist> </song> … </songs> Flere song-elementer, metadata som attributter.
Candy:Data-elementer <?xmlversion="1.0" encoding="UTF-8"?> <songscommunity="candy"> <songcandyfactor="6“ sugarlevel="medium"> <title>Freia gele</title> <artist>The Jelloes</artist> </song> … </songs> Metadata som elementer.
XSLT • eXtensible Style sheet Language: Transformations • Forvandle XML til annen XML, HTML eller tekst. • Opererer på en logisk trestruktur. • Noder, rot, stier, akser, osv.
XPath • Språk for å angi stier og utdrag fra et XML (DOM)-tre. • Brukes i XSLT og XQuery. • Innebygde funksjoner og akser. • Meget ekspressivt.
Candy: Utdrag med XSLT • Sangens tittel • Candyfactor • Sortert • Omdøpe candyfactor • <xsl:something/> angir XSLT-instruksjoner • Alt annet er ”utskrift” til skjerm
Candy: Stilarket <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputmethod="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template> </xsl:stylesheet>
Candy: Stilarket <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputmethod="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:templatematch="/songs"> … </xsl:template> </xsl:stylesheet>
Stilarket: Prolog <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputmethod="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:templatematch="/songs"> … </xsl:template> </xsl:stylesheet>
Stilarket: Meta-info <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputmethod="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:templatematch="/songs"> … </xsl:template> </xsl:stylesheet> Informasjon om stilarket, XSLT-versjon og namespace.
Stilarket: Output-metode <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputmethod="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:templatematch="/songs"> … </xsl:template> </xsl:stylesheet> Informasjon om resultatet av transformasjonen.
Stilarket: Maler <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:outputmethod="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:templatematch="/songs"> … </xsl:template> </xsl:stylesheet> Mal for å håndtere elementer fra match.
Stilarket: Songs-malen <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template>
Stilarket: Nytt topp-element <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template> Opprette nytt toppnivå-element.
Stilarket: Iterasjon over noder <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template> Itererer over song-noder som er barn av songs.
Stilarket: Sortering av noder <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template> Sortering basert på en nøkkel, stigende eller synkende.
Stilarket: Nytt song-element <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template> Opprette flere song-elementer.
Stilarket: XPath-kortform <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template> XPath-uttrykk i {…}
Stilarket: Tekstverdi av noder <xsl:templatematch="/songs"> <songs> <xsl:for-eachselect="song"> <xsl:sortselect="@candyfactor" order="descending"/> <songcandy="{@candyfactor}"> <xsl:value-ofselect="title"/> </song> </xsl:for-each> </songs> </xsl:template> Hente verdien av alle tekstnoder av valgte node.
Sablotron • XSLT-prosessor for kommandolinjen • Linux: /store/opt/sablotron/bin/sabcmd • sabcmd <stilark> <xml-fil> <output-fil> • sabcmd lagxml.xsl candy.xml candy2.xml
Candy: Utdraget <?xmlversion="1.0" encoding="UTF-8"?> <songs> <songcandy="6">Freia gele</song> <songcandy="5">Sweet</song> <songcandy="3">Sugar Baby Love</song> <songcandy="2">Honey</song> <songcandy="0">Bittersweet symphony</song> </songs>
Bruk av XSLT i eksemplet • <xsl:stylesheet version=”[versjon av XSLT]” xmlns:xsl:=”[URL til namespace]”/> • <xsl:template select=”[XPath-uttrykk]”/> • <xsl:for-each select=”[XPath-uttrykk]”/> • <xsl:sort select=”[XPath-uttrykk]” order=”[stigende/økende]”/> • <xsl:value-of select=”[XPath-uttrykk]”/> • Les mer i spesifikasjonen
Noen muligheter i XSLT/XPath • Flere maler. • Navngitte maler. • Import/include. • Variabler og parametere. • Parse eksterne dokumenter. • Avanserte XPath-uttrykk. • If-else/for each/select case. • +++
Ressurser • http://www.w3.org/TR/xslt • http://www.w3.org/TR/xpath • http://www.w3.org/TR/REC-xml/ • http://zvon.org/xxl/XSLTreference/Output/index.html