Difference between revisions of "XSLT"
(→Beispiel Notizbuch) |
(→Beispiel Notizbuch) |
||
Line 131: | Line 131: | ||
Generiert als Ergebnis die [[Atom]]-Datei '''NotizbuchAtom.xml''': | Generiert als Ergebnis die [[Atom]]-Datei '''NotizbuchAtom.xml''': | ||
<pre> | <pre> | ||
− | + | <?xml version="1.0" encoding="UTF-8"?> | |
+ | <feed xmlns="http://www.w3.org/2005/Atom"> | ||
+ | <title>Notizbuch</title> | ||
+ | <subtitle>Ehemalige Outlook-Notizen</subtitle> | ||
+ | <link rel="self" href="http://example.org/feed/"/> | ||
+ | <link href="http://example.org/"/> | ||
+ | <updated>2008-03-31T18:56:03Z</updated> | ||
+ | <author> | ||
+ | <name>Dietrich Kracht</name> | ||
+ | <email>dietrich@kr8.de</email> | ||
+ | </author> | ||
+ | <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> | ||
+ | <entry> | ||
+ | <title>Zählspiel nach Stableford</title> | ||
+ | <link href="http://localhost/wordpress/?p=33"/> | ||
+ | <id>[FA797DE2-6761-4369-B0C5E5FD4B3D1D86]</id> | ||
+ | <updated>2005-03-05T22:14:45Z</updated> | ||
+ | <published>2008-03-31T16:21:11Z</published> | ||
+ | <category term="Notizbuch"/> | ||
+ | <author> | ||
+ | <name>Dietrich Kracht</name> | ||
+ | </author> | ||
+ | <content type="xhtml"><![CDATA[<DIV>Zählspiel nach Stableford<br /><br />Vorgabe 0 18 36 54<br />Par 2 3 4 5 <br />1 über Par 1 2 3 4 <br />2 über Par 0 1 2 3<br />3 über Par 0 0 1 2<br />4 über Par 0 0 0 1</DIV>]]> | ||
+ | </content> | ||
+ | <content2>Zählspiel nach Stableford Vorgabe 0 18 36 54 Par 2 3 4 5 1 über Par 1 2 3 4 2 über Par 0 1 2 3 3 über Par 0 0 1 2 4 über Par 0 0 0 1 Open source Outlook message >> | ||
+ | </content2> | ||
+ | </entry> | ||
+ | ... | ||
+ | </feed> | ||
</pre> | </pre> | ||
Revision as of 20:03, 12 April 2009
Siehe auch: XSLT
Mithilfe von XSL-Stylesheets kann man XML-Dokumente in verschiedenen Output-Formate transformieren.
Web Links
XSL Namespace
Das XSL-Stylesheet sollte anfangen mit:
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
XSL Output Methods
- <xsl:output method = "text" />
- <xsl:output method = "html" />
- <xsl:output method = "xml" />
Ein einfaches Beispiel
Als Eingabe folgendes XML-Dokument:
<?xml version="1.0" encoding="UTF-8"?> <message>Yep, it worked!</message>
Zur Transformation folgendes XSL-Stylesheet:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" encoding="UTF-8"/> <!-- simply copy the message to the result tree --> <xsl:template match="/"> <xsl:value-of select="message"/> </xsl:template> </xsl:stylesheet>
Der Aufruf eines XSLT-Prozessors erzeut die Ausgabedatei (output-mthod="text"):
Yep, it worked!
XSLT-Prozessoren
Um aus Eingabe und XSLT-Sheet eine Ausgabe zu erzeugen, benötigen wir einen sog. XSLT-Prozessor. Gängige XSLT-Prozessoren sind u.a.:
- Xalan (Xalan-J)
- Apache Xalan C++
- SAXON
- Sablotron
Beispiel Notizbuch
Mein Outlook-Notizbuch hatte ich in EverNote importiert, um daraus ein irgendwie brauchbares XML-Standatdformat zu erzeugen, z.B. RSS oder Atom...
Eingabe ist die mit EverNote erstellte Datei EverNote_v2.xml:
<evernote> <notes> <note name="title" content_date="2005/03/30 15:30:45" created="2008/04/07 19:55:01" id="[FA1234-5678-45454554]"> <content type="html"> <div>...html content...</div> </content> <contentplain> ....plain ascii content </contentplain> </note> </notes> </evernote>
Zur Transformation in Atom habe ich das XSL-Sheet evernote2atom.xsl entwickelt:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/2005/Atom" version="1.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" cdata-section-elements="content" /> <xsl:template match="/EVERNOTE"> <xsl:text> </xsl:text><feed> <title>Notizbuch</title> <subtitle>Ehemalige Outlook-Notizen</subtitle> <link href="http://example.org/feed/" rel="self"/> <link href="http://example.org/"/> <xsl:variable name="D" select="@date" /> <updated> <xsl:value-of select="concat(substring($D,1,4),'-',substring($D,6,2),'-',substring($D,9,2),'T',substring($D,12,8),'Z')" /> </updated> <author> <name>Dietrich Kracht</name> <email>dietrich@kr8.de</email> </author> <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> <xsl:apply-templates select="//NOTE"/> </feed> </xsl:template> <xsl:template match="/EVERNOTE/NOTES/NOTE"> <entry> <title> <xsl:value-of select="@name"/> </title> <link href="http://localhost/wordpress/?p=33"/> <id> <xsl:value-of select="@id" /> </id> <xsl:variable name="C" select="@content_date" /> <updated> <xsl:value-of select="concat(substring($C,1,4),'-',substring($C,6,2),'-',substring($C,9,2),'T',substring($C,12,8),'Z')" /> </updated> <xsl:variable name="D" select="@created" /> <published> <xsl:value-of select="concat(substring($D,1,4),'-',substring($D,6,2),'-',substring($D,9,2),'T',substring($D,12,8),'Z')" /> </published> <category term="Notizbuch" /> <author> <name>Dietrich Kracht</name> </author> <content type="xhtml"> <xsl:value-of select="CONTENT" /> </content> <content2> <xsl:value-of select="CONTENTPLAIN"/> </content2> </entry> </xsl:template> </xsl:stylesheet>
Aufruf des XSLT-Prozessors Xalan:
java org.apache.xalan.xslt.Process -IN EverNote_v2.xml -XSL evernote2atom.xsl -OUT NotizbuchAtom.xml
Generiert als Ergebnis die Atom-Datei NotizbuchAtom.xml:
<?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Notizbuch</title> <subtitle>Ehemalige Outlook-Notizen</subtitle> <link rel="self" href="http://example.org/feed/"/> <link href="http://example.org/"/> <updated>2008-03-31T18:56:03Z</updated> <author> <name>Dietrich Kracht</name> <email>dietrich@kr8.de</email> </author> <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> <entry> <title>Zählspiel nach Stableford</title> <link href="http://localhost/wordpress/?p=33"/> <id>[FA797DE2-6761-4369-B0C5E5FD4B3D1D86]</id> <updated>2005-03-05T22:14:45Z</updated> <published>2008-03-31T16:21:11Z</published> <category term="Notizbuch"/> <author> <name>Dietrich Kracht</name> </author> <content type="xhtml"><![CDATA[<DIV>Zählspiel nach Stableford<br /><br />Vorgabe 0 18 36 54<br />Par 2 3 4 5 <br />1 über Par 1 2 3 4 <br />2 über Par 0 1 2 3<br />3 über Par 0 0 1 2<br />4 über Par 0 0 0 1</DIV>]]> </content> <content2>Zählspiel nach Stableford Vorgabe 0 18 36 54 Par 2 3 4 5 1 über Par 1 2 3 4 2 über Par 0 1 2 3 3 über Par 0 0 1 2 4 über Par 0 0 0 1 Open source Outlook message >> </content2> </entry> ... </feed>
Weitere Beispiele
-- Dkracht 13:07, 12 April 2009 (CEST)