当根元素的属性xmlns设置为(非功能)URI“http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2”时,xpath查询失败。如果没有该属性,xpath查询将按预期工作。
以下两个块是问题的最小工作示例
foo <- xmlParse(file='<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase>
<Activities>
<Activity Sport="Other">
<Id>2013-08-31T09:01:43Z</Id>
<Lap StartTime="2013-08-31T09:01:43Z">
<TotalTimeSeconds>1928.82</TotalTimeSeconds>
<DistanceMeters>11250.1543</DistanceMeters>
<MaximumSpeed>15.6822205</MaximumSpeed>
<Calories>631</Calories>
<AverageHeartRateBpm><Value>126</Value></AverageHeartRateBpm>
<MaximumHeartRateBpm><Value>151</Value></MaximumHeartRateBpm>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track><Trackpoint>
<Time>2013-08-31T09:01:43Z</Time>
<Position><LatitudeDegrees>57.91393099</LatitudeDegrees><LongitudeDegrees>11.77463514</LongitudeDegrees></Position>
<AltitudeMeters>55.4130859</AltitudeMeters>
<DistanceMeters>0.00000000</DistanceMeters>
<HeartRateBpm><Value>77</Value></HeartRateBpm>
</Trackpoint></Track></Lap></Activity></Activities></TrainingCenterDatabase>
')
xpathSApply(foo,'//*/Time',xmlValue)
[1] "2013-08-31T09:01:43Z"
非工作版
foo <- xmlParse(file='<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Other">
<Id>2013-08-31T09:01:43Z</Id>
<Lap StartTime="2013-08-31T09:01:43Z">
<TotalTimeSeconds>1928.82</TotalTimeSeconds>
<DistanceMeters>11250.1543</DistanceMeters>
<MaximumSpeed>15.6822205</MaximumSpeed>
<Calories>631</Calories>
<AverageHeartRateBpm><Value>126</Value></AverageHeartRateBpm>
<MaximumHeartRateBpm><Value>151</Value></MaximumHeartRateBpm>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track><Trackpoint>
<Time>2013-08-31T09:01:43Z</Time>
<Position><LatitudeDegrees>57.91393099</LatitudeDegrees><LongitudeDegrees>11.77463514</LongitudeDegrees></Position>
<AltitudeMeters>55.4130859</AltitudeMeters>
<DistanceMeters>0.00000000</DistanceMeters>
<HeartRateBpm><Value>77</Value></HeartRateBpm>
</Trackpoint></Track></Lap></Activity></Activities></TrainingCenterDatabase>
')
xpathSApply(foo,'//*/Time',xmlValue)
list()
我的理解是xml数据是使用此xslt-file生成的,它会针对此xsd-scheme进行验证,并且可以使用这两个来源代替{{3}的无效链接}。我使用http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2与来自garmin forerunner 305的数据一起创建xml数据。
简单的方法是从R包XML告诉xmlParse()忽略xmln属性。我该怎么做?
另一种方法是将xlmns属性的内容更改为有效的内容在这种情况下需要什么,xsd-scheme和/或xslt-file可以在这里帮助吗?
答案 0 :(得分:1)
您需要为xpathSApply
提供名称空间参数并适当调整您的xpath:
foo <- xmlParse(file='<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Other">
<Id>2013-08-31T09:01:43Z</Id>
<Lap StartTime="2013-08-31T09:01:43Z">
<TotalTimeSeconds>1928.82</TotalTimeSeconds>
<DistanceMeters>11250.1543</DistanceMeters>
<MaximumSpeed>15.6822205</MaximumSpeed>
<Calories>631</Calories>
<AverageHeartRateBpm><Value>126</Value></AverageHeartRateBpm>
<MaximumHeartRateBpm><Value>151</Value></MaximumHeartRateBpm>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track><Trackpoint>
<Time>2013-08-31T09:01:43Z</Time>
<Position><LatitudeDegrees>57.91393099</LatitudeDegrees><LongitudeDegrees>11.77463514</LongitudeDegrees></Position>
<AltitudeMeters>55.4130859</AltitudeMeters>
<DistanceMeters>0.00000000</DistanceMeters>
<HeartRateBpm><Value>77</Value></HeartRateBpm>
</Trackpoint></Track></Lap></Activity></Activities></TrainingCenterDatabase>
')
xpathSApply(foo,'//*/ns:Time',xmlValue
, namespaces = c(ns = "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"))
> xpathSApply(foo,'//*/ns:Time',xmlValue
+ , namespaces = c(ns = "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"))
[1] "2013-08-31T09:01:43Z"