Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 7th, 2009, 06:48 AM
Registered User
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML - Extract Data from Child Nodes with different Names

Hi,

I am having a hard time extracting data from an XML file with different child node names (ForecastDay1, ForecastDay2, etc..). Unfortunately I cannot change the XML and will have to find a work around.

My aim is to extract only the Day Names (and date) with the Min and Max Temperatures and present this in a table for all the child nodes.

I can't seem to get the Temperature (min, max) values from the nodes. Any help is greatly appreciated.

The XML:

Code:
 

<Location>
<ObservedConditions>
<ObservedDate>2008-11-11T06:00:00</ObservedDate>
<DrybulbTemperature Unit="Celsius" />
<Humidity Unit="Percentage" />
<BarometricPressure Unit="Hecto Pascal" />
<DewpointTemperature Unit="Celsius" />
<Picture>modheavyrain</Picture>
<Description xsi:nil="true" />
<Wind>
<Speed Unit="Knots" />
<Direction Unit="Degrees" />
</Wind>
</ObservedConditions>
<Forecasts>
<ForecastDay0 FDay="0" Date="2008-11-11T00:00:01.930" Name="Tuesday">
<Temperature Unit="Celcius">
<Min>17</Min>
<Max>21</Max>
<AppTemp>17</AppTemp>
<SeaTemp xsi:nil="true" />
</Temperature>
</ForecastDay0>
<ForecastDay1 FDay="1" Date="2008-11-12T00:00:01.930" Name="Wednesday">
<Temperature Unit="Celcius">
<Min>18</Min>
<Max>22</Max>
<AppTemp>17</AppTemp>
<SeaTemp xsi:nil="true" />
</Temperature>
</ForecastDay1>
<ForecastDay2 FDay="2" Date="2008-11-12T00:00:01.930" Name="Thursday">
<Temperature Unit="Celcius">
<Min>16</Min>
<Max>19</Max>
<AppTemp>17</AppTemp>
<SeaTemp xsi:nil="true" />
</Temperature>
</ForecastDay2>
Thanks!
Reply With Quote
  #2 (permalink)  
Old January 7th, 2009, 07:55 AM
Friend of Wrox
Points: 1,306, Level: 14
Points: 1,306, Level: 14 Points: 1,306, Level: 14 Points: 1,306, Level: 14
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: Pondicherry, India
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

If I have correctly understood your need, then the below script should help. If not, please let me know.
Code:
<xsl:template match="Location">
<xsl:apply-templates select="Forecasts"></xsl:apply-templates>
</xsl:template>

<xsl:template match="Forecasts">
<days>
<xsl:for-each select="*">
<day>
<dayname><xsl:value-of select="@Name"></xsl:value-of></dayname>
<date><xsl:value-of select="@Date"></xsl:value-of></date>
<min><xsl:value-of select="Temperature/Min"></xsl:value-of></min>
<max><xsl:value-of select="Temperature/Max"></xsl:value-of></max>
</day>
</xsl:for-each>
</days>
</xsl:template>

__________________
Rummy
Reply With Quote
  #3 (permalink)  
Old January 7th, 2009, 08:18 AM
Registered User
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mrame,

Basically I wish to display the data in an ASPX page e.g.

Tuesday (5 Jan 2009)
Min Temp: 18
Max Temp 20

Wednesday (6 Jan 2009)
Min Temp: 18
Max Temp 20

and so on...

I have a page called weather.aspx with the following;

Code:
<asp:Xml ID="Xml1" runat="server" DocumentSource="68842.xml" TransformSource="68842.xslt"></asp:Xml>
I have create a XSLT file with your code and called it 68842.xslt.

Code:
 
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
 
<xsl:template match="Location">
<xsl:apply-templates select="Forecasts"></xsl:apply-templates>
</xsl:template>
<xsl:template match="Forecasts">
<days>
<xsl:for-each select="*">
<day>
<dayname>
<xsl:value-of select="@Name"></xsl:value-of>
</dayname>
<date>
<xsl:value-of select="@Date"></xsl:value-of>
</date>
<min>
<xsl:value-of select="Temperature/Min"></xsl:value-of>
</min>
<max>
<xsl:value-of select="Temperature/Max"></xsl:value-of>
</max>
</day>
</xsl:for-each>
</days>
</xsl:template>
</xsl:stylesheet>
When I run the weather.aspx file, it appears to bring back the entire XML file (see extract below);

HTML Code:
 
...............2008-11-11T06:00:00modheavyrain172117808549NoneCloudy with light
 showerscloudlightrainCloudyGood30%Light showersOn and off1.0SWSouth
 Westerly2251525Moderate South WesterlyCloudy with 
showersmodheavyrainCloudyModerate60%ShowersIn places2.0SESouth
 Easterly1351828Fresh South EasterlyCloudy with showersmodheavyrainCloudyPoor80%
  .....................

Any idea why the entire XML is displayed and not the items as per the XSLT?
Reply With Quote
  #4 (permalink)  
Old January 7th, 2009, 08:49 AM
Friend of Wrox
Points: 1,306, Level: 14
Points: 1,306, Level: 14 Points: 1,306, Level: 14 Points: 1,306, Level: 14
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: Pondicherry, India
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

Try changing 68842.xslt to 68842.xsl, if you have saved the xsl file as 68842.xsl.
__________________
Rummy
Reply With Quote
  #5 (permalink)  
Old January 7th, 2009, 09:21 AM
Registered User
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Still no luck...


Last edited by hendrik_sa; January 8th, 2009 at 05:29 AM.
Reply With Quote
  #6 (permalink)  
Old January 8th, 2009, 01:04 AM
Friend of Wrox
Points: 1,306, Level: 14
Points: 1,306, Level: 14 Points: 1,306, Level: 14 Points: 1,306, Level: 14
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: Pondicherry, India
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

This stylesheet should help. The problem is namespace you have used in your input xml. I have added a dummy prefix in the stylesheet. Try it out.

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://schemas.saws.co.org/Weather/1.0" exclude-result-prefixes="x">
<xsl:output method="xml" indent="yes"/>
 
<xsl:template match="x:Location">
<days>
<xsl:apply-templates select="x:Forecasts"></xsl:apply-templates>
</days>
</xsl:template>
<xsl:template match="x:Forecasts">

<xsl:for-each select="*">
<day>
<dayname>
<xsl:value-of select="@Name"></xsl:value-of>
</dayname>
<date>
<xsl:value-of select="@Date"></xsl:value-of>
</date>
<min>
<xsl:value-of select="x:Temperature/x:Min"></xsl:value-of>
</min>
<max>
<xsl:value-of select="x:Temperature/x:Max"></xsl:value-of>
</max>
</day>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

__________________
Rummy
Reply With Quote
  #7 (permalink)  
Old January 8th, 2009, 05:32 AM
Registered User
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That did the trick!


Thanks a lot for all your efforts and time to help me, it's greatly appreciated!

Hope I can return a favour one day!
Cheers...
Reply With Quote
Reply


Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I display parent & child nodes if XML .... vishnu108mishra ASP.NET 2.0 Basics 0 November 17th, 2007 07:45 AM
How to use Xpath to extract child nodes smilysuresh XSLT 3 February 23rd, 2007 06:32 AM
I cant retrieve XMl child nodes jfergy Classic ASP XML 0 December 8th, 2006 10:24 PM
Extract data from the XML result of a web service s.hendy ASP.NET 1.0 and 1.1 Basics 0 March 16th, 2006 12:27 PM
Extract data and create xml for input dhol General .NET 1 February 2nd, 2005 03:01 PM



All times are GMT -4. The time now is 09:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.