Skip to content

Instantly share code, notes, and snippets.

@tdack
Created September 27, 2012 04:39
Show Gist options
  • Save tdack/3792195 to your computer and use it in GitHub Desktop.
Save tdack/3792195 to your computer and use it in GitHub Desktop.
XML -> HTML using XSL in PHP
The parse-xml.php file uses the opf.xsl style sheet to read the book.opf XML file and output some minimal html for he description of the book.
The html produced is the contents of the <dc:description> tag surrounded by a <div>.
<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="uuid_id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier opf:scheme="calibre" id="calibre_id">224</dc:identifier>
<dc:identifier opf:scheme="uuid" id="uuid_id">208c182b-043a-4e0e-99b9-696acd5c522d</dc:identifier>
<dc:title>Total Recall</dc:title>
<dc:creator opf:file-as="Anthony, Piers" opf:role="aut">Piers Anthony</dc:creator>
<dc:contributor opf:file-as="calibre" opf:role="bkp">calibre (0.8.55) [http://calibre-ebook.com]</dc:contributor>
<dc:date>1989-09-14T14:00:00+00:00</dc:date>
<dc:description>&lt;p class="description"&gt;A novelization of the movie about a secret agent on Mars searching for his past. Also use But What of Earth? (Tor, 1989).&lt;/p&gt;</dc:description>
<dc:publisher>W. Morrow</dc:publisher>
<dc:identifier opf:scheme="AMAZON">B00446K32Q</dc:identifier>
<dc:identifier opf:scheme="GOOGLE">YG36GRajANgC</dc:identifier>
<dc:identifier opf:scheme="ISBN">9780688052096</dc:identifier>
<dc:language>eng</dc:language>
<dc:subject>Science Fiction</dc:subject>
<dc:subject>Fiction</dc:subject>
<dc:subject>General</dc:subject>
<dc:subject>Mars (Planet)</dc:subject>
<meta content="{&quot;Piers Anthony&quot;: &quot;&quot;}" name="calibre:author_link_map"/>
<meta content="6.0" name="calibre:rating"/>
<meta content="2012-06-11T12:15:01+00:00" name="calibre:timestamp"/>
<meta content="Total Recall" name="calibre:title_sort"/>
</metadata>
<guide>
<reference href="Total Recall - Piers Anthony.jpg" type="cover" title="Cover"/>
</guide>
</package>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/">
<xsl:output method="text" omit-xml-declaration="yes" standalone="no"/>
<xsl:template match="/">
<div class="readme">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="dc:*"></xsl:template>
<xsl:template match="dc:description">
<div><xsl:apply-templates disable-output-escaping="no"/></div>
</xsl:template>
</xsl:stylesheet>
<?php
$parser = new XSLTProcessor();
$parser->importStylesheet(DomDocument::load("opf.xsl"));
$html_output = $parser->transformToXml(DomDocument::load("book.opf"));
?>
<?=$html_output?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment