XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 20th, 2004, 09:04 PM
northern northern is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 northern User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
XSLT - Match parent element based on child element value

Hello, I'm very new to experimenting with XML and XSLT, so this is a newbie question...but I can't find the solution to this.

Given the xml:
PHP Code:
<catalog>
    <
mediatype>
        <
typename>Magazine</typename>
        <
title>Car Driver</title>
            <
info>
                <
publisher>Some Publisher</title>
                <
date>9-19-2004</date>
            </
info>
    </
mediatype>
    <
mediatype>
        <
typename>DVD</typename>
        <
title>The Godfather</title>
            <
info>
                <
publisher>Some Publisher 2</title>
                <
date>9-18-2004</date>
            </
info>
    </
mediatype>
    
.
.
.
.
.    
</
catalog


How would I select and display all titles whose publisher is "Some Publisher" ??
In the example above, it would display "Car & Driver" and not "The Godfather"
Thanks

Reply With Quote
  #2  
Old October 4th, 2004, 02:34 PM
nBIL nBIL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 14 nBIL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 12 m 6 sec
Reputation Power: 0
ok, first step is that there were some errors in the xml: publisher tag has wrong closing and ampersand is not encoded. Here is the xml I used instead...

Code:

<catalog>
    <mediatype>
        <typename>Magazine</typename>
        <title>Car & Driver</title>
            <info>
                <publisher>Some Publisher</publisher>
                <date>9-19-2004</date>
            </info>
    </mediatype>
    <mediatype>
        <typename>DVD</typename>
        <title>The Godfather</title>
            <info>
                <publisher>Some Publisher 2</publisher>
                <date>9-18-2004</date>
            </info>
    </mediatype>
    
  
</catalog> 



The xsl...

Code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
		
<xsl:output method="html" indent="yes" encoding="utf-8"/>

<xsl:template match="catalog/mediatype">

	<xsl:for-each select="info/publisher">
		Publisher: <xsl:value-of select="text()" /><br />
		Title: <xsl:value-of select="parent::node()/parent::node()/title" /><br />
	</xsl:for-each>

</xsl:template>

</xsl:stylesheet>


I'm not certain exactly what you wanted but the code above is my best guess. Using this solution you select every title and corresponding publisher. Maybe you wanted to select a specific publisher/title combo? That's easy enough to do too so just let me know if you need that instead. Hope it helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSLT - Match parent element based on child element value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT