XSLT不显示所有结果

时间:2012-03-23 22:58:17

标签: xml xslt

我有一个包含联系信息的XML文档,如下所示:

<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">gorgeousgeorge@lse.ac.uk</email>  
    <email type="personal">george123@gmail.com</email>
    <fax prefix="+44" type="work">
        <areacode>020</areacode>
        <number>78001234</number>
    </fax>
    <website>www.espionage.co.uk</website>
</contact

>

我有一个XSL模板,它应该显示表中的所有信息,但只显示第一个电子邮件地址。请有人可以告诉我我做错了什么:

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

3 个答案:

答案 0 :(得分:2)

您需要for-each才能访问所有电子邮件地址,方法与显示所有联系人的方式相同。像这样的东西

<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>

答案 1 :(得分: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>

答案 2 :(得分:0)

您希望将它们作为电子邮件[1]和电子邮件[2]

引用