Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Join in XSLT: too many copies

    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:
    Code:
    <?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:

    Code:
    <?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:

    Code:
    <?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>
    Last edited by carota; 05-03-2004 at 05:13 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •