8 Replies - 870 Views - Last Post: 24 January 2011 - 02:46 PM Rate Topic: -----

#1 glandrum101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

XPath Query with PHP (more of an XPath question)

Posted 21 January 2011 - 10:45 AM

Mods: feel free to move this to a better forum if that is where XPath questions should go.


Basically, I have a fairly large XML feed that I am trying to read and I need to select only the nodes that have a specific child.

Here is an excerpt of the XML file with multiple nodes:
<event>
	<event_datetimeGMT>2011-05-07 00:05</event_datetimeGMT>

	<gamenumber>182331575</gamenumber>

	<sporttype>Basketball</sporttype>

	<league>NBA</league>

	<IsLive>No</IsLive>

	<participants>

		<participant>

			<participant_name>Check The Live NBA Folder</participant_name>

			<contestantnum>1001</contestantnum>

			<rotnum>1001</rotnum>

			<visiting_home_draw>Visiting</visiting_home_draw>

		</participant>

		<participant>

			<participant_name>For Our Live Offerings</participant_name>

			<contestantnum>1002</contestantnum>

			<rotnum>1002</rotnum>

			<visiting_home_draw>Home</visiting_home_draw>

		</participant>

	</participants>

	<periods>

		<period>

			<period_number>0</period_number>

			<period_description>Game</period_description>

			<periodcutoff_datetimeGMT>2011-05-07 00:05</periodcutoff_datetimeGMT>

			<period_status>H</period_status>

			<period_update>offline</period_update>

			<spread_maximum>20000</spread_maximum>

			<moneyline_maximum>10000</moneyline_maximum>

			<total_maximum>10000</total_maximum>

			<spread>

				<spread_visiting>0</spread_visiting>

				<spread_adjust_visiting>-105</spread_adjust_visiting>

				<spread_home>0</spread_home>

				<spread_adjust_home>-105</spread_adjust_home>

			</spread>

		</period>

	</periods>

</event>
<event>
	<event_datetimeGMT>2011-01-22 16:00</event_datetimeGMT>

	<gamenumber>191794779</gamenumber>

	<sporttype>Basketball</sporttype>

	<league>NCAA</league>

	<IsLive>No</IsLive>

	<participants>

		<participant>

			<participant_name>George Mason</participant_name>

			<contestantnum>527</contestantnum>

			<rotnum>527</rotnum>

			<visiting_home_draw>Visiting</visiting_home_draw>

		</participant>

		<participant>

			<participant_name>James Madison</participant_name>

			<contestantnum>528</contestantnum>

			<rotnum>528</rotnum>

			<visiting_home_draw>Home</visiting_home_draw>

		</participant>

	</participants>

	<periods>

	</periods>

</event>

<event>

	<event_datetimeGMT>2011-01-22 17:00</event_datetimeGMT>

	<gamenumber>191794780</gamenumber>

	<sporttype>Basketball</sporttype>

	<league>NCAA</league>

	<IsLive>No</IsLive>

	<participants>

		<participant>

			<participant_name>Ohio State</participant_name>

			<contestantnum>529</contestantnum>

			<rotnum>529</rotnum>

			<visiting_home_draw>Visiting</visiting_home_draw>

		</participant>

		<participant>

			<participant_name>Illinois</participant_name>

			<contestantnum>530</contestantnum>

			<rotnum>530</rotnum>

			<visiting_home_draw>Home</visiting_home_draw>

		</participant>

	</participants>

	<periods>

	</periods>

</event>



This is the PHP code minus the query that I need to create:

$source = 'http://xml.pinnaclesports.com/pinnacleFeed.aspx?';
$sitemap = simplexml_load_file($source);
$xpath_results = $sitemap->xpath('[b]<QUERY GOES HERE OBV>[b]');


I am basically just lost on how to get this query to return the COMPLETE <event> node if it has a child node that has the <league> AND <sporttype> attributes equal to specific values. For example sake, lets make the <league> value we are looking for "NBA" and the <sporttype> value "Basketball".

If there are easier ways that do not incorporate XPath I am fine with using that. The way I return these nodes is irrelevant, I just need the nodes.

This post has been edited by glandrum101: 21 January 2011 - 10:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: XPath Query with PHP (more of an XPath question)

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: XPath Query with PHP (more of an XPath question)

Posted 21 January 2011 - 12:30 PM

//event[league = $liga and sporttype = $type] or //event[league = $liga][sporttype = $type] should work.
Was This Post Helpful? 3
  • +
  • -

#3 glandrum101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

Re: XPath Query with PHP (more of an XPath question)

Posted 21 January 2011 - 01:03 PM

perfect. that worked. thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#4 glandrum101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

Re: XPath Query with PHP (more of an XPath question)

Posted 21 January 2011 - 03:32 PM

one more quick question:

This is what I have currently:

	$source = 'http://xml.pinnaclesports.com/pinnacleFeed.aspx?';
	$sitemap = simplexml_load_file($source);
	
	//displayChildrenRecursive($sitemap);
	
	$league = "NBA";
	$sporttype = "Basketball";
	
	$xpath_results = $sitemap->xpath('//event[league="'.$league.'"][sporttype="'.$sporttype.'"]/periods/period[period_description="Game"][moneyline][spread][total]');
	


However, this returns all the <period> nodes that fall into this category. Instead I'd like it to return the complete <event> node. Basically, I am looking for all the nodes that have a <sporttype> value of "Basketball", <league> value of "NBA", and it has attributes <periods><period><moneyline> AND <periods><period><spread> AND <periods><period><total>.

EDIT: changing '//event[league="'.$league.'"][sporttype="'.$sporttype.'"]/periods/period[period_description="Game"][moneyline][spread][total]' to '//event[league="'.$league.'"][sporttype="'.$sporttype.'"] | event/periods/period[period_description="Game"][moneyline][spread][total]' fixed it

This post has been edited by glandrum101: 21 January 2011 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 glandrum101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

Re: XPath Query with PHP (more of an XPath question)

Posted 21 January 2011 - 03:44 PM

I think my edit window may have closed but I wanted to add the above code to a code snippet for readability purposes. sorry for spamming the board needlessly.

The following change fixed my issue:

	$xpath_results = $sitemap->xpath('//event[league="'.$league.'"][sporttype="'.$sporttype.'"]/periods/period[period_description="Game"][moneyline][spread][total]');
	


changed to:

 $xpath_results = $sitemap->xpath('//event[league="'.$league.'"][sporttype="'.$sporttype.'"] | event/periods/period[period_description="Game"][moneyline][spread][total]');
	

Was This Post Helpful? 0
  • +
  • -

#6 glandrum101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

Re: XPath Query with PHP (more of an XPath question)

Posted 24 January 2011 - 01:57 PM

My post above actually did not fix my problems (I thought it had).

I need to find the COMPLETE <event> nodes that have the attribute of <league> = "NBA", the <sporttype> = "Basketball", <period_description> = "Game", AND has an attribute called <moneyline>. Currently I have this:

	$league = "NBA";
	$sporttype = "Basketball";
	
	$xpath_results = $sitemap->xpath('//event[league="'.$league.'"][sporttype="'.$sporttype.'"] and //event/periods/period[period_description="Game"][moneyline]');


But that returns no results. Any help is appreciated. Thanks again.

This post has been edited by glandrum101: 24 January 2011 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: XPath Query with PHP (more of an XPath question)

Posted 24 January 2011 - 02:06 PM

there is no attribute called moneyline, there is not even one attribute in the whole XML. you only have a tag called <moneyline_maximum>.
//event[league = $liga][sporttype = $type][descendant::period_description = $desc][descendant::moneyline_maximum]

Was This Post Helpful? 0
  • +
  • -

#8 glandrum101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

Re: XPath Query with PHP (more of an XPath question)

Posted 24 January 2011 - 02:26 PM

View PostDormilich, on 24 January 2011 - 02:06 PM, said:

there is no attribute called moneyline, there is not even one attribute in the whole XML. you only have a tag called <moneyline_maximum>.
//event[league = $liga][sporttype = $type][descendant::period_description = $desc][descendant::moneyline_maximum]


The XML that I posted was just a snippet of the full XML which contains upwards of a 1000 event nodes. I didn't realize that moneyline wasn't in the snippet, but it occurs under the <period><periods> section as a child (same area as moneyline_maximum), but they don't show it if there is no value for it for that specific node.

For future reference, how does the [descendant::moneyline_maximum] part work? How far will that check? i.e Does it check the children of children of children? Based on your solution that seems to be the case, but wanted to make sure.

Also, that structure for the XPath query worked. Thanks again.

This post has been edited by glandrum101: 24 January 2011 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: XPath Query with PHP (more of an XPath question)

Posted 24 January 2011 - 02:46 PM

View Postglandrum101, on 24 January 2011 - 10:26 PM, said:

For future reference, how does the [descendant::moneyline_maximum] part work? How far will that check? i.e Does it check the children of children of children?

that’s what the descendant axis does. see http://www.w3.org/TR/xpath/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1