Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2009, 02:05 PM
Registered User
 
Join Date: Oct 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Problem with displaying data

Hello Everyone

I am dispalying XML data using XSLT I want to display data from 'address' element first and then 'name' element. But the problem is its displaying data from 'name' element first then from 'address'.

below is the code for your view :

XML Document :

Code:
<?xml version="1.0" encoding="UTF-8"?>


  <employees>
	<employee>
           <name>
            <firstname>peter</firstname>
            <lastname>Dossan</lastname>
	   </name>
	   <address>
		<street>purly</street>
		<state>MH</state>
		<zip>4009890</zip>
           </address>	
	</employee>
  </employees>
XSLT :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    
        
    <xsl:template match="/">
        
        <xsl:apply-templates/>       
        
    </xsl:template>
    
        
    <xsl:template match="address">                 
                          
	 <xsl:value-of select="street"></xsl:value-of>
         <br />

         <xsl:value-of select="state"></xsl:value-of>
	 <br/>	

	<xsl:value-of select="zip"></xsl:value-of>
	 <br/>		

        
    </xsl:template>


    <xsl:template match="name">                 
                          
	 <xsl:value-of select="firstname"></xsl:value-of>
         <br />

         <xsl:value-of select="lastname"></xsl:value-of>
	 <br/>

	
        
    </xsl:template>

</xsl:stylesheet>
how can i achieve that ?

Thanks for your help

yanki
 
Old October 29th, 2009, 02:11 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Replace
Code:
    <xsl:template match="/">
        
        <xsl:apply-templates/>       
        
    </xsl:template>
with
Code:
    <xsl:template match="/">
        
        <xsl:apply-templates select="employees/employee/address, employees/employee/name"/>       
        
    </xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; October 29th, 2009 at 02:30 PM.. Reason: correcting typo in code sample
 
Old October 29th, 2009, 03:44 PM
Registered User
 
Join Date: Oct 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks martin its working.

I have a quick question.

In XML i have element like below :

[code]

<departments>
<department>Sales</department>
<department>Marketing</department>
<department>Finance</department>

</departments>

[code]



Code:
<xsl:template match="departments" 
        <tr>
            <td colspan="3">
                <marquee><xsl:value-of select="department"/> <xsl:text>  </xsl:text> <xsl:text>  </xsl:text></marquee>                
            </td>
        </tr>        
    </xsl:template>

I want 3 white space between department values.
How can i do that ?

Thanks again

yanki
 
Old October 30th, 2009, 02:17 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:

<xsl:template match="departments">
<tr>
<td colspan="3">
<marquee>
<xsl:for-each select="department">
<xsl:value-of select="."/>
<xsl:if test="not(position() = last())">
<xsl:text> </xsl:text>
<xsl:text> </xsl:text>
<xsl:text> </xsl:text>
<!-- or insert three spaces inside one <xsl:text> -->
</xsl:if>
</xsl:for-each>
</marquee>
</td>
</tr>
</xsl:template>
__________________
Rummy

Last edited by mrame; October 30th, 2009 at 02:30 AM..
 
Old October 30th, 2009, 05:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you are using XSLT 2.0 then

Code:
<xsl:value-of select="department" separator="   "/>
will display all the departments, with three spaces between adjacent departments. (Of course if this is generating HTML, you probably want three non-breaking spaces instead.)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying Data areed24 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 13 March 30th, 2009 01:28 PM
Problem Displaying Data Using Repeater Control cyberstore05 ASP.NET 2.0 Professional 0 January 5th, 2008 01:45 PM
Problem in Displaying preselected data in dropdown deb_kareng ASP.NET 2.0 Professional 2 August 14th, 2007 05:00 AM
Displaying My Data jamil umar ASP.NET 2.0 Basics 0 June 5th, 2006 09:48 AM
No errors and Data not displaying Calibus Classic ASP Databases 6 August 10th, 2004 08:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.