XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 3rd, 2010, 02:04 PM
daniboi81 daniboi81 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2010
Posts: 1 daniboi81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 11 sec
Reputation 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>
</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>

Reply With Quote
  #2  
Old December 7th, 2010, 02:28 AM
xml-profi xml-profi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 190 xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 7 h 8 m 25 sec
Reputation Power: 48
Send a message via ICQ to xml-profi
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > 2 column HTML table using XSLT

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap