Difference between revisions of "XSLT"

From Dietrich Blog (Strato)
Jump to: navigation, search
(Beispiel Notizbuch)
Line 50: Line 50:
 
Mein Outlook-[[Notizbuch]] hatte ich in [[EverNote]] importiert, um daraus ein irgendwie brauchbares XML-Standatdformat zu erzeugen, z.B. [[RSS]] oder [[Atom]]...
 
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 XML-Datei:
+
Eingabe ist die mit [[EverNote]] erstellte Datei '''EverNote_v2.xml''':
 
<pre>
 
<pre>
 
<evernote>
 
<evernote>
Line 66: Line 66:
 
</pre>
 
</pre>
  
Zur Transformation in [[Atom]] habe ich folgendes XSL-Sheet entwickelt:
+
Zur Transformation in [[Atom]] habe ich das XSL-Sheet '''evernote2atom.xsl''' entwickelt:
 
<pre>
 
<pre>
 
<?xml version="1.0" encoding="iso-8859-1"?>
 
<?xml version="1.0" encoding="iso-8859-1"?>

Revision as of 19:49, 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 eine Atom-Datei:


Weitere Beispiele



-- Dkracht 13:07, 12 April 2009 (CEST)