Jump to content

Getting Multiple Nodes (if Exist) To Appear In One Table Cell Using Xslt


Recommended Posts

Hey all,Here's what I'm trying to accomplish: I would like to get multiple nodes (if they exist) inside the same table cell using XSLT.Here's my XML File:

<?xml version="1.0" encoding="ISO-8859-15"?><?xml-stylesheet type="text/xsl" href="test.xsl"?><contacts_list xmlns:xsi="http://www.w3.org/2001/XMLSchmea-in-stance"     xsi:schemaLocation="test.xsd">  <contact>    <name>      <first>Jane</first>      <middle></middle>      <last>Doe</last>    </name>    <address>      <street>1313 Mockingbird Ln.</street>      <street2></street2>      <city>Transylvannia</city>      <state>UT</state>      <zip>84084</zip>    </address>    <phone>      <home>8011234567</home>      <mobile></mobile>      <work></work>    </phone>    <email>yourname@anywhere.com</email>    <email>test@someplace.net</email>  </contact></contacts_list>

and here is my XSLT file:

<?xml version="1.0"?><xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="/">  <html>  <head>  <title>Test Document</title>  <style>  * {    margin: 0;    padding: 0;    text-align: center;    font-size: 12pt;    color: black;    background-color: transparent;  }  table {    margin: 0 auto;    border: 2px solid black;  }  th {    background-color: gray;    color: white;    padding: 3px 5px;    border: 1px solid black;  }  td {    border: 1px solid black;    font-size: 10pt;    padding: 3px;  }  h1 {    font-size: 2.5em;    color: green;    background-color: transparent;  }  </style>  </head>  <body>  <h1>Testing</h1>  <table>  <thead>    <tr>      <th>First</th>      <th>Middle</th>      <th>Last</th>      <th>Street</th>      <th>Street 2</th>      <th>City</th>      <th>State</th>      <th>Zip</th>      <th>Home</th>      <th>Mobile</th>      <th>Work</th>      <th>Email</th>          </tr>  </thead>  <tfoot>  </tfoot>  <tbody>    <tr>      <xsl:for-each select="contacts_list/contact">      <xsl:sort select="name/last" order="ascending" />      <xsl:sort select="name/first" order="ascending" />      <td><xsl:value-of select="name/first"/></td>      <td><xsl:value-of select="name/middle"/></td>      <td><xsl:value-of select="name/last"/></td>      <td><xsl:value-of select="address/street"/></td>      <td><xsl:value-of select="address/street2"/></td>      <td><xsl:value-of select="address/city"/></td>      <td><xsl:value-of select="address/state"/></td>      <td><xsl:value-of select="address/zip"/></td>      <td><xsl:value-of select="phone/home"/></td>      <td><xsl:value-of select="phone/mobile"/></td>      <td><xsl:value-of select="phone/work"/></td>      <xsl:for-each select="email">      <td><xsl:value-of select="email"/><br /></td>      </xsl:for-each>      </xsl:for-each>    </tr>  </tbody>  </table>  </body>  </html>  </xsl:template></xsl:stylesheet>

I can get everything else working except that last table cell. I would like to see all possible email addresses in that same cell and on a new line. What am I doing wrong? I've searched a few XSLT tutorials on line but haven't been able to figure it out myself. Is there a better way to do this?...any help you can offer will be greatly appreciated. I don't know if it has something to do with this one "http://w3schools.invisionzone.com/index.php?s=&showtopic=9130&view=findpost&p=49438" . Seems like a scripting solution?

Link to comment
Share on other sites

thanks, that almost fixed it and got me more on the right track. I had to modify it a little to get my desired result (the above suggestion was adding another cell on to the end of the table row for each instance of "email"). Here's what I did to fix it:

  <td>    <xsl:for-each select="email">      <xsl:value-of select="."/><br />    </xsl:for-each>  </td>

To help me understand the suggested change, I'd like some clarification if at all possible. I guess I was on the right track with the HTML portion, but don't really understand what this tag does:

<xsl:value-of select="."/>

Is that a wild card meaning to select all instances of the "email" tag?

Link to comment
Share on other sites

'.' = the current nodeWhen you say <for-each select="email"><value-of select="email">its looking for <email><email/></email>, in other words, a child node called email of the current node (.) called emailyou just wanted the current nodeyou could also usevalue-of select="current()"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...