|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
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 |
|
#2
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSLT - Match parent element based on child element value |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|