2012-03-23 5 views
1

Ich habe ein XML-Dokument Informationen enthalten, Kontakt, wie unten zu sehen:XSLT nicht alle Anzeige der Ergebnisse

<contact type="individual"> 
    <firstname>Some</firstname> 
    <surname>Guy</surname> 
    <organisation> 
     <name>London School of Espionage</name> 
    </organisation> 
    <address> 
     <line1>Houghton St</line1> 
     <cityortown>London</cityortown> 
     <postalcode>WC2A 2AE</postalcode> 
     <country>UK</country> 
     </address> 
    <telephone prefix="+44" type="work"> 
     <areacode>020</areacode> 
     <number>71239876</number> 
    </telephone> 
    <telephone prefix="+44" type="mobile"> 
     <areacode>07123</areacode> 
     <number>543098</number> 
    </telephone> 
    <email type="work">[email protected]</email> 
    <email type="personal">[email protected]</email> 
    <fax prefix="+44" type="work"> 
     <areacode>020</areacode> 
     <number>780</number> 
    </fax> 
    <website>www.espionage.co.uk</website> 
</contact 

> 

Ich habe eine XSL-Vorlage, die alle Daten in einer Tabelle anzeigen soll, sondern nur die erste E-Mail-Adresse angezeigt wird . Bitte könnte jemand raten, was ich falsch mache:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"> 
<xsl:template match="/"> 
<html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="contacts.css"/> 
    </head> 
    <body> 

     <div id="main"> 
    <h1 align="center">XML Contact Book</h1> 
    <table> 
     <tr><th>Name</th> 
     <th>Organisation</th> 
     <th>Address</th> 
     <th>Email</th> 
     <th>Telephone</th> 
     <th>Fax</th> 
     <th>Website</th></tr> 
     <xsl:for-each select="contacts/contact/."> 
      <tr> 
       <td valign="bottom"><xsl:value-of select="firstname"/>&#160;<xsl:value-of select="surname"/></td> 
       <td valign="bottom"><xsl:value-of select="organisation/name"/></td> 
       <td valign="bottom" width="200px"><xsl:value-of select="address/line1"/><br/> 
        <xsl:value-of select="address/line2"/><br/> 
        <xsl:value-of select="address/line3"/><br/> 
        <xsl:value-of select="address/cityortown"/><br/> 
        <xsl:value-of select="address/countyorstate"/><br/> 
        <xsl:value-of select="address/postalcode"/><br/> 
        <xsl:value-of select="address/country"/><br/></td> 
       <!--creates a mailto: link for the email address contained in contacts.xml--> 
       <td valign="bottom"><a><xsl:attribute name="href">mailto:<xsl:value-of select="email"/></xsl:attribute><xsl:value-of select="email"/></a> 
        <br/><p><xsl:value-of select="email/@type"/>&#160;email</p></td> 
       <td valign="bottom"><p>Prefix: <xsl:value-of select="telephone/@prefix"/></p> <xsl:value-of select="telephone/areacode"/>&#160;<xsl:value-of select="telephone/number"/></td> 
       <td valign="bottom"><p>Prefix: <xsl:value-of select="fax/@prefix"/></p><xsl:value-of select="fax/areacode"/>&#160;<xsl:value-of select="fax/number"/></td> 
       <!--creates hyperlink to website listed in contact details--> 
       <td valign="bottom"><a><xsl:attribute name="href">http://<xsl:value-of select="website"/></xsl:attribute><xsl:value-of select="website"/></a></td> 
      <hr/> 
      </tr> 
     </xsl:for-each> 

    </table> 

     </div> 
    </body> 
</html> 
</xsl:template> 

Antwort

1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/contacts"> 
    <table> 
     <!-- apply templates to each contact --> 
      <xsl:apply-templates select="contact"/> 
    </table> 
</xsl:template> 

<xsl:template match="contact"> 
    <tr> 
     <!-- apply templates as required --> 
     <td> 
      <xsl:apply-templates select="firstname"/> 
     </td> 
     <td> 
      <xsl:apply-templates select="email"/> 
     </td> 
    </tr> 
</xsl:template> 

<!-- email template --> 
    <xsl:template match="email"> 
      <a href="mailto:{.}"><xsl:value-of select="."/></a> 
    </xsl:template> 

    <!-- add other templates, example --> 
    <xsl:template match="firstname"> 
      <xsl:value-of select="concat(.,'&#160;',following-sibling::surname)"/> 
    </xsl:template> 

</xsl:stylesheet> 
0

Sie sie als E-Mail verweisen möchten [1] und E-Mail [2]

2

Sie benötigen einen for-each zugreifen alle E-Mail-Adressen, genauso wie Sie alle Kontakte anzeigen müssen. So etwas wie dieses

<xsl:for-each select="email"> 
    <td valign="bottom"> 
    <a> 
     <xsl:attribute name="href">mailto:<xsl:value-of select="."/></xsl:attribute> 
     <xsl:value-of select="."/> 
    </a> 
    <br/> 
    <p> 
     <xsl:value-of select="@type"/>&#160;email 
    </p> 
    </td> 
</xsl:for-each> 
Verwandte Themen