darkstarbg
Technical User
Hello, I'm trying to make a DVD catalog but I'm having problems extracting data from my XML file using the id values and a variable. I'm going to post the source files first to make clear exactly what I'm trying to achieve.
XML file:
<?xml version="1.0" encoding="windows-1251"?>
<?xml-stylesheet type="text/xsl" href="catalog_director_tests2.xsl"?>
<catalog>
<dvds>
<dvd id="1">
<title>????? ????????? ?? ????????</title>
<price>24.99$</price>
<director>1</director>
<year>2001</year>
<description>National Geographic Beyond the Movie The Lord of the Rings - The Fellowship of the Ring</description>
</dvd>
<dvd id="2">
<title>????????? ?? ???????? - ????????? ?????????????? ???????</title>
<price>64.99$</price>
<director>1</director>
<year>2001</year>
<description>Platinum Series Special Extended Edition Collector's Gift Set (2001)</description>
</dvd>
<dvd id="3">
<title>????????? ?? ???????? ?? ?????? - ???????</title>
<price>13.99$</price>
<director>1</director>
<year>2001</year>
<description>Secrets of Middle-Earth: Inside Tolkien's The Fellowship of the Rings</description>
</dvd>
<dvd id="4">
<title>??????????? ?? ????? ?? ?????? - ???????</title>
<price>13.99$</price>
<director>1</director>
<year>2004</year>
<description>Secrets of Middle-Earth: Inside Tolkien's The Return of the King </description>
</dvd>
<directors>
<director id="2">
<name>George Lucas</name>
</director>
<director id="1">
<name>Peter Jackson</name>
</director>
</directors>
</catalog>
XSL file:
<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="
<xsl:template match="/">
<xsl:for-each select="catalog/directors/director">
<xsl:if test="name = 'Peter Jackson'">
<xsl:variable name="dname" select="@id"/>
<xsl:value-of select="$dname"/>
</xsl:if>
</xsl:for-each>
<html>
<body>
<h2>catalog 2</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Price</th>
</tr>
<xsl:for-each select="catalog/dvds/dvd">
<xsl:sort select="price"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="@id"/></td>
</tr>
</xsl:for-each>
</table>
<h2>?????????</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>ID</th>
<th>???</th>
</tr>
<xsl:for-each select="catalog/directors/director">
<xsl:if test="name= 'Peter Jackson'">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="name"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The problem is that I can't use the $dname variable outside of the for-each block as it loses scope. I need it so that I can associate a movie with its director. How can I make this variable global? When I declare it otside xsl:template I recieve an error message: Keyword xsl:stylesheet may not contain xsl:for-each. How can I go arround this?
XML file:
<?xml version="1.0" encoding="windows-1251"?>
<?xml-stylesheet type="text/xsl" href="catalog_director_tests2.xsl"?>
<catalog>
<dvds>
<dvd id="1">
<title>????? ????????? ?? ????????</title>
<price>24.99$</price>
<director>1</director>
<year>2001</year>
<description>National Geographic Beyond the Movie The Lord of the Rings - The Fellowship of the Ring</description>
</dvd>
<dvd id="2">
<title>????????? ?? ???????? - ????????? ?????????????? ???????</title>
<price>64.99$</price>
<director>1</director>
<year>2001</year>
<description>Platinum Series Special Extended Edition Collector's Gift Set (2001)</description>
</dvd>
<dvd id="3">
<title>????????? ?? ???????? ?? ?????? - ???????</title>
<price>13.99$</price>
<director>1</director>
<year>2001</year>
<description>Secrets of Middle-Earth: Inside Tolkien's The Fellowship of the Rings</description>
</dvd>
<dvd id="4">
<title>??????????? ?? ????? ?? ?????? - ???????</title>
<price>13.99$</price>
<director>1</director>
<year>2004</year>
<description>Secrets of Middle-Earth: Inside Tolkien's The Return of the King </description>
</dvd>
<directors>
<director id="2">
<name>George Lucas</name>
</director>
<director id="1">
<name>Peter Jackson</name>
</director>
</directors>
</catalog>
XSL file:
<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="
<xsl:template match="/">
<xsl:for-each select="catalog/directors/director">
<xsl:if test="name = 'Peter Jackson'">
<xsl:variable name="dname" select="@id"/>
<xsl:value-of select="$dname"/>
</xsl:if>
</xsl:for-each>
<html>
<body>
<h2>catalog 2</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Price</th>
</tr>
<xsl:for-each select="catalog/dvds/dvd">
<xsl:sort select="price"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="@id"/></td>
</tr>
</xsl:for-each>
</table>
<h2>?????????</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>ID</th>
<th>???</th>
</tr>
<xsl:for-each select="catalog/directors/director">
<xsl:if test="name= 'Peter Jackson'">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="name"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The problem is that I can't use the $dname variable outside of the for-each block as it loses scope. I need it so that I can associate a movie with its director. How can I make this variable global? When I declare it otside xsl:template I recieve an error message: Keyword xsl:stylesheet may not contain xsl:for-each. How can I go arround this?