1 / 34

XSLT

XSLT. XSLT (Extensible Stylesheet Language Transformations).

lindsay
Download Presentation

XSLT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XSLT

  2. XSLT (Extensible Stylesheet Language Transformations) XSLT je opisni jezik baziran na XML-u, koji služi za transformaciju XML dokumenata u druge XML dokumente ili dokumente drugih formata koji se mogu prikazati u web browser-u.XML dokument je moguće konvertovati u sledeće formate: - HTML - XHTML - PDF - Plain Text - . . .

  3. XSLT je najbitniji deo XSL-a (eXtensible Stylesheet Language).XSLT je razvijan od strane World Wide WebConsotium-a (W3C).Postojeći standardi XSLT-a: - 1.0 - 2.0Svi noviji web pretraživači imaju u sebiugrađen XSLT.

  4. XSLT se sastoji od: - Izvornog XML dokumenta (XML izvorno stablo) - XSLT stylesheet dokumenta - XSLT procesora - Rezultujućeg dokumentaXSLT procesor, koji je implementiran u web browseru, vrši transformaciju izvornog XML dokumenta pomoću XSLT stylesheet dokumenta, dobijajući na izlazu novi dokument.

  5. Pomoću XSLT-a možemo: - dodavati nove elemente u izlazni dokument - ukloniti elemente iz izlaznog dokumenta - premeštati elemente u dokumentu - sortirati elemente u dokumentu - birati koje ćemo elemente prikazati, a koje sakriti - vršiti određene transformacije, formatiranje...

  6. XSLT koristi XPath (jezik putanja), koji služi za navigaciju kroz XML dokument.XSLT posmatra elemente ulaznog XML dokumenta kao skup čvorova.XSLT stylesheet dokument sadrži određeni broj šablona (templates).Prilikom transformacije elementa, XSLT pomoću XPath-a prolazi kroz ulazni dokument i svaki put kada naiđe na čvor pokušava da ga uklopi u odgovarajući šablon.

  7. Struktura XSLT dokumenta XSLT definiše 37 elemenata, koji su svrstani u 3 kategorije: - 2 root elementa - 12 top-level elemenata - 23 instruction elemenata Root elementi su xsl:stylesheet i xsl:transform. Ova dva elementa su sinonimi. XSLT dokument počinje root elementom. <xsl:stylesheet version=“1.0”> <!-- sadržaj dokumenta --> </xsl:stylesheet>

  8. XSLT Namespace Svi standardni XSLT elementi se nalaze u http://www.w3.org/1999/XSL/Transform namespace-u. Namespace je poseban tip XML atributa koji jednoznačno određuje neki XML element. <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <!-- sadržaj dokumenta --> </xsl:stylesheet>

  9. Izvorni XML dokument - primer <?xml version="1.0"?> <music> <cd> <album>Appetite for Destruction</album> <artist>Guns N' Roses</artist> <year>1987</year> <country>USA</country> </cd> <cd> <album>The Razors Edge</album> <artist>AC/DC</artist> <year>1990</year> <country>Australia</country> </cd> <cd> <album>Ten</album> <artist>Pearl Jam</artist> <year>1991</year> <country>USA</country> </cd> <cd> <album>Urban Hymns</album> <artist>The Verve</artist> <year>1997</year> <country>England</country> </cd> </music>

  10. Druge dve kategorije XSLT elemenata su top-level elementi i instruction elementi.Top-levelelementi su elementi koji su direktni potomci (child nodes) root elementa.Ovoj grupi pripadaju elementi za opis šablona, importovanje drugih stylesheetova u dokument, opis skupova atributa...Instruction elementi sadrže instrukcije pomoću kojih dobijamo rezultujuće stablo. Sintaksa ovih elemenata je sledeća:<xsl:Ime_elementa atribut1 = “dozvoljena vrednost atributa” atribut2 = “dozvoljena vrednost atributa” ><!-- Sadržaj --></xsl:Ime_elementa>

  11. <xsl:template> element-Top-level element - Najvažniji XSLT element - Koristi se za kreiranje šablonaElement sadrži match atribut koji sadrži obrazac (pattern) po kome se čvorovi upoređuju prilikom obilaska. Ukoliko obrazac odgovara čvoru, šablon(tj. sadržaj template elementa) se instancira i ubacuje se u rezultujuće stablo.Sintaksa:<xsl:template match=“obrazac”><!-- Sadržaj šablona --></xsl:template>

  12. Vrednost match atributa tj. obrazac zapravo prestavlja izraz napisan u Xpath-u.Primeri:match = “/” - definiše ceo XML dokumentmatch = “ime_čvora” - definiše sve direktne potomke (decu) datog čvoramatch = “/čvor1/čvor2” - definiše apsolutnu putanju nekog čvora (u ovom slučaju čvor2 koji je dete čvora1)match = “//ime_čvora” - definiše čvor sa zadatim imenom bez obzira gde se nalazi u dokumentumatch = “/čvor1//čvor2” - definiše čvor2 bez obzira gde se nalazi u dokumentu, a koji je potomak čvora1

  13. <xsl:output> element - Top-level element - Određuje tip tj. tačan format izlaznog dokumenta - Atributom method se određuje tip izlaznog dokumenta (xml, html, text,...) - Postoji još mnogo atributa za naprednija podešavanja <xsl:value-of> element - Instruction element - Uzima vrednost XML elementa i dodaje ga u rezultujuće stablo - Poseduje atribut select koji kao vrednost ima XPath izraz, slično match atributu kod <xsl:template> elementa Primer: select = “/čvor1/čvor2/čvor3” - apsolutna putanja do nekog čvora select = “.” - trenutni čvor

  14. Primer: Kada se naiđe na element “artist” primeniće se dole definisan šablon. <xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xml:output method="html" /><xsl:template match="artist" >, <b>Izvodjac: </b> <xsl:value-of select="." />,</xsl:template></xsl:stylesheet>Rezultat:Appetite for Destruction , Izvodjac: Guns N' Roses, 1987 USA The Razors Edge , Izvodjac: AC/DC, 1990 Australia Ten , Izvodjac: Pearl Jam, 1991 USA Urban Hymns , Izvodjac: The Verve, 1997 England asasasdasd

  15. Primer sređenog dokumenta: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="artist" > <b>Artist: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="album"> <b>Album: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="year"> <b>Year: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="country"> <b>Country: </b> <xsl:value-of select="." />.<p/> </xsl:template> </xsl:stylesheet>

  16. Rezultat: Album: Appetite for Destruction,Artist: Guns N' Roses,Year: 1987,Country: USA. Album: The Razors Edge,Artist: AC/DC,Year: 1990,Country: Australia. Album: Ten,Artist: Pearl Jam,Year: 1991,Country: USA. Album: Urban Hymns,Artist: The Verve,Year: 1997,Country: England. asasasdasd

  17. <xsl:apply-templates> element - Instruction element - Primenjuje šablon na trenutni element ili na direktne potomke trenutnog elementa - Ako element sadrži select atribut, onda se obrađuje samo direktni potomak koji odgovara vrednosti atributa - Moguće je dodati više select atributa Primer: Želimo da dodamo neki naslov listi iz prethodnog rezultata, npr. Music collection. Ovo ćemo uraditi dodavanjem šablona elementu music.

  18. XSLT procesor će obraditi samo prvi čvor po hijerarhiji, odnosno čvor koji je predak ostalim elementima (bez obzira na njegovu poziciju u kodu) i neće obraditi ostale elemente. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="music"> <h2>Music collection</h2> </xsl:template> <xsl:template match="artist" > <b>Artist: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="album"> <b>Album: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="year"> <b>Year: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="country"> <b>Country: </b> <xsl:value-of select="." />.<p/> </xsl:template> </xsl:stylesheet> Rezultat ovakvog koda je samo jedan red: Music collection

  19. <xsl:apply-templates> element “kaže” XSLT procesoru da ne obradi kompletan dokument i prikaže ga na izlazu, već da nastavi da obilazi XML dokument i da primenjuje odgovarajuće šablone. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="music"> <h2>Music collection</h2> <xsl:apply-templates /> </xsl:template> <xsl:template match="artist" > <b>Artist: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="album"> <b>Album: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="year"> <b>Year: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="country"> <b>Country: </b> <xsl:value-of select="." />.<p/> </xsl:template> </xsl:stylesheet>

  20. Rezultat prethodnog primera: Music collection Album: Appetite for Destruction,Artist: Guns N' Roses,Year: 1987,Country: USA. Album: The Razors Edge,Artist: AC/DC,Year: 1990,Country: Australia. Album: Ten,Artist: Pearl Jam,Year: 1991,Country: USA. Album: Urban Hymns,Artist: The Verve,Year: 1997,Country: England. asasasdasd

  21. Primer2: Želimo iz prethodnog rezultata da izdvojimo samo izvođače. Pošto je element artist direktan potomak elementa cd, primenjujemo šablon na cd. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="music"> <h3>Artists:</h3> <xsl:apply-templates /> </xsl:template> <xsl:template match="cd"> <xsl:apply-templates select="artist" /> </xsl:template> <xsl:template match="artist"> <b>Artist: </b> <xsl:value-of select="." /><br/> </xsl:template> <xsl:template match="album"> <b>Album: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="year"> <b>Year: </b> <xsl:value-of select="." />,<br/> </xsl:template> <xsl:template match="country"> <b>Country: </b> <xsl:value-of select="." />.<p/> </xsl:template> </xsl:stylesheet>

  22. <apply-templates> element iz prethodnog primera mora da ostane kako bi se obilazio ostatak dokumenta, a <xsl:apply-templates select="artist" /> u cd elementu će izdvojiti samo artist elemente, dok ostatak neće procesirati jer je viši po hijerarhiji. Rezultat : Artists: Artist: Guns N' RosesArtist: AC/DCArtist: Pearl JamArtist: The Verve asasasdasd

  23. <xsl:for-each> element - Instruction element - Omogućava iteracije u XSLT-u - Sadrži select atribut koji za vrednost ima XPath izraz - Služi za pristup svakom XML elementu u određenom čvoru Napravićemo novi prikaz za naš XML dokumet. Formiraćemo prikaz pomoću tabele: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="/"> <html> <body> <table border="1"> <tr style="background-color:#00aadd"> <th>Album</th> <th>Artist</th> <th>Year</th> <th>Country</th> </tr>

  24. <tr> <td><xsl:value-of select="./music/cd/album" /></td> <td><xsl:value-of select="./music/cd/artist" /></td> <td><xsl:value-of select="./music/cd/year" /></td> <td><xsl:value-of select="./music/cd/country" /></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Ovime smo formirali tabelu za prikaz elemenata, međutim ovakva struktura prikazuje samo jedan cd element:

  25. Pomoću <xsl:for-each> elementa možemo izlistati elemente za svaki cd element: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="/"> <html> <body> <table border="1"> <tr style="background-color:#00aadd"> <th>Album</th> <th>Artist</th> <th>Year</th> <th>Country</th> </tr> <xsl:for-each select="./music/cd"> <tr> <td><xsl:value-of select="album" /></td> <td><xsl:value-of select="artist" /></td> <td><xsl:value-of select="year" /></td> <td><xsl:value-of select="country" /></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

  26. Rezultat:

  27. <xsl:sort> element - Instruction element - Služi za sortiranje prikaza - Sadrži select atribut koji za vrednost ima XPath izraz koji određuje XML element koji treba sortirati Primer - sortiraćemo tabelu po elementu country: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="/"> <html> <body> <table border="1"> <tr style="background-color:#00aadd"> <th>Album</th> <th>Artist</th> <th>Year</th> <th>Country</th> </tr>

  28. <xsl:for-each select="./music/cd"> <xsl:sort select="country" /> <tr> <td><xsl:value-of select="album" /></td> <td><xsl:value-of select="artist" /></td> <td><xsl:value-of select="year" /></td> <td><xsl:value-of select="country" /></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>Rezultat:

  29. <xsl:if> element • - Instruction element • - Služi za postavljanje uslova za prikaz pojedinih elemenata • - Poseduje test atribut u kome se navodi uslov; ovaj atribut je obavezan • Sintaksa: • <xsl:if test = “izraz” > • <!-- sadržaj --> • </xsl:if> • Operatori: • jednakost  a=b • nejednakost  a!=b • a<b  a &lt; b • a>b  a &gt; b

  30. Primer: Prikazati podatke o cd elementima koji su izdati posle 1990. godine <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="/"> <html> <body> <table border="1"> <tr style="background-color:#00aadd"> <th>Album</th> <th>Artist</th> <th>Year</th> <th>Country</th> </tr>

  31. <xsl:for-each select="./music/cd"> <xsl:if test="year &gt; 1990" > <tr> <td><xsl:value-of select="album" /></td> <td><xsl:value-of select="artist" /></td> <td><xsl:value-of select="year" /></td> <td><xsl:value-of select="country" /></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> Rezultat: </xsl:stylesheet>

  32. <xsl:choose> element - Instruction element - Koristi se za formiranje if-else strukture - Sadrži elemente <xsl:when> i <xsl:otherwise>, koji predstavljaju if i else strukture, respektivno - Može postojati više <xsl:when> elemenata - <xsl:when> element ima test atribut koji sadrži uslov Primer: Promenićemo boju pozadine svim vrstama čiji albumi su izdati posle 1990. godine <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xml:output method="html" /> <xsl:template match="/"> <html> <body> <table border="1"> <tr style="background-color:#00aadd"> <th>Album</th> <th>Artist</th> <th>Year</th> <th>Country</th> </tr>

  33. <xsl:for-each select="./music/cd"> <xsl:choose> <xsl:when test="year &gt; 1990" > <tr> <td style="background-color:#ffaa00"><xsl:value-of select="album" /></td> <td style="background-color:#ffaa00"><xsl:value-of select="artist" /></td> <td style="background-color:#ffaa00"><xsl:value-of select="year" /></td> <td style="background-color:#ffaa00"><xsl:value-of select="country"/></td> </tr> </xsl:when> <xsl:otherwise> <tr> <td><xsl:value-of select="album" /></td> <td><xsl:value-of select="artist" /></td> <td><xsl:value-of select="year" /></td> <td><xsl:value-of select="country"/></td> </tr> </xsl:otherwise> </xsl:choose> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

  34. Rezultat:

More Related