 |
| 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
|
|
|
|

April 4th, 2008, 10:39 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Help with filtering xml
Hi
I am working on extracting info from a remote xml file using an xsl page. the xml has the following tree
category
Bookmaker
name
timeGenerated
<>event
date
eventid
meeting
name
sport
sporttype
time
venue
<>bettype
bettypeid
eachway
ewplaceterms
ewreduction
inrunning
name
suspended
<>bet
active-price-types
id
name
price
priceDecimal
I thought the tree would give a clearer picture of what I want to do and how to do it. Without going into any detail about code I have already tried I will just say what I want to do.
there could be anything from 2-40 node names in <>bet
I want to access category/event/bettype/bet/price
I have passed a parameter to the xsl file for <>bet/name and can access the <>bet/price for that name. The main problem is I only want to access the <>bet element if category/event/date is equal to a date passed as a parameter to the xsl file.
there are about 20-30 different <>event elements and I only want to search the ones that match a date and then search the child <>bet element for a name.
I hope I explained this properly and people can understand what I am trying to do.
|
|

April 5th, 2008, 02:37 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I suspect you need to spend some time trying things yourself and learning how XSLT works.
I'd recommend a web-site such as http://www.w3schools.com/xsl/default.asp or the Wrox XSLT book (new edition due out in a few weeks).
Once you've tried some things and if you then have any more problems then get back to us with an example of what you've tried and any problems you are having.
/- Sam Judson : Wrox Technical Editor -/
|
|

April 5th, 2008, 03:02 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>Without going into any detail about code I have already tried I will just say what I want to do.
It's always best to show the code that you've already tried. That gives us a feel for what you know and what you still need to learn. Without that, you're just asking us to write the program for you, and it's not clear whether that will help you over your learning difficulties.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 5th, 2008, 05:38 AM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi guys
Thanks for replying. I have spend a lot of time on w3schools and other sites trying to figure it out with no success. this is the first time I have been working with xml feeds and do most of my programming in php and javascript.You are probably right that I wanted somebody to write the code to do this because I have hit numerous dead ends. It is probably only a couple of lines of code to somebody who does this every day of the week.
I have tried using template, for-each, if, choose and key with no success.
here is an extract from the xml file
- <event name="7f Handicap 0-90" eventid="457865.10" date="20080405" time="1410" meeting="Lingfield" venue="Lingfield" sporttype="Horse Racing" sport="Horse Racing U.K.">
- <bettype ewreduction="4" ewplaceterms="3" eachway="1/4 Odds Place 1,2,3" suspended="false" name="Outright Betting" inrunning="0" bettypeid="955892.10">
<bet name="King Of Dixie" id="54548264.10" price="11/4" priceDecimal="3.75" active-price-types="EP,SP" />
<bet name="Buxton" id="54548270.10" price="9/1" priceDecimal="10.00" active-price-types="EP,SP" />
<bet name="Yarqus" id="54548271.10" price="7/1" priceDecimal="8.00" active-price-types="EP,SP" />
<bet name="Desert Dreamer" id="54548272.10" price="9/1" priceDecimal="10.00" active-price-types="EP,SP" />
<bet name="Purus" id="54548273.10" price="12/1" priceDecimal="13.00" active-price-types="EP,SP" />
<bet name="Councellor" id="54548274.10" price="25/1" priceDecimal="26.00" active-price-types="EP,SP" />
<bet name="Resplendent Nova" id="54548275.10" price="14/1" priceDecimal="15.00" active-price-types="EP,SP" />
<bet name="Carcinetto" id="54548276.10" price="25/1" priceDecimal="26.00" active-price-types="EP,SP" />
<bet name="Lucayos" id="54548277.10" price="25/1" priceDecimal="26.00" active-price-types="EP,SP" />
<bet name="Secret Night" id="54548278.10" price="8/1" priceDecimal="9.00" active-price-types="EP,SP" />
<bet name="Forest Dane" id="54548279.10" price="20/1" priceDecimal="21.00" active-price-types="EP,SP" />
<bet name="Danetime Lord" id="54548280.10" price="12/1" priceDecimal="13.00" active-price-types="EP,SP" />
<bet name="Lunces Lad" id="54548281.10" price="25/1" priceDecimal="26.00" active-price-types="EP,SP" />
<bet name="Farley Star" id="54548283.10" price="4/1" priceDecimal="5.00" active-price-types="EP,SP" />
</bettype>
</event>[/code]
My current php code lookes like this.
$mm_xsl = new MM_XSLTransform();
$mm_xsl->setXML("http://xml.bookmaker.com/xml/horseracing.xml");
$mm_xsl->setXSL("Bookmaker_price_feed.xsl");
$mm_xsl->addParameter("horse_name", $t_horse_name);
$boyle_price = $mm_xsl->Transform();
$t_boyle_price = trim($boyle_price);
if(strlen($t_boyle_price) > 6) {$t_boyle_price = "err"; }
my xsl code is
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:param name="horse_name" select="'nag'"/>
<xsl:for-each select="category/event/bettype/bet[@name = $horse_name]">
<xsl:value-of select="@price"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
there is no formatting and is very simple as I only want the price of the horse returned to the main program. The problem is there could be more than one event with a horses name in it under a different date so I want to filter only events where the date is a date I pass as a parameter.
if I do
<xsl:for-each select="category/event[@date = race_date]">
<xsl:value-of select=bettype/bet"@price"/>
</xsl:for-each>
it will only return the first price, I was hoping for it to return all prices. being able to nest for-each loops would be perfect, but wont let me do it.
As I said I have played with it for a long time trying different things(mainly nested loops) but with no success.
Joe
|
|

April 5th, 2008, 06:31 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You seem to have overlooked that predicates can appear after any step in a path expression:
<xsl:for-each select="category/event[@date = race_date]/bettype/bet[@name = $horse_name]">
But also, there's no problem in using nested loops:
<xsl:for-each select="category/event[@date = race_date]">
<xsl:for-each select="bettype/bet[@name = $horse_name]">
<xsl:value-of select="@price"/>
You haven't shown your nested loop code that wasn't working, so I can't tell you what you did wrong. (Please! - never say on a forum that you tried something and it didn't work, without saying EXACTLY what you tried and EXACTLY how it failed). The most common mistake is to get the context wrong, that is, to remember that the context node for evaluating the inner for-each is the node selected by the outer for-each.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 5th, 2008, 07:55 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Michael for the reply. I learned from your post and am very appreciative of the help.
Your code was very similar to what I was trying to do with no success. I believe you know what you are talking about so continued with a lot of trial and error. I came to the conclusion the the passing of the parameters was the problem but could not figure out why. Again through trial and error I found out the problem and will tell what the problem was in case any other novice xml programmer comes across the same problem.
The reason I didn't post the code I was testing was because I was changing it every 5 mins and testing then change again and test and was using nearly every different xsl element I studied.
my original code looked like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:param name="date" select="20080405"/>
<xsl:param name="time" select="1412"/>
<xsl:for-each select="category/event[@time=$time][@date=$date]">
<xsl:for-each select="bettype/bet">
<xsl:value-of select="@price"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
which didn't work. the problem was the <xsl:param/> needs to be above the <xsl:template> while I had it below it. I don't know xsl well enough to fully understand why but it works fine now and thanks again for your help.
Joe
|
|

April 6th, 2008, 11:33 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Here are two very inefficient ways of learning a language:
(a) use trial and error
(b) whenever things don't work, ask for help on a forum
Here are two better ways:
(a) attend a training course
(b) buy a good book and study it
An xsl:param inside a template defines parameters to that template. An xsl:param outside a template defines parameters to the stylesheet as a whole. Stylesheet parameters can be set by the calling application or from the command line; template parameters can only be set from the apply-templates or call-template instruction that invokes that particular template.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |