【发布时间】:2021-10-30 06:19:49
【问题描述】:
我有一些健康数据的 xml 文档,这是默认的苹果健康数据库导出格式。我在这里截断了该文档,但它是一个有效的 xml 文档。如何选择与另一个属性匹配的所有“值”属性?例如,如何获得所有 type="HKQuantityTypeIdentifierBodyMass" 的矩阵或数据框?还是我所有的身高?
我尝试过类似的方法,但它只返回空值。
library(xml2)
xml <- read_xml('<?xml version="1.0" encoding="UTF-8"?>
<HealthData locale="en_US">
<ExportDate value="2021-10-29 20:24:26 -0700"/>
<Me HKCharacteristicTypeIdentifierDateOfBirth="1975-05-18" HKCharacteristicTypeIdentifierBiologicalSex="HKBiologicalSexMale" HKCharacteristicTypeIdentifierBloodType="HKBloodTypeNotSet" HKCharacteristicTypeIdentifierFitzpatrickSkinType="HKFitzpatrickSkinTypeNotSet" HKCharacteristicTypeIdentifierCardioFitnessMedicationsUse="None"/>
<Record
type="HKQuantityTypeIdentifierHeight"
sourceName="Neil’s Apple Watch"
sourceVersion="2.1"
unit="ft"
creationDate="2016-02-23 14:12:38 -0700"
startDate="2016-02-23 14:12:38 -0700"
endDate="2016-02-23 14:12:38 -0700"
value="6.16667"/>
<Record type="HKQuantityTypeIdentifierHeight" sourceName="Neil’s Apple Watch" sourceVersion="2.2.1" unit="ft" creationDate="2016-08-17 08:00:37 -0700" startDate="2016-08-17 08:00:37 -0700" endDate="2016-08-17 08:00:37 -0700" value="6.16667"/>
<Record type="HKQuantityTypeIdentifierBodyMass" sourceName="Neil’s Apple Watch" sourceVersion="2.1" unit="lb" creationDate="2016-02-23 14:12:38 -0700" startDate="2016-02-23 14:12:38 -0700" endDate="2016-02-23 14:12:38 -0700" value="175"/>
<Record type="HKQuantityTypeIdentifierBodyMass" sourceName="Neil’s Apple Watch" sourceVersion="2.2.1" unit="lb" creationDate="2016-08-17 08:00:36 -0700" startDate="2016-08-17 08:00:36 -0700" endDate="2016-08-17 08:00:36 -0700" value="180"/>
<Record type="HKQuantityTypeIdentifierBodyMass"
sourceName="Neils Apple Watch"
sourceVersion="2.1"
unit="lb"
creationDate="2016-02-23 14:12:38 -0700"
startDate="2016-02-23 14:12:38 -0700"
endDate="2016-02-23 14:12:38 -0700"
value="175"/>
</HealthData>')
listOfAllMyHeights <- xml_text(xml, "???")
listOfAllMyWeights <- xml_something("???")
print(heights)```
【问题讨论】: