【问题标题】:XSLT not displaying all resultsXSLT 不显示所有结果
【发布时间】:2026-02-14 12:35:02
【问题描述】:

我有一个包含联系信息的 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>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    您需要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>
    

    【讨论】:

      【解决方案2】:
      <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>
      

      【讨论】:

        【解决方案3】:

        您需要将它们引用为电子邮件[1] 和电子邮件[2]

        【讨论】:

          最近更新 更多