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 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!
 
Old January 7th, 2009, 07:55 AM
Friend of Wrox
 
Join Date: Jun 2008
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
 
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?
 
Old January 7th, 2009, 08:49 AM
Friend of Wrox
 
Join Date: Jun 2008
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
 
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..
 
Old January 8th, 2009, 01:04 AM
Friend of Wrox
 
Join Date: Jun 2008
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
 
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...





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





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