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 November 23rd, 2005, 02:33 AM
Authorized User
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jvraman
Default XML, XSLT Help

I am having a XML file and i wanted to display the data from the xml using xslt file format. Can any 1 of u help me out what are the steps that i have to follow. Iam new in using XML.


Regards
Venkat
__________________
Regards
Venkat
 
Old November 23rd, 2005, 08:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, there is several ways... I guess the most simple way is to include a reference to the XSLT document within the XML document. Try making a file named data.xml with the following contents...
Code:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="data.style.xslt"?>
<test title="my test XML/XSLT page" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <something another="node01">this is some text...</something>
    <something another="node02">... and this is some text</something>
</test>
... and then make a file named data.style.xslt with the following contents...
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="test">
        <html>
            <head>
                <title><xsl:value-of select="@title" /></title>
                <style>
                    table { width:600px; font-family:verdana; }
                    td    { border:solid 1px #aaaaaa; padding:4px; }
                </style>
            </head>
            <body>
                <table cellspacing="2" cellpadding="0">
                    <td colspan="2"><xsl:value-of select="@title" /></td>
                    <xsl:apply-templates select="something" />
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="something">
        <tr>
            <td><xsl:value-of select="@another"/></td>
            <td><xsl:value-of select="."/></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>
... and put it in the same folder as the XML document. Now open the XML document in a browser. The XML file should now be displayed as specified in the XSLT document.

Go through the XSLT document and understand why it displays what it does the way it does it. For further information introduction to XSLT look at this basic tutorial...

http://www.w3schools.com/xsl/

Hope this helps,
Jacob.
 
Old November 23rd, 2005, 09:05 AM
Authorized User
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jvraman
Default

Hi jacob,

the below is my .xsl file code

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>

<BODY>
<h4>All records of the Table</h4>
<table border="1">
            <tr>

                <th>First Name</th>
                <th>Last Name</th>
                <th>Address1</th>
        <th>Address2</th>
        <th>Phone</th>
                <th>Email</th>
           </tr>
<xsl:for-each select="rolodex/contact">
        <tr>

<td>
<xsl:value-of select="@firstName"/>
</td>



        <td>
<xsl:attribute name="firstName">


  <xsl:value-of select="@firstName" />

</xsl:attribute>


            <xsl:value-of select="lastName" />
            <xsl:value-of select="address1" />
            <xsl:value-of select="address2" />
            <xsl:value-of select="phone" />
            <xsl:value-of select="email" />


         </td>



        </tr>
</xsl:for-each>

 </table>
<br />
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>


My XML Code is this

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="MyTransformFile1.xsl"?>
<rolodex><contact><field id="firstName" taborder="1"><field_value></field_value></field><field id="lastName" taborder="2"><field_value></field_value></field><field id="address1" taborder="3"><field_value></field_value></field><field id="address2" taborder="4"><field_value></field_value></field><field id="phone" taborder="5"><field_value></field_value></field><field id="email" taborder="6"><field_value></field_value></field></contact><contact><field id="firstName" taborder="1"><field_value>mithun</field_value></field><field id="lastName" taborder="2"><field_value>mithun</field_value></field><field id="address1" taborder="3"><field_value>dcvsdc</field_value></field><field id="address2" taborder="4"><field_value>asSD</field_value></field><field id="phone" taborder="5"><field_value>34523455</field_value></field><field id="email" taborder="6"><field_value>kl;sdc</field_value></field></contact><contact><field id="firstName" taborder="1"><field_value>Venkata</field_value></field><field id="lastName" taborder="2"><field_value>Ramana</field_value></field><field id="address1" taborder="3"><field_value>a;lkdsjf</field_value></field><field id="address2" taborder="4"><field_value>a;lkdsjf</field_value></field><field id="phone" taborder="5"><field_value>03940932840</field_value></field><field id="email" taborder="6"><field_value>[email protected]</field_value></field></contact><contact><field id="firstName" taborder="1"><field_value>adsf</field_value></field><field id="lastName" taborder="2"><field_value>asdf</field_value></field><field id="address1" taborder="3"><field_value>adf</field_value></field><field id="address2" taborder="4"><field_value>adfadfasdf</field_value></field><field id="phone" taborder="5"><field_value>asdfasdfadsf</field_value></field><field id="email" taborder="6"><field_value>adsfasdfasdf</field_value></field></contact><contact><field id="firstName" taborder="1"><field_value>Venkatesh</field_value></field><field id="lastName" taborder="2"><field_value>Murali</field_value></field><field id="address1" taborder="3"><field_value>alksjfd</field_value></field><field id="address2" taborder="4"><field_value>aslkfd;j</field_value></field><field id="phone" taborder="5"><field_value>32048</field_value></field><field id="email" taborder="6"><field_value>023948</field_value></field></contact></rolodex>

when i open the xml file then the headings are only getting displayed. No record is displayed. Can u check what is the error in this?

Rgds
Venkat.

Regards
Venkat
 
Old November 23rd, 2005, 09:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, you XML code is really hard to read, but I don't think you have read the post that I did and understood why it displays what it does; e.g. get the values of current attributes by prefixing the name of the attribute with an @ and use a dot to get the value of the current node.

Read my post and learn why it displays the stuff, or read the tutorial.

Jacob.
 
Old November 24th, 2005, 02:42 AM
Authorized User
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jvraman
Default

thanks jacob i tried both ur method and as well as mine.

The records are not getting displayed and the heading are only getting displayed.

Regards
Venkat
 
Old November 24th, 2005, 07:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Did you read the first post or the tutorial on how to extract values and attributes? Here is some help... put in this code instead of your for-each block...
Code:
<xsl:for-each select="rolodex/contact">
    <tr>
        <td>
            <xsl:value-of select="field[@id='firstName']/field_value"/>
        </td>
        <td>
            <xsl:value-of select="field[@id='lastName']/field_value"/>
        </td>
        <td>
            <xsl:value-of select="field[@id='address1']/field_value"/>
        </td>
        <td>
            <xsl:value-of select="field[@id='address1']/field_value"/>
        </td>
        <td>
            <xsl:value-of select="field[@id='phone']/field_value"/>
        </td>
        <td>
            <xsl:value-of select="field[@id='email']/field_value"/>
        </td>
    </tr>
</xsl:for-each>
Also look more into XPath, which helps you to extract the values the way you want, and BTW it is often helpful to make a smaller example and then expand it from there if you are in doubt.

Hope it helps, Jacob.
 
Old November 24th, 2005, 07:15 AM
Authorized User
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jvraman
Default

Hi Jacob,
  Thanks a ton for the gr8 help. It was woking. actually after posting my reply to u, i tried extracting the data and i was able to extract the whole row as 1 data. And then i tried splitting it like the first name, last name, address1, and so on.. I was not able to succeed. But this code has helped me a lot and the report is now perfect.


Thanks a ton once again. i have got a new assignment and in this i have to display like this and there are many more doubts that are there in the pipe line.

Will catch up with u at that juncture.

this forum is of gr8 help to me..

Regards
Venkat





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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