Jump to content

Placing Pictures From Xml File In Table In Row Wise Fashion.

Recommended Posts

hi guys I have created something in xslt. but my problem is same pic is selected in different column of the row.what i want is a row with 2 columns which has the images which are there in xml document and after the 2nd column the third image should be in second row 1st column and so on..... here is what i have created... plzzzz someone help me do this.....-------------------------XSL---------------------------------------------<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="assrec.xml" --><!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> <!ENTITY copy "©"> <!ENTITY reg "®"> <!ENTITY trade "™"> <!ENTITY mdash "—"> <!ENTITY ldquo "“"> <!ENTITY rdquo "”"> <!ENTITY pound "£"> <!ENTITY yen "¥"> <!ENTITY euro "€">]><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:template match="/"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>Untitled Document</title></head><body><table cellspacing="0px" cellpadding="1px" style="width:192px; border:0px; color:#999999;"><tr><th align='center' colspan="2" style=" background-image:url(images/menutop.gif); background-repeat:repeat; height:30px; color: #CCCCCC; font:Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;">Employee</th></tr><xsl:for-each select="ListItems/ListItem"><tr style="background-image: url(images/menumiddle.gif); background-repeat:repeat; width:192px; height:80px;"><td ><img src="{image/@href}" width="75" height="70" align="right" style="border: solid; border-color:#999999; border-width:2px; "/><br /> <xsl:value-of select="name"/></td><td><img src="{image/@href}" width="75" height="70" align="right" style="border: solid; border-color:#999999; border-width:2px; "/><br /> <xsl:value-of select="name"/></td></tr></xsl:for-each><tr><td colspan="2" style=" background-image:url(images/menubottom.gif); color:#000000">.</td></tr></table></body></html></xsl:template></xsl:stylesheet>------------------------------------------------------XML------------------------------------------------<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?><?xml:stylesheet type="text/xsl" href="assrec.xsl"?><ListItems> <ListItem> <image href="C:\wampserver 2\www\aafes test\my\by vickey\cdtest\images\member1.jpg"> </image> <name>anuj</name> </ListItem> <ListItem> <image href="C:\wampserver 2\www\aafes test\my\by vickey\cdtest\images\member2.jpg"> </image> <name>bb</name> </ListItem> <ListItem> <image href="C:\wampserver 2\www\aafes test\my\by vickey\cdtest\images\member3.jpg"> </image> <name>anuj</name> </ListItem> <ListItem> <image href="C:\wampserver 2\www\aafes test\my\by vickey\cdtest\images\member4.jpg"> </image> <name>bb</name> </ListItem></ListItems>right now I am getting the out as1 12 23 3but I want it to b like..1 23 45 6

Link to post
Share on other sites

yaa I want to do something of tht kind... The code I have given in my previous post prints the data in following fashion1 12 23 3and so onbut I want it to b like1 23 45 6and so on...can you help me with it?I saw the Q5 answer but couldn't get it much.....

Edited by anujojha
Link to post
Share on other sites

The actual issue here is not table building, but how to determine when to break an iteration at the point where a new row is to be created, or a br or hr or p tag. So breaking this down to its smallest piece, lets output each image node value and then next one then the tag. You'll see that we simply check the position of the node in the iteration and determine if is evenly divided by two, If yes, we output it, if not we output it PLUS something else, in this case a br

	<xsl:template match="/">		<xsl:for-each select="//image">			<xsl:choose>				<xsl:when test="position() mod 2 = 1">					<xsl:value-of select="."/>				</xsl:when>			<xsl:otherwise>				<xsl:value-of select="."/><br/>			</xsl:otherwise>			</xsl:choose>		</xsl:for-each>	</xsl:template>

Tables have special needs (we can't output a tr without closing it properly). So we use the knowledge that what we really want is a way to know when we have to break, in this case it's the fact that the position in the iteration is evenly divided by two (or 3 or whatever you want your column count to be).The fact that we are iterating on the images nodes is irrelevant, we could also say for i = 1 to 6 if it were permitted.So building on the above, we take that knowledge and pass the position to a named template to build the row.

	<xsl:template match="/">		<table border="1">			<xsl:for-each select="//image">				<xsl:if test="position() mod 2 = 1">					<xsl:call-template name="BuildRow">						<xsl:with-param name="position" select="position()"/>					</xsl:call-template>				</xsl:if>			</xsl:for-each>		</table>	</xsl:template>	<xsl:template name="BuildRow">		<xsl:param name="position" />		<tr>			<td><xsl:value-of select="//image[$position]"/></td>			<td><xsl:value-of select="//image[$position + 1]"/></td>		</tr>	</xsl:template>

Hope this helps some

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...