#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    2 column HTML table using XSLT


    I'm hoping I can get some assistance with an XML transformation

    Given the below XML file

    Code:
    <?xml version="1.0" ?>
    <?xml-stylesheet type="text/xsl" href="https://domain.name.com/db/bfunjbizz?act=dbpage^pagename=billboard2.xsl"?>
    <qdbapi>
    	<action>API_DoQuery</action>
    	<errcode>0</errcode>
    	<errtext>No error</errtext>
    <dbinfo>
    <name>eCard</name>
    <desc></desc>
    </dbinfo>
    <variables>
    </variables>
    <chdbids>
    </chdbids>
      <record>
        <ecard_url>https://devwww20.domain.com/test.html</ecard_url>
        <recipient_name>John Doe</recipient_name>
      </record>
      <record>
        <ecard_url>https://devwww20.domain.com/test1.html</ecard_url>
        <recipient_name>Jane Smith</recipient_name>
      </record>
      <record>
        <ecard_url>https://devwww20.domain.com/test2.html</ecard_url>
        <recipient_name>Emily Test</recipient_name>
      </record>
      <record>
        <ecard_url>https://devwww20.domain.com/test3.html</ecard_url>
        <recipient_name>Bugs Bunny</recipient_name>
      </record>
    </qdbapi>
    I want to be able to transform this into a two column HTML table so that the results look like

    Code:
    <table>
      <tr>
        <td><A href="https://devwww20.domain.com/test.html">John Doe</a></td>
        <td><A href="https://devwww20.domain.com/test1.html">Jane Smith</a></td>
      </tr>
      <tr>
        <td><A href="https://devwww20.domain.com/test2.html">Emily Test</a></td>
        <td><A href="https://devwww20.domain.com/test3.html">Buggs Bunny</a></td>
      </tr>
    </table>
    I've searched for a number of XSL examples, and the closest one I could find is the following. The below will render one column of every 2nd node; however I'm really trying to accomplish the above HTML rendering given the XML sample provided at the very top...Any thoughts/suggestions would be appreciated.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
    <xsl:variable name="newline">
      <xsl:text>&#xa;</xsl:text>
    </xsl:variable>
    <xsl:template match="/">
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title></title>
      <link rel="stylesheet" type="text/css" href="http://www.domain.com/template_styles_navy.css"/>
    </head>
    
      <xsl:choose>
      <xsl:when test="qdbapi/record">
    
      <table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#e6e6e6">
        <p align="justify" class="space">
        <xsl:for-each select="qdbapi/record[position() mod 2 = 1]">
          <xsl:variable name="pos" select="position()-1" />
          <tr valign="top">
        	<td width="50%" class="space" style="font-size: small;">
    	  <a target="_blank">
    	    <xsl:attribute name="href">
    	      <xsl:value-of select="ecard_url" />
    	    </xsl:attribute>
    	    <xsl:value-of select="recipient_name" />
    	  </a>
    	</td>
    	<!-- conditional to test if next record in 2-pair series exists, if it does, render -->
    	<td width="50%" class="space" style="font-size: small;">
    	  <a target="_blank">
    	    <xsl:attribute name="href">
    	      <xsl:value-of select="card_url[position() = ($pos * 2) + 1]" />
    	    </xsl:attribute>
    	    <xsl:value-of select="recipient_name[position() = ($pos * 2) + 1]" />
    	  </a>
    	
    	</td>
    
          </tr>
        </xsl:for-each> 
        </p>
      </table>
      </xsl:when>
      <xsl:otherwise>
        <!-- SHOULD ANYTHING BE DISPLAYED WHEN NO RECORDS ARE FOUND -->
      </xsl:otherwise>
      </xsl:choose>
    <body>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    Not use xsl:for-each
    make a template is the better way
    for testing i modify the xml source
    make numbers by name
    Code:
    <?xml version="1.0"?>
    <!--<?xml-stylesheet type="text/xsl" href="https://domain.name.com/db/bfunjbizz?act=dbpage^pagename=billboard2.xsl"?>
    -->
    <qdbapi>
    	<action>API_DoQuery</action>
    	<errcode>0</errcode>
    	<errtext>No error</errtext>
    	<dbinfo>
    		<name>eCard</name>
    		<desc></desc>
    	</dbinfo>
    	<variables>
    	</variables>
    	<chdbids>
    	</chdbids>
    	<record>
    		<ecard_url>https://devwww20.domain.com/test.html</ecard_url>
    		<recipient_name>John Doe1</recipient_name>
    	</record>
    	<record>
    		<ecard_url>https://devwww20.domain.com/test1.html</ecard_url>
    		<recipient_name>Jane Smith2</recipient_name>
    	</record>
    	<record>
    		<ecard_url>https://devwww20.domain.com/test2.html</ecard_url>
    		<recipient_name>Emily Test1</recipient_name>
    	</record>
    	<record>
    		<ecard_url>https://devwww20.domain.com/test3.html</ecard_url>
    		<recipient_name>Bugs Bunny2</recipient_name>
    	</record>
    </qdbapi>

    xsl:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="html" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
    	<xsl:template match="/">
    		<html>
    			<head>
    				<!--
    				<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    				-->
    				<title></title>
    
    				<link rel="stylesheet" type="text/css" href="http://www.domain.com/template_styles_navy.css"/>
    
    				<style type="text/css">
    					<!--
    					/* ... defined css ... */
    					-->
    table
    {
    /*border-collapse:collapse;*/
    
    }
    table,th,td
    {
    border: 2px solid black;
    width:100%;
    margin:10px;
    }
    td
    {
    width:50%;
    background-color:#e6e6e6;
    padding:30px;
    text-align:center;
    }
    			</style>
    			</head>
    			<body>
    
    				<xsl:choose>
    					<xsl:when test="not(qdbapi/record)">
    						<!-- SHOULD ANYTHING BE DISPLAYED WHEN NO RECORDS ARE FOUND -->
    						<p>NO RECORDS ARE FOUND</p>
    					</xsl:when>
    					<xsl:otherwise>
    						<xsl:apply-templates select="qdbapi"/>
    					</xsl:otherwise>
    				</xsl:choose>
    			</body>
    		</html>
    	</xsl:template>
    	<xsl:template match="qdbapi">
    		<table>
    			<xsl:apply-templates select="record"/>
    		</table>
    	</xsl:template>
    
    	<xsl:template match="record">
    		<xsl:if test="position() mod 2 = 1">
    			<tr>
    				<td>
    					<xsl:attribute name="class">
    						<xsl:text>space</xsl:text>
    					</xsl:attribute>
    					<xsl:element name="a">
    						<xsl:attribute name="href">
    							<xsl:value-of select="ecard_url"/>
    						</xsl:attribute>
    						<xsl:attribute name="target">
    							<xsl:text>_blank</xsl:text>
    						</xsl:attribute>
    
    						<xsl:value-of select="recipient_name"/>
    					</xsl:element>
    				</td>
    
    				<xsl:call-template name="record">
    					<xsl:with-param name="kn" select="following::record"/>
    				</xsl:call-template>
    			</tr>
    		</xsl:if>
    	</xsl:template>
    
    	<xsl:template name="record">
    		<xsl:param name="kn"/>
    		<td>
    			<xsl:attribute name="class">
    				<xsl:text>space</xsl:text>
    			</xsl:attribute>
    			<xsl:element name="a">
    				<xsl:attribute name="href">
    					<xsl:value-of select="$kn/ecard_url"/>
    				</xsl:attribute>
    				<xsl:attribute name="target">
    					<xsl:text>_blank</xsl:text>
    				</xsl:attribute>
    
    				<xsl:value-of select="$kn/recipient_name"/>
    			</xsl:element>
    		</td>
    	</xsl:template>
    </xsl:stylesheet>
    result when record defined
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="http://www.domain.com/template_styles_navy.css">
        <style type="text/css">
    					table
    {
    /*border-collapse:collapse;*/
    
    }
    table,th,td
    {
    border: 2px solid black;
    width:100%;
    margin:10px;
    }
    td
    {
    width:50%;
    background-color:#e6e6e6;
    padding:30px;
    
    text-align:center;
    }
        </style>
      </head>
      <body>
        <table>
          <tr>
            <td class="space">
              <a href="https://devwww20.domain.com/test.html" target="_blank">John Doe1</a>
            </td>
            <td class="space">
              <a href="https://devwww20.domain.com/test1.html" target="_blank">Jane Smith2</a>
            </td>
          </tr>
          <tr>
            <td class="space">
              <a href="https://devwww20.domain.com/test2.html" target="_blank">Emily Test1</a>
            </td>
            <td class="space">
              <a href="https://devwww20.domain.com/test3.html" target="_blank">Bugs Bunny2</a>
            </td>
          </tr>
        </table>
      </body>
    </html>
    record not defined
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="http://www.domain.com/template_styles_navy.css">
        <style type="text/css">
    					
    table
    {
    /*border-collapse:collapse;*/
    
    }
    table,th,td
    {
    border: 2px solid black;
    width:100%;
    margin:10px;
    }
    td
    {
    width:50%;
    background-color:#e6e6e6;
    padding:30px;
    text-align:center;
    }
    			
        </style>
      </head>
      <body>
        <p>NO RECORDS ARE FOUND</p>
      </body>
    </html>
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage

IMN logo majestic logo threadwatch logo seochat tools logo