I have xml like this:
<root>
<cards>
<meeting name="Punchestown (IRE)" id="195" diffusion_course_name="PUNCHESTOWN">
<race id="692415" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>12:25</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Adare Manor Opportunity Handicap Chase</title>
<type>C</type>
<distance>2m4f</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>10</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Handicap Chase</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="692416" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>1:00</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Total Event Rental (Kildare) Novice Chase (Grade 3)</title>
<type>C</type>
<distance>2m4f</distance>
<group>Grade 3</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>7</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Novice Chase Grade 3</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="692417" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>1:35</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Connolly's RED MILLS Amateur National (Q.R.) Handicap Chase</title>
<type>C</type>
<distance>3m1f</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>12</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Handicap Chase</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="692418" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>2:10</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Sky Bet Moscow Flyer Novice Hurdle (Grade 2)</title>
<type>H</type>
<distance>2m</distance>
<group>Grade 2</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>7</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Novice Hurdle Grade 2</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="692419" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>2:45</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Sportinglife.com Maiden Hurdle</title>
<type>H</type>
<distance>2m</distance>
<group/>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>17</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Maiden Hurdle</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="692420" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>3:20</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Leinster Leader Mares Handicap Hurdle</title>
<type>H</type>
<distance>2m4f40y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>8</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Handicap Hurdle</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="692421" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>3:50</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>David Trundley Artist At Punchestown Irish Stallion Farms EBF Mares Flat Race</title>
<type>B</type>
<distance>2m</distance>
<group/>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>14</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>NHF</raceDescription>
<tvText>ATR </tvText>
</race>
</meeting>
<meeting name="Warwick" id="85" diffusion_course_name="WARWICK">
<race id="691061" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>12:40</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betfred Supports Jack Berry House Novices' Handicap Hurdle</title>
<type>H</type>
<distance>2m</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>18</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 4 Novice Handicap Hurdle</raceDescription>
<tvText>RUK </tvText>
<betOffers>
<betOffer>WH</betOffer>
</betOffers>
</race>
<race id="691060" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>1:15</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betfred Mobile Edward Courage Cup Handicap Chase</title>
<type>C</type>
<distance>2m54y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>7</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 3 Handicap Chase</raceDescription>
<tvText>RUK </tvText>
<betOffers>
<betOffer>LB</betOffer>
<betOffer>WH</betOffer>
</betOffers>
</race>
<race id="691058" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>1:50</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betfred Home Of Goals Galore Hampton Novices' Chase (Listed Race)</title>
<type>C</type>
<distance>3m</distance>
<group>Listed</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>5</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 1 Novice Chase Listed</raceDescription>
<tvText>ITV4 </tvText>
<betOffers>
<betOffer>Coral</betOffer>
</betOffers>
</race>
<race id="691059" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>2:25</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Pertemps Network Handicap Hurdle (Series Qualifier)</title>
<type>H</type>
<distance>3m1f</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>12</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 2 Handicap Hurdle</raceDescription>
<tvText>ITV4 </tvText>
<betOffers>
<betOffer>LB</betOffer>
<betOffer>WH</betOffer>
<betOffer>Coral</betOffer>
</betOffers>
</race>
<race id="691057" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>3:00</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Ballymore Leamington Novices' Hurdle (Grade 2)</title>
<type>H</type>
<distance>2m5f</distance>
<group>Grade 2</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>6</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 1 Novice Hurdle Grade 2</raceDescription>
<tvText>ITV4 </tvText>
<betOffers>
<betOffer>WH</betOffer>
<betOffer>Coral</betOffer>
</betOffers>
</race>
<race id="691056" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>3:35</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betfred Classic Handicap Chase (Grade 3)</title>
<type>C</type>
<distance>3m5f54y</distance>
<group>Grade 3 Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>15</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 1 Handicap Chase Grade 3</raceDescription>
<tvText>ITV4 </tvText>
<betOffers>
<betOffer>LB</betOffer>
<betOffer>WH</betOffer>
<betOffer>Coral</betOffer>
<betOffer>PP</betOffer>
</betOffers>
</race>
<race id="691062" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>4:05</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betfred TV "Newcomers" Standard Open National Hunt Flat Race</title>
<type>B</type>
<distance>2m</distance>
<group/>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>9</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 5 NHF</raceDescription>
<tvText>RUK </tvText>
<betOffers>
<betOffer>WH</betOffer>
</betOffers>
</race>
</meeting>
<meeting name="Wetherby" id="87" diffusion_course_name="WETHERBY">
<race id="691067" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>12:30</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Racing UK Jump To It Novices' Hurdle</title>
<type>H</type>
<distance>2m3f154y</distance>
<group/>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>9</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 4 Novice Hurdle</raceDescription>
<tvText>RUK </tvText>
</race>
<race id="691066" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>1:05</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Racing UK In Stunning HD "Confined" Novices' Chase</title>
<type>C</type>
<distance>2m3f85y</distance>
<group/>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>7</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 4 Novice Chase</raceDescription>
<tvText>RUK </tvText>
</race>
<race id="691068" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>1:40</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Bet At racinguk.com Handicap Hurdle</title>
<type>H</type>
<distance>2m</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>9</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 4 Handicap Hurdle</raceDescription>
<tvText>RUK </tvText>
</race>
<race id="691063" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>2:15</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>totescoop6 Play Today Handicap Chase</title>
<type>C</type>
<distance>1m7f36y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>5</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 2 Handicap Chase</raceDescription>
<tvText>RUK </tvText>
</race>
<race id="691064" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>2:50</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>totescoop6 Results On totepoolliveinfo.com Handicap Hurdle</title>
<type>H</type>
<distance>2m3f154y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>11</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 3 Handicap Hurdle</raceDescription>
<tvText>RUK </tvText>
</race>
<race id="691065" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>3:25</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Book Now For Medieval Day - 3rd February Handicap Chase (Northern Lights Middle Distance Series)</title>
<type>C</type>
<distance>2m3f85y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>7</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 4 Handicap Chase</raceDescription>
<tvText>RUK </tvText>
<betOffers>
<betOffer>LB</betOffer>
</betOffers>
</race>
<race id="691069" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>3:55</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Racing UK On Sky 432 Fillies' "Junior" Standard Open National Hunt Flat Race</title>
<type>B</type>
<distance>1m4f77y</distance>
<group/>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>8</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 5 NHF</raceDescription>
<tvText>RUK </tvText>
</race>
</meeting>
<meeting name="Wolverhampton (AW)" id="513" diffusion_course_name="WOLVERHAMPTON">
<race id="691141" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>5:45</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Bet & Watch At sunbets.co.uk Apprentice Handicap</title>
<type>X</type>
<distance>1m142y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>13</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 6 Handicap</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="691136" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>6:15</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>sunbets.co.uk Handicap</title>
<type>X</type>
<distance>1m142y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>9</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 4 Handicap</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="691140" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>6:45</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betway Live Casino Handicap</title>
<type>X</type>
<distance>2m120y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>13</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 6 Handicap</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="691138" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>7:15</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betway Casino Handicap (Div I)</title>
<type>X</type>
<distance>6f20y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>13</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 5 Handicap</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="692653" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>7:45</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betway Casino Handicap (Div II)</title>
<type>X</type>
<distance>6f20y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>12</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 5 Handicap</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="691139" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>8:15</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>Betway Novice Stakes</title>
<type>X</type>
<distance>5f21y</distance>
<group/>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>6</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 5 Novice</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="691137" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>8:45</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>32Red.com Handicap</title>
<type>X</type>
<distance>5f21y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>7</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 4 Handicap</raceDescription>
<tvText>ATR </tvText>
</race>
<race id="691142" perform_race_id="" perform_race_id_atr="" details_available="1" race_status_code="R">
<time>9:15</time>
<date>2018-01-13</date>
<ampm>pm</ampm>
<title>32Red Casino Handicap</title>
<type>X</type>
<distance>1m1f104y</distance>
<group>Handicap</group>
<tipsAllowed>1</tipsAllowed>
<predictorAllowed>1</predictorAllowed>
<bettingLink>1</bettingLink>
<declaredRunners>11</declaredRunners>
<liveCommentary>1</liveCommentary>
<liveTab>1</liveTab>
<raceDescription>Class 6 Handicap</raceDescription>
<tvText>ATR </tvText>
</race>
</meeting>
</cards>
</root>
I can get the data i want which is essentially the race data (child nodes) by running this R code:
CardList=cbind(
date,
data.frame(raceid=xpathSApply(CardList_tmp, "//meeting/race", xmlGetAttr, 'id')),
data.frame(cards=xpathSApply(CardList_tmp, "//meeting/race", xmlGetAttr, 'details_available')),
data.frame(status=xpathSApply(CardList_tmp, "//meeting/race", xmlGetAttr, 'race_status_code')),
xmlToDataFrame(nodes = getNodeSet(CardList_tmp, "//meeting/race"))
)
However it doesn't contain the meeting data which is held at the parent attribute level:
course = xpathSApply(CardList_tmp, "//meeting", xmlGetAttr, 'name')
cid = xpathSApply(CardList_tmp, "//meeting", xmlGetAttr, 'id')
Is there a way i can combine the two sets of code together to provide one dataframe and in one step?
Here's options with xml2 for XML handling and the tidyverse for munging. The attributes (
xml_attrs
returns a named character vector), node names, and node values can be read into a three-element list that can be coerced to a data frame:which can in turn be cleaned up with a lot of tidyr:
This works, but the unnesting and spreading is fragile. Writing a more robust method is actually not too much more work, though, as you can just arrange the list columns before unnesting:
The most direct approach is to do the rearranging right while iterating over nodes. This is most concise and likely most efficient approach, but writing it correctly relies on careful manipulation of the data structures, so writing viable code may take longer.
All return the same data, though column order is different for
races_tidy
.Consider parsing meeting data by node index and expand it to the number of its child race elements, then column bind with race data:
Output
Alternatively, consider XSLT, the special-purpose langauge designed specifically to transform XML files such as flatter, simpler ones for your R needs. R can run XSLT 1.0 scripts with the
xslt
third-party package (extension ofxml2
).But also, XSLT is portable and can be run even outside R with Java, Python, PHP, or dedicated executables such as Saxon and Xalan. Below shows a
system
call to Unix's xsltproc. There is a similar batch call available for Windows. Once simplified, pass the new XML using XML'sxmlToDataframe
.Specifically, XSLT below parses down to race level and pulls meeting data from parent node.
XSLT (save as .xsl, a well-formed .xml file)
R