Die Datei hallo.xml enthält folgendes XML-Dokument:
<?xml version="1.0" encoding="iso-8859-1"?> <hallo>Hallo, Welt!</hallo>
Dazu gehört das Stylesheet hallo.xsl:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>Ruby XSLT-Demo</title> </head> <body> <p><xsl:value-of select="hallo"/></p> </body> </html> </xsl:template> </xsl:stylesheet>
Für die Umsetzung wird das Stylesheet mit den zugehörigen Daten verknüpft:
ruby xslt.rb hallo.xsl hallo.xml > hallo.html # ergibt ein HTML-Dokument hallo.html <html> <head> <title>Ruby XSLT-Demo</title> </head> <body> <p>Hallo, Welt!</p> </body> </html>
XSLT kann auch in eigenen Programmen verwendet werden, wie das folgende Beispiel zeigt:
require "xslt.rb"
xsl = File.readlines("hallo.xsl").to_s
xml = File.readlines("hallo.xml").to_s
stylesheet = XSLT::Stylesheet.new(xsl, [])
result = stylesheet.apply(xml)
p "---"
p result[0]
# liefert
<html>
<head>
<title>Ruby XSLT-Demo</title>
</head>
<body>
<p>Hallo, Welt!</p>
</body>
</html>
"---"
"\n<html>\n<head>\n<title>Ruby XSLT-Demo
</title>\n</head>\n<body>\n <p>"