xmlns 属性导致 xpath 查询失败
xmlns attribute causes xpath queries to fail
当根元素的属性 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[=29] 进行验证=],并且可以使用这两个来源代替非功能性 link 到 http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 。我使用 this-bash-script 与来自 garmin 先行者 305 的数据一起创建 xml 数据。
最简单的方法是告诉 xmlR 包 XML 中的 Parse() 忽略 xmln 属性。我该怎么做?
另一种方法是将 xlmns 属性的内容更改为有效的东西在这种情况下需要什么,xsd-scheme 和/或 xslt-file 在这里有帮助吗?
您需要为 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"
当根元素的属性 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[=29] 进行验证=],并且可以使用这两个来源代替非功能性 link 到 http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 。我使用 this-bash-script 与来自 garmin 先行者 305 的数据一起创建 xml 数据。
最简单的方法是告诉 xmlR 包 XML 中的 Parse() 忽略 xmln 属性。我该怎么做?
另一种方法是将 xlmns 属性的内容更改为有效的东西在这种情况下需要什么,xsd-scheme 和/或 xslt-file 在这里有帮助吗?
您需要为 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"