Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 10th, 2006, 08:33 AM
Authorized User
 
Join Date: Nov 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Match by attribute.

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.

 
Old November 10th, 2006, 08:47 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

There were quite a few mistakes in the XSLT but I think these may have ben copy/paste as what you showed would have produced very little.
This isa working version, I've removed the doctype stuff to simplify because with XHTML you need to create namespaced elements.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
    <xsl:template match="/">
    <html><head><title>Books</title></head><body>
        <div class="featured">
            <xsl:apply-templates select="library/book[@featured = 'yes']"/>
        </div>
        <div class="others">
            <xsl:apply-templates select="library/book[not(@featured = 'yes')]"/>
        </div>
    </body></html>
    </xsl:template>
    <xsl:template match="book[@featured = 'yes']">
        <h1><xsl:value-of select="title"/></h1>
    </xsl:template>
    <xsl:template match="book[not(@featured = 'yes')]">
        <div><h2><xsl:value-of select="title"/></h2></div>
    </xsl:template>
</xsl:stylesheet>

--

Joe (Microsoft MVP - XML)
 
Old November 10th, 2006, 08:57 AM
Authorized User
 
Join Date: Nov 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow, what a great place this is :)
Thanks joefawcett that worked perfectly.

Obviously I need to look into XPath more.

 
Old November 11th, 2006, 08:11 AM
Authorized User
 
Join Date: Nov 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK. I'm a bit confused.
I've just tried having a template matching
Code:
book[@featured = 'yes']
and
Code:
book[not(@featured = 'yes')]
and that works very well.

Then I tried having a template matching
Code:
book[@featured = 'yes']
and
Code:
book[@featured != 'yes']
The != one doesn't match.

Why is this?

 
Old November 11th, 2006, 08:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Because @featured != 'yes' needs an attribute called featured that doesn't equal 'yes'. As the elements don't have a featured attribute it doesn't not equal 'yes'. not(@featured = 'yes') means any situation except where there is a featured attribute with a value of 'yes'.

--

Joe (Microsoft MVP - XML)
 
Old November 11th, 2006, 09:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

@featured='yes' means "there is an attribute featured and it is equal to yes". @featured!=yes means "there is an attribute featured and it is not equal to yes". not(@featured=yes)" means "there is not an attribute featured that is equal to yes".

Hope this makes it more clear!

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 11th, 2006, 10:24 AM
Authorized User
 
Join Date: Nov 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ahh yes that does make things clearer.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
Match element based on attribute of child? transom XSLT 1 September 11th, 2005 03:26 PM
document(), attribute and name() match perropicante XSLT 8 November 17th, 2004 07:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.