Skip to content

Instantly share code, notes, and snippets.

@miklund
Last active January 15, 2016 19:22
Show Gist options
  • Save miklund/d8928eee2c2fba80e02e to your computer and use it in GitHub Desktop.
Save miklund/d8928eee2c2fba80e02e to your computer and use it in GitHub Desktop.
2008-12-15 Xml, future of the past
# Title: Xml, future of the past
# Author: Mikael Lundin
# Link: http://blog.mikaellundin.name/2008/12/15/xml-future-of-the-past.html
<!ELEMENT bookstore (authors,books)>
<!-- AUTHORS -->
<!ELEMENT authors (author*)>
<!ELEMENT author (firstname,lastname)>
<!ATTLIST author id ID #REQUIRED>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!-- BOOKS -->
<!ELEMENT books (book*)>
<!ELEMENT book (authors-ref,price,plot)>
<!ATTLIST book isbn ID #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT authors-ref EMPTY>
<!ATTLIST authors-ref values IDREFS #REQUIRED>
<!ELEMENT price EMPTY>
<!ATTLIST price sek CDATA #IMPLIED>
<!ATTLIST price usd CDATA #IMPLIED>
<!ATTLIST price eur CDATA #IMPLIED>
<!ELEMENT plot (#PCDATA)>
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<bookstore>
<authors>
<author id="janeausten">
<firstname>Jane</firstname>
<lastname>Austen</lastname>
</author>
</authors>
<books>
<book isbn="ISBN-9781853260001">
<title>Pride and Prejudice</title>
<authors-ref values="janeausten" />
<price sek="79" />
<plot><![CDATA[Pride and Prejudice is the story of Mr and Mrs Bennet (minor gentry), their five daughters, and the various romantic adventures at their Hertfordshire residence of Longbourn....]]></plot>
</book>
</books>
</bookstore>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://mint.litemedia.se" targetNamespace="http://mint.litemedia.se" elementFormDefault="qualified">
<!-- ROOT ELEMENT -->
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element name="author" type="author" maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="book" maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!-- KEYS -->
<xs:keyref name="author-ref" refer="author-id">
<xs:selector xpath="books/book/author-ref"/>
<xs:field xpath="@values"/>
</xs:keyref>
<xs:unique name="author-id">
<xs:selector xpath="authors/author"/>
<xs:field xpath="@id"/>
</xs:unique>
<xs:unique name="book-isbn">
<xs:selector xpath="books/book"/>
<xs:field xpath="@isbn"/>
</xs:unique>
</xs:element>
<!-- AUTHOR TYPE -->
<xs:complexType name="author">
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
<!-- BOOK TYPE -->
<xs:complexType name="book">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="authors-ref">
<xs:complexType>
<xs:attribute name="values" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="price">
<xs:complexType>
<xs:attribute name="sek" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
<xs:element name="plot">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="isbn">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="ISBN-[\d]*"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:mint="http://mint.litemedia.se">
<xsl:output encoding="utf-8" indent="yes" method="xhtml" />
<xsl:output doctype-system="http://www.w3c.org/tr/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3c//DTD HTML 4.01//EN"/>
<xsl:template match="/">
<html>
<head>
<title>Bookstore</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<h1>Bookstore</h1>
<ul class="books">
<xsl:apply-templates select="/mint:bookstore/mint:books/*" />
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="mint:book">
<li class="book">
<h2><xsl:value-of select="mint:title"/></h2>
<em>by</em><xsl:value-of select="id(mint:authors-ref/@values)" />
<p>
Plot: <xsl:value-of select="mint:plot" />
</p>
</li>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment