Plan prezentacji Przedmiot: XML i jego zastosowania Wstęp Dr inż. Stanisław Polak Akademia Górniczo - Hutnicza w Krakowie, Katedra Informatyki http://www.icsr.agh.edu.pl/~polak/ Przetwarzanie danych 2.0 Dr inż. Stanisław Polak 1 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 2 Przedmiot: XML i jego zastosowania Wstęp Wstęp Ogólna charakterystyka Drzewo XML / (ang. XSL Transformations, Extensible Stylesheet Language Transformations) Przypomina języki funkcyjne Szeroko stosowany przy tworzeniu stron WWW 2 <p r a c o w n i k> 3 <i m i e>jan</ i m i e> <n a z w i s k o>nowak</ n a z w i s k o> 5 <d a t a u r o d z e n i a> 6 <rok>1969</ r o k> 7 <m i e s i a c>maj</ m i e s i a c> 8 <d z i e n>1</ d z i e n> 9 </ d a t a u r o d z e n i a> 10 <! komentarz > 11 <m i e j s c e p r a c y budynek="c1"/> 12 </ p r a c o w n i k> imie Jan nazwisko Nowak rok 1969 pracownik data urodzenia miesiac Maj dzien 1 komentarz miejsce pracy budynek= C1 Dr inż. Stanisław Polak 3 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak Przedmiot: XML i jego zastosowania
Wstęp Wstęp Arkusz Budowa 1 <? xml version=" 1.0 " e n c o d i n g="utf -8"?> 2 <! Zamiast " xsl:stylesheet " moż na u ż y ć " xsl:transform " > 3 <x s l : s t y l e s h e e t version=" 1.0 " x m l n s : x s l=" http: // www.w3.org /1999/ XSL / Transform " xmlns=" http: // www.w3.org /1999/ xhtml "> 5 <! S z a b l o n > 6 <x s l : template name=" nazwa "> 7... 8 </ x s l : template> 9... 10 <! S z a b l o n d e f i n i o w a n i e r e g u ł y > 11 <x s l : template match="wzorzec"> 12 Dane wynikowe 13 </ x s l : template> 1... 15 </ x s l : s t y l e s h e e t> Dołączanie arkusza Zewnętrznego 2 <?xml-stylesheet t y p e="text /xsl " href=" arkusz. xsl "?> 3 <elementg ł ówny>... 5 </ elementg ł ówny> Wewnętrznego Dokument XML 2 <x s l : s t y l e s h e e t version=" 1.0 " x m l n s : x s l= " http: // www.w3.org /1999/ XSL / Transform " xmlns=" http: // www.w3. org /1999/ xhtml "> 3... </ x s l : s t y l e s h e e t> arkusz.xsl 2 <? xml s t y l e s h e e t t y p e=" text / xml " h r e f="#id( mojstyl )"?> 3 <elementg ł ówny> <x s l : s t y l e s h e e t version=" 1.0 " x m l n s : x s l=" http: // www.w3.org /1999/ XSL / Transform " id=" mojstyl "> 5... 6 </ x s l : s t y l e s h e e t> 7... 8 </ elementg ł ówny> Dr inż. Stanisław Polak 5 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 6 Przedmiot: XML i jego zastosowania Definiowanie szablonu Element template Aplikowanie reguł Instrukcja apply-templates 1 <! Kategoria: element najwy ż szego poziomu ( top l e v e l element ) > 2 <x s l : t e m p l a t e 3 match = p a t t e r n name = qname 5 priority = number 6 mode = qname> 7 <! Zawarto ś ć : ( xsl: param, szablon ) > 8 </ x s l : t e m p l a t e> 1 <x s l : t e m p l a t e match="/"> 2 <html> 3 <x s l : a p p l y t e m p l a t e s /> </ html> 5 </ x s l : t e m p l a t e> Składnia Definiowanie reguły 1 <! K a t e g o r i a : i n s t r u k c j a > 2 <x s l : a p p l y t e m p l a t e s 3 select = node s e t e x p r e s s i o n mode = qname> 5 <! Zawarto ś ć : ( x s l : s o r t x s l : w i t h param ) > 6 </ x s l : a p p l y t e m p l a t e s> 1 <x s l : t e m p l a t e match="/"> 2 <html> 3 <x s l : apply-templates/> </ html> 5 </ x s l : t e m p l a t e> Składnia Rekurencyjne przetwarzanie wszystkich dzieci węzła / Dr inż. Stanisław Polak 7 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 8 Przedmiot: XML i jego zastosowania
Pobieranie wartości węzłów Instrukcja value-of Wstawianie danych testowych Instrukcja text 1 <! K a t e g o r i a : i n s t r u k c j a > 2 <x s l : v a l u e o f 3 select = s t r i n g e x p r e s s i o n disable-output-escaping = " yes " "no"/> Składnia 1 <! K a t e g o r i a : i n s t r u k c j a > 2 <x s l : t e x t 3 d i s a b l e output e s c a p i n g = " yes " "no" <! Zawarto ś ć : #PCDATA > 5 </ x s l : t e x t> Składnia 1 <x s l : t e m p l a t e match=" pracownik "> 2 <x s l : value-of select=" imie "/> 3 <x s l : t e x t> </ x s l : t e x t> <x s l : value-of select=" nazwisko "/> 5 </ x s l : t e m p l a t e> Pobranie imienia i nazwiska 1 <x s l : t e m p l a t e match=" pracownik "> 2 <x s l : v a l u e o f s e l e c t=" imie "/> 3 <x s l : text> </ x s l : text> <x s l : v a l u e o f s e l e c t=" nazwisko "/> 5 </ x s l : t e m p l a t e> Wstawienie spacji pomiędzy imię a nazwisko Dr inż. Stanisław Polak 9 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 10 Przedmiot: XML i jego zastosowania Sterowanie formatem wyprowadzanego wyniku Element output Sterowanie formatem wyprowadzanego wyniku Metody wyprowadzania wyniku 1 <! Kategoria: Element najwy ż szego poziomu > 2 <x s l : o u t p u t 3 method = " xml " " html " " text " qname but not ncname version = nmtoken 5 e n c o d i n g = s t r i n g 6 omit xml d e c l a r a t i o n = " yes " "no" 7 standalone = " yes " "no" 8 doctype p u b l i c = s t r i n g 9 doctype system = s t r i n g 10 cdata s e c t i o n e l e m e n t s = qnames 11 i n d e n t = " yes " "no" 12 media t y p e = s t r i n g /> 1 <x s l : o u t p u t method=" xml " version=" 1.0 " standalone=" yes " encoding="utf -8" omit xml d e c l a r a t i o n="no" doctype p u b l i c=" -// W3C // DTD HTML.01 Transitional // EN " doctype system=" http: // www.w3.org /TR/ html / loose.dtd " i n d e n t="yes " media t y p e=" text / xml "/> 2... Arkusz 1 <x s l : o u t p u t method="html"/> 2 <x s l : t e m p l a t e match="/"> 3 <html> <x s l :processing i n s t r u c t i o n name= php >echo (123)</ x s l :processing i n s t r u c t i o n> 5 <br xmlns=" http: // www. agh. edu.pl/"/> 6 <br /> 7 <j /> 8 <s c r i p t t y p e=" text / javascript " l a n g u a g e=" JavaScript 1.5 "> 9 alert (12 > 6) ; 10 </ s c r i p t> 11 </ html> 12 </ x s l :template> 2 <html><? php echo ( 1 2 3 )?><br xmlns=" http: // www. agh. edu.pl/"/><br /><j /><s c r i p t t y p e=" text / javascript " l a n g u a g e =" JavaScript 1.5 "> 3 alert (12 > ; 6) ; </ s c r i p t></ html> method= xml 1 <html> 2 <?php echo (123)><br xmlns=" http: // www. agh. edu.pl/"></br><br><j></ j><s c r i p t t y p e=" text / javascript " l a n g u a g e= " JavaScript 1.5 "> 3 alert (12 > 6) ; </ s c r i p t> 5 </ html> 1 <? xml version=" 1.0 " e n c o d i n g="utf -8" standalone=" yes "?> 2 <! DOCTYPE html PUBLIC " -// W3C // DTD HTML.01 Transitional // EN" " http: // www.w3. org /TR/ html / loose.dtd "> 3... 1 2 alert (12 > 6) ; method= html method= text Dr inż. Stanisław Polak 11 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 12 Przedmiot: XML i jego zastosowania
Przetwarzanie danych Przetwarzanie danych Metody konwersji dokumentu XML Reguły domyślne Dokument XML oraz XSL na kliencie 1 Klient przekształacanie i wyświetlanie przekształconego dokumentu (Firefox 1.0.2, Opera 9, IE 6, Netscape 8, Mozilla 1.7.8) Dokument XML oraz XSL na serwerze Serwer przekształcanie (PHP, Apache Cocoon,... ) Klient wyświetlanie przekształconego dokumentu Dokument przekształcany przez odrębny program, np.: SAXON obsługuje 1.0, 2.0 oraz 3.0 AltovaXML obsługuje 1,0 oraz 2.0 Apache Xalan obsługuje 1.0 xsltproc obsługuje 1.0 1 <x s l : t e m p l a t e match=" * / "> 2 <x s l : a p p l y t e m p l a t e s /> 3 </ x s l : t e m p l a t e> 5 <x s l : t e m p l a t e match=" * / " mode="m"> 6 <x s l : a p p l y t e m p l a t e s mode="m"/> 7 </ x s l : t e m p l a t e> 1 <x s l : t e m p l a t e match=" text () @*"> 2 <x s l : v a l u e o f s e l e c t="."/> 3 </ x s l : t e m p l a t e> Elementów Atrybutów i węzłów tekstowych 1 <x s l : t e m p l a t e match=" processing - instruction () comment ()"/> Instrukcji przetwarzania i komentarzy 1 Przeglądarki obsługują 1.0, a po zainstalowaniu Saxon-CE, również 2.0 Dr inż. Stanisław Polak 13 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 1 Przedmiot: XML i jego zastosowania Przetwarzanie danych Przetwarzanie danych Ogólny algorytm przetwarzania danych Ogólny algorytm wybierania reguł Utwórz drzewo reprezentujące dokument XML Dołącz reguły domyślne Rozpocznij rekurencyjne przetwarzanie drzewa XML Znajdź najlepiej pasującą regułę i wstaw do drzewa wynikowowego dane zawarte w treści reguły if W treści reguły znajduje się <apply-templates/> then Rozpocznij rekurencyjne przetwarzanie drzewa XML od wskazanego miejsca (gdy wyspecyfikowano wartość atrybutu select ) lub wszystkich elementów potomnych (gdy brak select ) end if if Nie istnieje reguła dla danego kontekstu then zastosuj regułę domyślną return end if if Istnieje dokładnie jedna reguła dla danego kontekstu then zastosuj tę regułę return end if if Istnieje kilka reguł dla danego kontekstu then zastosuj regułę o najwyższym priorytecie return end if if Istnieje kilka reguł dla danego kontekstu o jednakowym priorytecie then zastosuj tę z nich, która występuje najpóźniej w arkuszu return end if Dr inż. Stanisław Polak 15 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 16 Przedmiot: XML i jego zastosowania
Przykłady Przetwarzanie danych Przykłady Przetwarzanie danych Przykład 1 Użycie reguł domyślnych / 2 <p r a c o w n i k> 3 <i m i e>jan</ i m i e> <n a z w i s k o>nowak</ n a z w i s k o> 5 <d a t a u r o d z e n i a> 6 <rok>1969</ rok> 7 <m i e s i a c>maj</ m i e s i a c> 8 <d z i e n>1</ d z i e n> 9 </ d a t a u r o d z e n i a> 10 <! komentarz > 11 <m i e j s c e p r a c y budynek="c1"/> 12 </ p r a c o w n i k> 1 <x s l : s t y l e s h e e t version= 1.0 x m l n s : x s l= http: // www.w3. org /1999/ XSL / Transform > 2 </ x s l : s t y l e s h e e t> Arkusz 2 3 Jan Nowak 5 6 1969 7 Maj 8 1 imie Jan nazwisko Nowak rok 1969 pracownik data urodzenia miesiac Maj dzien 1 komentarz miejsce pracy budynek= C1 Dr inż. Stanisław Polak 17 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 18 Przedmiot: XML i jego zastosowania Przykłady Przetwarzanie danych Przykład 2 Użycie własnych reguł Przetwarzanie zbioru elementów po kolei Instrukcja for-each 2 <x s l : s t y l e s h e e t x m l n s :xsl=" http: // www.w3.org /1999/ XSL / Transform " version=" 1.0 "> 3 <x s l : o u t p u t method=" html " i n d e n t=" yes " e n c o d i n g="iso -8859-2 "/> 5 6 <x s l : t e m p l a t e match="/"> 7 <html> 8 <x s l : a p p l y t e m p l a t e s /> 9 </ html> 10 </ x s l :template> 11 12 <x s l : t e m p l a t e match=" text ()"/> 13 1 <x s l : t e m p l a t e match="pracownik"> 15 <head><t i t l e><x s l :value o f s e l e c t=" imie "/> <x s l : t e x t> </ x s l : t e x t><x s l :value o f s e l e c t=" nazwisko "/></ t i t l e></ head> 16 <body> 17 <h1><x s l :value o f s e l e c t=" imie "/> <x s l : t e x t> </ x s l : t e x t><x s l :value o f s e l e c t=" nazwisko "/> </h1> 18 <x s l : a p p l y t e m p l a t e s /> 19 </ body> 20 </ x s l :template> 21 22 <xsl:template match="data urodzenia"> Urodzony: <x s l : t e x t> </ x s l : t e x t> 23 <x s l :value o f s e l e c t=" dzien "/><x s l : t e x t> </ x s l : t e x t> <x s l :value o f s e l e c t=" miesiac "/> 2 <x s l : t e x t> </ x s l : t e x t> <x s l :value o f s e l e c t=" rok "/> <br /> 25 </ x s l :template> 26 27 <xsl:template match="miejsce pracy"> Pracuje w budynku: <xsl:value of s e l e c t=" @budynek "/> <br /> </ x s l :template> 28 29 </ x s l : s t y l e s h e e t> Arkusz 1 <html> 2 <head> 3 <meta http e q u i v=" Content - Type " content=" text / html ; charset =iso -8859-2 "> <title>jan Nowak</ title> 5 </ head> 6 <body> 7 <h1>jan Nowak</h1> Urodzony : 1 Maj 1969<br> Pracuje w budynku : C1<br> 8 </ body> 9 </ html> imie Jan nazwisko Nowak rok 1969 / pracownik data urodzenia miesiac Maj dzien 1 komentarz miejsce pracy budynek= C1 1 <x s l : t e m p l a t e match="/"> 2 <html> 3 <head><t i t l e>customers</ t i t l e></ head> <body> 5 <t a b l e> 6 <tbody> 7 <x s l : for-each select=" customers / customer "> 8 <t r> 9 <th><x s l : a p p l y t e m p l a t e s s e l e c t=" name "/></ th> 10 <x s l : for-each select=" order "> 11 <td><x s l : a p l y t e m p l a t e s /></ p td> 12 </ x s l : for-each> 13 </ t r> 1 </ x s l : for-each > 15 </ tbody> 16 </ t a b l e> 17 </ body> 18 </ html> 19 </ x s l :template> Arkusz 1 <c u s t o m e r s> 2 <customer> 3 <name>...</name> <o r d e r>...</ o r d e r> 5 <o r d e r>...</ o r d e r> 6 </ customer> 7 <customer> 8 <name>...</name> 9 <o r d e r>...</ o r d e r> 10 <o r d e r>...</ o r d e r> 11 </ customer> 12 </ c u s t o m e r s> 1 <html xmlns=" http :// www.w3.org /1999/ xhtml "> 2 <head> 3 <meta http e q u i v=" Content - Type " content=" text / html ; charset = UTF -8"> <title>customers</ title> 5 </ head> 6 <body> 7 <table> 8 <tbody> 9 <tr><th>...</th><td>...</td><td>...</td></tr> 10 <tr><th>...</th><td>...</td><td>...</td></tr> 11 </ tbody> 12 </ table> 13 </ body> 1 </ html> Dr inż. Stanisław Polak 19 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 20 Przedmiot: XML i jego zastosowania
Tworzenie elementów i atrybutów Instrukcje element oraz attribute Definiowanie zbioru atrybutów Element attribute-set 1 <x s l : t e m p l a t e match="/"> 2 <x s l : element name="qqq"> 3 <x s l : element name="{//bbb}" namespace=" http: //www.agh.edu.pl/"> <x s l : attribute name = "{//CCC}" >a 5 b</ x s l : attribute> 6 <XXX><x s l : attribute name="b" namespace=" http: // www. agh. edu.pl/">a</ x s l : attribute></xxx> 7 </ x s l : element> 8 </ x s l : element> 9 </ x s l : t e m p l a t e> Arkusz 1 <AAA> 2 <BBB>bbb</BBB> 3 <CCC>ccc</CCC> </AAA> 2 <QQQ> 3 <bbb xmlns="http: //www.agh.edu.pl/" ccc="a b"> <XXX xmlns="" x m l n s : n s 1="http: //www.agh. edu.pl/" n s 1 : b="a"/> 5 </bbb> 6 </QQQ> 1 <x s l : t e m p l a t e match=" chapter / heading "> 2 <f o : b l o c k quadding=" start " x s l : use-attribute-sets=" title - style "> 3 <x s l : a p p l y t e m p l a t e s /> </ f o : b l o c k> 5 </ x s l : t e m p l a t e> 6 <x s l : attribute-set name="title - style "> 7 <x s l : attribute name="font - size ">12 pt</ x s l : a t t r i b u t e> 8 <x s l : attribute name="font - weight ">bold</ x s l : a t t r i b u t e> 9 </ x s l : attribute-set> Definiowanie nazwanego zbioru atrybutów Dr inż. Stanisław Polak 21 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 22 Przedmiot: XML i jego zastosowania Tworzenie instrukcji przetwarzania i komentarzy Instrukcje processing-instruction oraz comment Zliczanie węzłów Instrukcja number 1 <x s l : processing-instruction name="xml - stylesheet ">href= "a. css " type=" text / css "</ x s l : processinginstruction> 2 <x s l : processing-instruction name=" php "> 3 echo ( "<a b="1">c </a>" ) </ x s l : processing-instruction> 5 <x s l : processing-instruction name=" php ">echo ( "?>" ) 6 </ x s l : processing-instruction><! Komunikat bł ę du: " xsl:processing - instruction:?> not allowed within PI content " > 7 8 <x s l : comment>this file is automatically generated. Do not edit!</ x s l : comment> 9 <x s l : comment> </ x s l : comment> <! Komunikat b ł ę d u : " xsl:comment : -- or ending - not allowed in comment " > Arkusz 1 <? xml s t y l e s h e e t h r e f="a. css " t y p e=" text / css "?> 2 <? php 3 echo ( "c" )?> 5 6 <! This f i l e i s a u t o m a t i c a l l y g e n e r a t e d. Do not e d i t! > 7 1 <x s l : t e m p l a t e match=" items "> 2 <x s l : f o r each s e l e c t=" item "> 3 <x s l : s o r t s e l e c t="."/> <p> 5 <x s l : number value=" position ()" format="1. "/> 6 <x s l : v a l u e o f s e l e c t="."/> 7 </p> 8 </ x s l : f o r each> 9 </ x s l : t e m p l a t e> Arkusz 1 <i t e m s> 2 <item>a</ item> 3 <item>b</ item> </ i t e m s> 1 <p>1. a</p> 2 <p>2. b</p> Dr inż. Stanisław Polak 23 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 2 Przedmiot: XML i jego zastosowania
Zmienne Instrukcja variable Makrodefinicje 1 <! Zmienna globalna > 2 <x s l : variable name="para -font - size ">12pt</ x s l : variable> 3 <x s l : v a r i a b l e name="para -font - size ">10 pt</ x s l : v a r i a b l e> <! Komunikat b ł ę d u : " redefinition of global variable para -font - size " > 5 <x s l : t e m p l a t e match="/"> 6 <! Zmienne lokalne > 7 <x s l : variable name ="para -font - size ">10pt</ x s l : variable> 8 <x s l : variable name ="para -font - size ">8pt</ x s l : variable> <! Komunikat bł ę du: " - variable: Redefinition of variable para -font -size." > 9 <x s l : v a r i a b l e name =" value " select=" count (/) "/> 10 <f o : b l o c k f o n t s i z e="{$para-font-size}"><x s l : v a l u e o f s e l e c t="$value"/> 11 </ f o : b l o c k> 12 </ x s l : t e m p l a t e> Arkusz 1 <f o : b l o c k f o n t s i z e="10 pt">1</ f o : b l o c k> 1 <x s l : template name ="numbered - block "> 2 <x s l : param name=" format ">1. </ x s l : param> 3 <f o : b l o c k> <x s l : n u m b e r format="{$format}"/> 5 <x s l : a p p l y t e m p l a t e s /> 6 </ f o : b l o c k> 7 </ x s l : template> 8 9 <x s l : t e m p l a t e match="// ol/li"> 10 <x s l : call-template name="numbered - block "> 11 <x s l : with-param name=" format ">a. </ x s l : withparam> 12 </ x s l : call-template> 13 </ x s l : t e m p l a t e> 1 15 <x s l : t e m p l a t e match="// ul/li"> 16 <x s l : call-template name="numbered - block "/> 17 </ x s l : t e m p l a t e> Arkusz 1 <body> 2 <o l> 3 < l i>aaa</ l i> < l i>bbb</ l i> 5 </ o l> 6 <u l> 7 < l i>ccc</ l i> 8 < l i>ddd</ l i> 9 </ u l> 10 </ body> 1 <f o : b l o c k>a. aaa</ f o : b l o c k> 2 <f o : b l o c k>b. bbb</ f o : b l o c k> 3 5 <f o : b l o c k>1. ccc</ f o : b l o c k> 6 <f o : b l o c k>2. ddd</ f o : b l o c k> Dr inż. Stanisław Polak 25 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 26 Przedmiot: XML i jego zastosowania Kopiowanie węzłów Instrukcja copy oraz copy-of 1 <Name> 2 <Family>Smith</ Family> 3 <Given>John</ Given> </Name> 1 <x s l : t e m p l a t e match=" Name "> 2 <x s l : copy/> 3 </ x s l : t e m p l a t e> </ x s l : s t y l e s h e e t> 1 <Name/> Arkusz 1 <x s l : t e m p l a t e match=" Name "> 2 <x s l : copy-of select="."/> 3 </ x s l : t e m p l a t e> </ x s l : s t y l e s h e e t> Arkusz 1 <Name> 2 <Family>Smith</ Family> 3 <Given>John</ Given> </Name> Sortowanie Instrukcja sort 1 <x s l : t e m p l a t e match=" employees "> 2 <u l> 3 <x s l : a p p l y t e m p l a t e s s e l e c t=" employee "> <x s l : sort select=" name / family "/> 5 <x s l : sort select=" name / given "/> 6 </ x s l :apply t e m p l a t e s> 7 </ u l> 8 </ x s l :template> 9 10 <x s l : t e m p l a t e match=" employee "> 11 <l i> 12 <x s l : v a l u e o f s e l e c t=" name / given "/> 13 <x s l : t e x t> </ x s l : t e x t> 1 <x s l : v a l u e o f s e l e c t=" name / family "/> 15 </ l i> 16 </ x s l :template> 17 </ x s l : s t y l e s h e e t> 1 <employees> 2 <employee> 3 <name> <g i v e n>john</ g i v e n> 5 <f a m i l y>doe</ f a m i l y> 6 </name> 7 </ employee> 8 <employee> 9 <name> 10 <g i v e n>james</ g i v e n> 11 <f a m i l y>clark</ f a m i l y> 12 </name> 13 </ employee> 1 </ employees> Szablon 1 <ul> 2 <li>james Clark</li> 3 <li>john Doe</li> </ul> Dr inż. Stanisław Polak 27 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 28 Przedmiot: XML i jego zastosowania
Przetwarzanie warunkowe Instrukcja if oraz choose 1 <n a m e l i s t> 2 <name>kowalski</name> 3 <name>nowak</name> <name>smith</name> 5 <name>doe</name> 6 </ n a m e l i s t> 1 <x s l : t e m p l a t e match=" namelist / name "> 2 <x s l : a p p l y t e m p l a t e s /> 3 <x s l : if test=" not ( position ()= last () -1)">, </ x s l : if> </ x s l :template> 1 Kowalski, 2 Nowak, 3 Smith, Doe Arkusz Dokument wynikowy 1 <o r d e r e d l i s t> 2 <l i s t i t e m>kowalski</ l i s t i t e m> 3 <o r d e r e d l i s t> <l i s t i t e m>nowak</ l i s t i t e m> 5 <o r d e r e d l i s t> 6 <l i s t i t e m>polak</ l i s t i t e m> 7 <l i s t i t e m>smith</ l i s t i t e m> 8 </ o r d e r e d l i s t> 9 </ o r d e r e d l i s t> 10 </ o r d e r e d l i s t> 1 <x s l : t e m p l a t e match=" orderedlist / listitem "> 2 <x s l : v a r i a b l e name=" level " s e l e c t=" count ( ancestor::orderedlist ) mod 3"/> 3 <x s l : choose> <x s l : when t e s t= $ level =1 > 5 <x s l : n u m b e r format="i"/> 6 </ x s l : when> 7 <x s l : w h e n t e s t= $ level =2 > 8 <x s l : n u m b e r format="a"/> 9 </ x s l : w h e n> 10 <x s l : otherwise> 11 <x s l : n u m b e r format="1"/> 12 </ x s l : otherwise> 13 </ x s l : choose> 1 <x s l : t e x t>. </ x s l : t e x t> 15 <x s l : a p p l y t e m p l a t e s /> 16 </ x s l :template> 1 i. Kowalski 2 a. Nowak 3 1. Polak 2. Smith Arkusz Dokument wynikowy Importowanie oraz dołączanie arkuszy Instrukcja import oraz include 1 <examples> 2 <example>polak</ example> 3 <example>nowak</ example> <example>kowalski</ example> 5 </ examples> 1 <x s l : t e m p l a t e match=" example "> 2 <p r e><x s l : a p p l y t e m p l a t e s /></ p r e> 3 </ x s l :template> 1 <x s l : import h r e f=" doc. xsl "/> 2 3 <x s l : t e m p l a t e match=" example "> <d i v s t y l e=" border: solid red "> 5 <x s l : apply-imports/> 6 </ d i v> 7 </ x s l :template> Główny arkusz 1 <div style=" border : solid red "><pre>polak</ pre></ div> 2 <div style=" border : solid red "><pre>nowak</ pre></ div> 3 <div style=" border : solid red "><pre>kowalski</ pre></ div> Arkusz doc.xsl 1 <x s l : include h r e f=" doc. xsl "/> 2 3 <x s l : t e m p l a t e match=" example "> <d i v s t y l e=" border: solid red "> 5 <x s l : a p p l y i m p o r t s /> 6 </ d i v> 7 </ x s l :template> Główny arkusz 1 <div style=" border : solid red "></ div> 2 <div style=" border : solid red "></ div> 3 <div style=" border : solid red "></ div> Dr inż. Stanisław Polak 29 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 30 Przedmiot: XML i jego zastosowania 2.0 2.0 2.0 Ogólna charakterystyka Obsługa z poziomu przeglądarki WWW Używa XPath 2.0 Obsługuje drzewa tymczasowe Pozwala na grupowanie węzłów i iteracyjne przetwarzanie grup instrukcja for-each-group Można definiować własne funkcje dla wyrażeń ścieżkowych Możliwość generowania wielu dokumentów wyjściowych instrukcja xsl:result-document Możliwość przeszukiwania tekstu z zastosowaniem wyrażeń regularnych instrukcja analyze-string XHTML nowy format wyjściowy dla xsl:output Ulepszone opcje sortowania Możliwość wczytywania plików tekstowych funkcja unparsed-text()... Saxon-CE (Client Edition) darmowa implementacja 2.0 w języku JavaScript http://www.saxonica.com/ce/index.xml Dr inż. Stanisław Polak 31 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 32 Przedmiot: XML i jego zastosowania
Przykłady 2.0 Przetwarzanie drzewa tymczasowego 1 <x s l : s t y l e s h e e t version=" 2.0 " x m l n s : x s l=" http: // www.w3.org /1999/ XSL / Transform "> 2 <x s l : o u t p u t method=" xml " i n d e n t=" yes "/> 3 <x s l : t e m p l a t e match="/"> 5 <x s l : v a r i a b l e name=" temp "> 6 <x s l : e l e m e n t name=" root "> 7 <x s l : e l e m e n t name="a">pierwszy</ x s l : e l e m e n t> 8 <x s l : e l e m e n t name="a">drugi</ x s l : e l e m e n t> 9 </ x s l : e l e m e n t> 10 </ x s l : v a r i a b l e> 11 <wynik><x s l : v a l u e o f s e l e c t="$temp/root/a[1]"/></ wynik> 12 </ x s l : t e m p l a t e> 13 1 </ x s l : s t y l e s h e e t> 2 <wynik>pierwszy</ wynik> Arkusz Przykłady 2.0 Grupowanie węzłów 1 <x s l : s t y l e s h e e t x m l n s :xsl=" http: // www.w3.org /1999/ XSL / Transform " version=" 2.0 "> 2 <x s l : o u t p u t method=" html " i n d e n t=" yes "/> 3 <x s l : t e m p l a t e match="/"> 5 <t a b l e> 6 <x s l : for-each-group select=" cities / city " group-by=" @country "> 7 <t r> 8 <td><x s l : v a l u e o f s e l e c t=" @country "/></ td> 9 <td><x s l : v a l u e o f s e l e c t="current-group()/ @name " s e p a r a t o r=", "/></ td> 10 <td><x s l : v a l u e o f s e l e c t=" sum (current-group()/ @pop )"/></ td> 11 </ t r> 12 </ x s l : for-each-group> 13 </ t a b l e> 1 </ x s l :template> 15 </ x s l : s t y l e s h e e t> Arkusz 1 <? xml v e r s i o n=" 1.0 " e n c o d i n g="utf -8"?> 2 <table> 3 <tr> <td>italy</td> 5 <td>milan, venice</td> 6 <td>6</td> 1 <c i t i e s> 7 </tr> 2 <c i t y name=" milan " c o u n t r y=" italy " pop="5"/> 8 <tr> 3 <c i t y name=" paris " c o u n t r y=" france " pop="7"/> 9 <td>france</td> <c i t y name=" munich " c o u n t r y=" germany " pop=""/> 10 <td>paris, lyon</td> 5 <c i t y name=" lyon " c o u n t r y=" france " pop="2"/> 11 <td>9</td> 6 <c i t y name=" venice " c o u n t r y=" italy " pop="1"/> 12 </tr> 7 </ c i t i e s> 13 <tr> 1 <td>germany</td> 15 <td>munich</td> 16 <td></td> 17 </tr> 18 </ table> Wejściowy dokument HTML Dr inż. Stanisław Polak 33 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 3 Przedmiot: XML i jego zastosowania Przykłady 2.0 Definiowanie funkcji użytkownika Przykłady 2.0 Generowanie wielu dokumentów wyjściowych 1 <x s l : s t y l e s h e e t version=" 2.0 " 2 x m l n s : x s l=" http: // www.w3.org /1999/ XSL / Transform " 3 x m l n s : l o c=" http: // cokolwiek "> 5 <x s l : function name=" loc:silnia "> 6 <x s l : param name="n"/> 7 <x s l : sequence s e l e c t="if ($n< ;=1) 8 then 1 9 else 10 $n * loc:silnia ($n - 1)"/> 11 </ x s l : function> 12 13 <x s l : t e m p l a t e match="/"> 1 Silnia 10 wynosi <x s l : v a l u e o f s e l e c t=" loc:silnia (10) "/> 15 </ x s l : t e m p l a t e> 16 </ x s l : s t y l e s h e e t> Arkusz 2 <x s l : s t y l e s h e e t x m l n s : x s l=" http: // www.w3.org /1999/ XSL / Transform " version=" 2.0 "> 3 <x s l : o u t p u t method=" html " i n d e n t=" yes " name=" html "/> 5 <x s l : t e m p l a t e match="/"> 6 <x s l : f o r each s e l e c t="// testrun "> 7 <x s l : v a r i a b l e name=" filename " s e l e c t=" concat ( output1 /,@run,. html )" /> 8 <x s l : v a l u e o f s e l e c t="$ filename " /> 9 <x s l : result-document href="{$ filename }" format=" html "> 10 <html> 11 <body><x s l : v a l u e o f s e l e c t=" @run "/></body> 12 </ html> 13 </ x s l : result-document> 1 </ x s l : f o r each> 15 </ x s l : t e m p l a t e> 16 </ x s l : s t y l e s h e e t> Arkusz 2 <t e s t s> 3 <t e s t r u n run=" test1 "> <t e s t name=" foo " p a s s=" true " /> 5 <t e s t name=" bar " p a s s=" true " /> 6 <t e s t name=" baz " p a s s=" true " /> 7 </ t e s t r u n> 8 <t e s t r u n run=" test2 "> 9 <t e s t name=" foo " p a s s=" true " /> 10 <t e s t name=" bar " p a s s=" false " /> 11 <t e s t name=" baz " p a s s=" false " /> 12 </ t e s t r u n> 13 <t e s t r u n run=" test3 "> 1 <t e s t name=" foo " p a s s=" false " /> 15 <t e s t name=" bar " p a s s=" true " /> 16 <t e s t name=" baz " p a s s=" false " /> 17 </ t e s t r u n> 18 </ t e s t s> 1 Silnia 10 wynosi 3628800 Dokument wynikowy 1 <html> 2 <body>test1</ body> 3 </ html> output1/test1.html 1 <html> 2 <body>test2</ body> 3 </ html> output1/test2.html 1 <html> 2 <body>test3</ body> 3 </ html> output1/test3.html Dr inż. Stanisław Polak 35 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 36 Przedmiot: XML i jego zastosowania
3.0 3.0 3.0 Ogólna charakterystyka Obsługa błędów Working draft. Poprzednia nazwa: 2.1 Używa XPath 3.0 Rozszerzona składnia wzorców w regułach Obsługa błędów instrukcje try / catch Wyrażenia XPath instrukcja evaluate Wsparcie dla przetwarzania strumieniowego... 1 <x s l : r e s u l t document h r e f=" out. xml "> 2 <x s l : v a r i a b l e name=" result "> 3 <x s l : c a l l t e m p l a t e name=" construct - output "/> </ x s l : v a r i a b l e> 5 <xsl:try> 6 <x s l : c o p y o f s e l e c t="$ result " v a l i d a t i o n=" strict "/> 7 <xsl:catch> 8 <x s l : m e s s a g e>warning: validation of result document failed: 9 Error code: <x s l : v a l u e o f s e l e c t="$ err:code "/> 10 Reason: <x s l : v a l u e o f s e l e c t="$ err:description "/> 11 </ x s l : m e s s a g e> 12 <x s l : s e q u e n c e s e l e c t="$ result "/> 13 </xsl:catch> 1 </xsl:try> 15 </ x s l : r e s u l t document> Dr inż. Stanisław Polak 37 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 38 Przedmiot: XML i jego zastosowania 3.0 3.0 Ewaluacja wyrażeń XPath Przetwarzanie strumieniowe 1 <xsl: param name=" sortkey " as=" xs:string " s e l e c t=" @name "/> 2... 3 <x s l : s o r t> 5 <xsl:evaluate xpath="$ sortkey " as=" xs:string "/> 6 </ x s l : s o r t> 2 <x s l : s t y l e s h e e t version=" 3.0 " 3 x m l n s : x s l=" http: // www.w3.org /1999/ XSL / Transform "> 5 <x s l : t e m p l a t e match="/"> 6 <xsl:stream hre f=" books.xml "> 7 <xsl:iterate s e l e c t="/ books / book "> 8 <x s l : r e s u l t document h r e f="{ concat ( book, position (),.xml )}"> 9 <x s l : c o p y o f s e l e c t="."/> 10 </ x s l : r e s u l t document> 11 <x s l : n e x t i t e r a t i o n /> 12 </xsl:iterate> 13 </xsl:stream> 1 </ x s l : t e m p l a t e> 15 </ x s l : s t y l e s h e e t> Dr inż. Stanisław Polak 39 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 0 Przedmiot: XML i jego zastosowania
Źródła Źródła Źródła I Źródła II Patryk Czarnik. XML i nowoczesne technologie zarządzania treścią. http://www.mimuw.edu.pl/~czarnik/. Jack Herrington. Tip: Create multiple files in 2.0. http://www.ibm.com/developerworks/xml/library/x-tipmultxsl/ index.html. Evan Lenz. How Works. http://lenzconsulting.com/how-xslt-works/. stylusstudio.com. Introduction to 3.0. http://www.stylusstudio.com/tutorials/intro-xslt-3.html. W3C. XSL Transformations () Version 1.0. http://www.w3.org/tr/xslt. W3C. XSL Transformations () Version 2.0. http://www.w3.org/tr/xslt20/. W3C. XSL Transformations () Version 3.0. http://www.w3.org/tr/xslt-30/. Fundacja Wikipedia. Wolna encyklopedia Wikipedia. http://pl.wikipedia.org/. Zvon. Tutorial. http://zvon.org/xxl/utorial/output_pol/. Dr inż. Stanisław Polak 1 Przedmiot: XML i jego zastosowania Dr inż. Stanisław Polak 2 Przedmiot: XML i jego zastosowania