From 71fcf7c296f16e31fe0e75eee08059456c080206 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 10 Aug 2014 13:09:42 +0200 Subject: Switched content formatter to kramdown * implemented language selection for automatic syntax highlighting ** language selection requires the language to be used to be passed as a class of the code element ** kramdown enables easy definition of this class attribute * kramdown offers more functionality such as table and class attribute support * updated all articles accordingly --- .../2011-11-08_informationen_umformen_mit_xsl.md | 196 +++++++++++---------- 1 file changed, 105 insertions(+), 91 deletions(-) (limited to 'source/00_content/articles/2011-11-08_informationen_umformen_mit_xsl.md') diff --git a/source/00_content/articles/2011-11-08_informationen_umformen_mit_xsl.md b/source/00_content/articles/2011-11-08_informationen_umformen_mit_xsl.md index 40777d0..47f9973 100644 --- a/source/00_content/articles/2011-11-08_informationen_umformen_mit_xsl.md +++ b/source/00_content/articles/2011-11-08_informationen_umformen_mit_xsl.md @@ -8,49 +8,57 @@ Schlussendlich habe ich dann eine [XSLT](http://de.wikipedia.org/wiki/XSLT) gesc Mit XSL lassen sich XML Dateien in andere Formen bringen. Da Mediawiki mehr oder weniger valides XHTML ausgibt, kann man, nachdem das XHTML mit [Tidy](http://tidy.sourceforge.net) ein wenig aufgeräumt wurde, sehr einfach die [Terminliste](http://wiki.piratenpartei.de/BW:Kreisverband_Konstanz/Termine) extrahieren und gleichzeitig in RSS umformen: - - - - - - - - - - Termine des KV Konstanz - http://wiki.piratenpartei.de/BW:Kreisverband_Konstanz/Termine - Termine des KV Konstanz - - - - - - - - - - - <xsl:value-of select="x:td[3]"/> am <xsl:call-template name="format-date"> - <xsl:with-param name="date" select="x:td[1]/x:span/."/> - <xsl:with-param name="format" select="'d.n.Y'"/></xsl:call-template> um <xsl:value-of select="x:td[2]"/> Uhr - http://wiki.piratenpartei.de - - - - - - - - - +~~~ + + + + + + + + + + Termine des KV Konstanz + http://wiki.piratenpartei.de/BW:Kreisverband_Konstanz/Termine + Termine des KV Konstanz + + + + + + + + + <xsl:value-of select="x:td[3]"/> + <xsl:text> am </xsl:text> + <xsl:call-template name="format-date"> + <xsl:with-param name="date" select="x:td[1]/x:span/."/> + <xsl:with-param name="format" select="'d.n.Y'"/> + </xsl:call-template> + <xsl:text> um </xsl:text> + <xsl:value-of select="x:td[2]"/> + <xsl:text> Uhr</xsl:text> + + + http://wiki.piratenpartei.de + + + + + + + + +~~~ +{: .language-xsl} Der Kern dieses XSL ist nicht mehr als ein Template welches auf den [XPATH](http://de.wikipedia.org/wiki/XPATH) zum Finden der Terminliste reagiert. Die For-Each-Schleife iteriert dann durch die Termine und formt diese entsprechend in RSS um. Der einzige Knackpunkt kommt daher, dass XHTML kein normales XML ist und somit seinen eigenen Namespace hat. Diesen sollte man im Element `xsl:stylesheet` korrekt angeben, sonst funktioniert nichts. Auch muss im XPATH Ausdruck dann vor jedem Element ein `x:` eingefügt werden um dem XSL Prozessor den Namespace für das jeweilige Element mitzuteilen. @@ -64,66 +72,72 @@ Zum Anpassen des Datums verwende ich - wie auch in diesem Blog - die [date-time. Mit der eben beschriebenen XSLT lässt sich jetzt in drei Schritten das fertige RSS generieren: - #!/bin/sh - wget -O termine_wiki.html "http://wiki.piratenpartei.de/BW:Kreisverband_Konstanz/Termine" - tidy -asxml -asxhtml -O termine_tidy.html termine_wiki.html - xsltproc --output termine_kvkn.rss --novalid termine_kvkn.xsl termine_tidy.html +~~~ +#!/bin/sh +wget -O termine_wiki.html "http://wiki.piratenpartei.de/BW:Kreisverband_Konstanz/Termine" +tidy -asxml -asxhtml -O termine_tidy.html termine_wiki.html +xsltproc --output termine_kvkn.rss --novalid termine_kvkn.xsl termine_tidy.html +~~~ +{: .language-sh} In PHP ist das ganze dann zusammen mit sehr einfachem Caching auch direkt auf einem Webserver einsetzbar: - define('CACHE_TIME', 6); - define('CACHE_FILE', 'rss.cache'); +~~~ +define('CACHE_TIME', 6); +define('CACHE_FILE', 'rss.cache'); - header("Content-Type: application/rss+xml"); +header("Content-Type: application/rss+xml"); - if ( file_exists(CACHE_FILE) ) - { - if ( !filemtime(CACHE_FILE) < time() - CACHE_TIME * 3600 ) { - readfile(CACHE_FILE); - } - else { - if ( !generate_rss() ) { - readfile(CACHE_FILE); - } - } +if ( file_exists(CACHE_FILE) ) +{ + if ( !filemtime(CACHE_FILE) < time() - CACHE_TIME * 3600 ) { + readfile(CACHE_FILE); } else { - ob_start(); - - generate_rss(); - - ob_end_flush(); - - $cache_file = fopen(CACHE_FILE, 'w'); - fwrite($cache_file, ob_get_contents()); - fclose($cache_file); + if ( !generate_rss() ) { + readfile(CACHE_FILE); + } } - - function generate_rss() +} +else { + ob_start(); + + generate_rss(); + + ob_end_flush(); + + $cache_file = fopen(CACHE_FILE, 'w'); + fwrite($cache_file, ob_get_contents()); + fclose($cache_file); +} + +function generate_rss() +{ + $event_page = file_get_contents('http://wiki.piratenpartei.de/BW:Kreisverband_Konstanz/Termine'); + + if ($event_page != false) { - $event_page = file_get_contents('http://wiki.piratenpartei.de/BW:Kreisverband_Konstanz/Termine'); - - if ($event_page != false) - { - $config = array('output-xhtml' => true, 'output-xml' => true); + $config = array('output-xhtml' => true, 'output-xml' => true); - $tidy = new tidy(); + $tidy = new tidy(); - $xml = new DomDocument(); - $xml->loadXML( $tidy->repairString($event_page, $config) ); + $xml = new DomDocument(); + $xml->loadXML( $tidy->repairString($event_page, $config) ); - $xsl = new DomDocument(); - $xsl->load('termine_kvkn.xsl'); + $xsl = new DomDocument(); + $xsl->load('termine_kvkn.xsl'); - $xslt = new XsltProcessor(); - $xslt->importStylesheet($xsl); + $xslt = new XsltProcessor(); + $xslt->importStylesheet($xsl); - $rss = $xslt->transformToXML($xml); - echo $rss; - - return true; - } - else { - return false; - } + $rss = $xslt->transformToXML($xml); + echo $rss; + + return true; + } + else { + return false; } +} +~~~ +{: .language-php} -- cgit v1.2.3