Question

I'm making a basic Contact List Web App with three pages - a Contact List page that displays all contacts in the DB (stored in an XML file), a Contact View page (that is a read-only page which displays Contact Information), and a New Contact page (that allows you to make a new contact or edit information of a previously existing contact).

My issue is with the Contact View page. Each Contact has an ID, and this ID is passed through the URL (i.e. contactViewer?id=mk). Using this ID, I was hoping that using a simple xsl for-each that checks the URL request (saved in an xsl variable) to pick which contact from the DB to display would work. Unfortunately, after adding the "for-each" nothing displays on the page and I can't figure out where it's going wrong.

This is the XSL page for the ContactView.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="url"/>
    <xsl:variable name="currentID" select="substring-after($url, 'id=')"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>Contact Database - Contact Viewer</title>
                <style>
                    h1 {color:white; background-color:black; border-style:solid;
                    border-color: #981b1e; padding-left:10px; font-weight:bold;}
                </style>
            </head>
            <body>
                <h1>Contact Viewer</h1>
                <xsl:value-of select="$currentID"/>
                <xsl:for-each select="contactdatabase/contact/id[$currentID]">
                    <table cellspacing="5" border="3">
                        <tr  height="60">
                            <td width="70px">
                                <xsl:value-of select="contactdatabase/contact/firstName"/>
                            </td>
                            <td width="100px">
                                <xsl:value-of select= "contactdatabase/contact/lastName"/>
                            </td>
                            <td width="120px">
                                <xsl:value-of select="contactdatabase/contact/firstName"/>
                            </td>
                            <td width="90px">
                                <xsl:value-of select="contactdatabase/contact/firstName"/>
                            </td>
                            <td width="35px">
                                <xsl:value-of select="contactdatabase/contact/state"/>
                            </td>
                            <td width="44px">
                                <xsl:value-of select="contactdatabase/contact/zipcode"/>
                            </td>
                            <td width="60px">
                                <xsl:value-of select="contactdatabase/contact/country"/>
                            </td>
                            <td width="80">
                                <xsl:value-of select="contactdatabase/contact/email"/> <br></br>
                                <xsl:value-of select="contactdatabase/contact/home"/> <br></br>
                                <xsl:value-of select="contactdatabase/contact/mail"/>
                            </td>
                            <td width="80">
                                <xsl:value-of select="contactdatabase/contact/phonenum"/>
                            </td>
                            <td width="180">
                                <xsl:value-of select="contactdatabase/contact/emailaddress"/>
                            </td>
                            <td width="120px">
                                <a href="contactViewer?contactid={id}">view</a> / edit / delete
                            </td>
                        </tr>
                    </table>
                </xsl:for-each>
                <a href="contactList">Contact List</a>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

The XML database is set up as

<contactdatabase>
    <contact>
        <firstName />
        <lastName />
        <address />
        <city />
        <state />
        <zipcode />
        <country />
        <email />
        <phone />
        <mail />
        <phonenum />
        <emailaddress />
        <comment />
        <id />
    </contact>
</contactdatabase>
Was it helpful?

Solution

The correct XPath expression to select the required contact element is contactdatabase/contact[id = $currentID].

Rather than a for-each I would use apply-templates to format the contact element with the given ID, and write a separate template to match contact elements. It would look like this.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:param name="url"/>
  <xsl:variable name="currentID" select="substring-after($url, 'id=')"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>Contact Database - Contact Viewer</title>
        <style>
          h1 {
            color:white;
            background-color:black;
            border-style:solid;
            border-color:#981b1e;
            padding-left:10px;
            font-weight:bold;
          }
        </style>
      </head>
      <body>
        <h1>Contact Viewer</h1>
        <xsl:value-of select="$currentID"/>
        <xsl:apply-templates select="contactdatabase/contact[id = $currentID]"/>
        <a href="contactList">Contact List</a>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="contact">

      <table cellspacing="5" border="3">
        <tr height="60">
          <td width="70px">
            <xsl:value-of select="firstName"/>
          </td>
          <td width="100px">
            <xsl:value-of select= "lastName"/>
          </td>
          <td width="120px">
            <xsl:value-of select="firstName"/>
          </td>
          <td width="90px">
            <xsl:value-of select="firstName"/>
          </td>
          <td width="35px">
            <xsl:value-of select="state"/>
          </td>
          <td width="44px">
            <xsl:value-of select="zipcode"/>
          </td>
          <td width="60px">
            <xsl:value-of select="country"/>
          </td>
          <td width="80">
            <xsl:value-of select="email"/> <br></br>
            <xsl:value-of select="home"/> <br></br>
            <xsl:value-of select="mail"/>
          </td>
          <td width="80">
            <xsl:value-of select="phonenum"/>
          </td>
          <td width="180">
            <xsl:value-of select="emailaddress"/>
          </td>
          <td width="120px">
            <a href="contactViewer?contactid={id}">view</a> / edit / delete
          </td>
        </tr>
      </table>

  </xsl:template>

</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top