Input XML:
Code:
<?xml version="1.0"?>
<library>
<book>
<title>PHP Cookbook</title>
<type>Reference</type>
</book>
<book featured="yes">
<title>Jam and the dodgers</title>
<type>Comedy</type>
</book>
<book>
<title>Matilda</title>
<type>Adventure</type>
</book>
</library>
Desired output:
Code:
<html><body>
<div class="featured">
<h1>Jam and the dodgers</h1>
</div>
<div class="others">
<div>
<h2>PHP Cookbook</h2>
</div>
<div>
<h2>Matilda</h2>
</div>
</div>
Basically I need to match for featured='yes' and others separately. I could use a
for-each and then
choose. But I'm trying to get my head around
template and
apply-templates. How would it be done using them?
This is what I'm trying at the moment, but the matches clearly aren't working.
xslt
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
<xsl:template match="/">
<div class="featured">
<xsl:apply-templates match="book[featured='yes']"/>
</div>
<div class="others">
<xsl:apply-templates match="book[featured!='yes']"/>
</div>
</xsl:template>
<xsl:template match="book[featured='yes']">
<h1><xsl:value-of select="title"/></h1>
</xsl:template>
<xsl:template match="book[featured!='yes']">
<div><h2><xsl:value-of select="title"/></h2></div>
</xsl:template>
</xsl:stylesheet>
Generates
Code:
<?xml version="1.0"?>
<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<div class="featured">Jam and the dodgersComedyMatildaAdventure</div><div class="others">Jam and the dodgersComedyMatildaAdventure</div>
Am I going about this the wrong way? Is there a better way to do this?
Many thanks in advance.