...

View Full Version : Join in XSLT: too many copies



carota
05-03-2004, 01:40 PM
I want to write this query in XSLT:
"copy all the books which have at least two authors with the same <last> but with different <first>" The problem is that I can't obtain just a single copy for each book, but I obtain as many copy as the matching authors.
How can I solve this?

It should transform this:


<?xml version="1.0" encoding="UTF-8"?>
<bib>
<book year="1999">
<title>A title</title>
<author>
<last>Smith</last>
<first>John</first>
</author>
</book>
<book year="2003">
<title>Another title</title>
<author>
<last>Lastname</last>
<first>Mark</first>
</author>
<author>
<last>Smith</last>
<first>John</first>
</author>
<author>
<last>Smith</last>
<first>Mary</first>
</author>
<author>
<last>Smith</last>
<first>Jack</first>
</author>
</book>
<book year="2003">
<title>The third title</title>
<author>
<last>Smith</last>
<first>John</first>
</author>
<author>
<last>Doe</last>
<first>Paul</first>
</author>
</book>
</bib>

into this:


<?xml version="1.0" encoding="UTF-8"?>
<bib>
<book year="2003">
<title>Another title</title>
<author>
<last>Lastname</last>
<first>Mark</first>
</author>
<author>
<last>Smith</last>
<first>John</first>
</author>
<author>
<last>Smith</last>
<first>Mary</first>
</author>
<author>
<last>Smith</last>
<first>Jack</first>
</author>
</book>
</bib>


I have written this code, but it creates too many copies of the same book:


<?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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<bib>
<xsl:apply-templates/>
</bib>
</xsl:template>
<xsl:template match="bib/book">
<xsl:variable name="contesto" select="."/>
<xsl:for-each select="author">
<xsl:variable name="first1" select="first/text()"/>
<xsl:variable name="last1" select="last/text()"/>
<xsl:for-each select="../author">
<xsl:variable name="first2" select="first/text()"/>
<xsl:variable name="last2" select="last/text()"/>
<xsl:if test="$first1!=$first2 and $last1=$last2">
<xsl:copy-of select="$contesto"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum