The sample presented is a simplified version of what I am trying to do.
Using XSLT, how do I compare data from nodes in different (non -child) xpaths? I want to compare each cd/artist with the dvd/director to see if they match (see samples below). There are multiple cd’s and dvd’s, but in this simplified example I only used one of each. I tried doing the comparison within one template, but I couldn’t access the dvd xpath. And I couldn’t access the dvd xpath by using a separate template. When using a separate template, I created a ‘compare’ template that I called (<xsl:call-template name="compare">). By access, I mean that I couldn’t print out the dvd/director’s value.
My Question: How do I access one xpath when accessing a different xpath? What I want to do is compare cd artist with dvd director to see if they match. And print out these values, if they match. What I can’t figure out is how to get the artist and director so I can compare them. My xslt does print the cd’s artist value, but I can’t print out the dvd/director’s value.
My samples follows:
The samples come from W3Schools. I modified them to illustrate my question.
Sample XSLT
<xsl:for-each select="catalog/cd">
<xsl:value-of select="@artist"/>
<xsl:for-each select="catalog/dvd">
<xsl:if test="@director = @artist">
<xsl:value-of select="@director"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
Sample XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd title="Unchain my heart" artist="Joe Cocker">
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
<dvd title="My DVD Movie" director="Joe Cocker">
<country>USA</country>
<company>WB</company>
<price>20.00</price>
<year>1999</year>
</dvd>
</catalog>
Thanks for your help in advance.
I think the larger issue is that you’re thinking in procedural terms, which is rarely the right way to approach a problem with XSLT. A better way is to first match
cdelements and then select thedvdelements whosedirectormatches theartist. Consider this stylesheet:The first template applies templates to all
dvdelements for which the following predicate evaluates to true:director=current()/artistOf course, as in your example, we’re just printing the name
Joe Cockertwice, which isn’t very useful.Note: This approach breaks down when there is more than one
cdelement with the sameartist. More complicated grouping strategies would address that case.