xml - Can't get XSL to Display Correctly with XSL Variables and XPATH -
i'm making basic contact list web app 3 pages - contact list page displays contacts in db (stored in xml file), contact view page (that read-only page displays contact information), , new contact page (that allows make new contact or edit information of existing contact).
my issue contact view page. each contact has id, , id passed through url (i.e. contactviewer?id=mk). using id, hoping using simple xsl for-each checks url request (saved in xsl variable) pick contact db display work. unfortunately, after adding "for-each" nothing displays on page , can't figure out it's going wrong.
this xsl page 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 set as
<contactdatabase> <contact> <firstname /> <lastname /> <address /> <city /> <state /> <zipcode /> <country /> <email /> <phone /> <mail /> <phonenum /> <emailaddress /> <comment /> <id /> </contact> </contactdatabase>
the correct xpath expression select required contact
element contactdatabase/contact[id = $currentid]
.
rather for-each
use apply-templates
format contact
element given id, , write separate template match contact
elements. 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>
Comments
Post a Comment