通过SSIS从TCX文件将时间和心率数据导入SQL Server

时间:2017-01-13 18:50:29

标签: sql-server xml ssis xsd

我正在尝试将Garmin连接的tcx文件导入带有SSIS的SQL Server。不幸的是,我完全失去了如何创建一个xsd以便导入我想要的数据点。 Garmin XSD是http://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd。我关心的部分只是这里的要素:

<xsd:complexType name="Trackpoint_t">
    <xsd:sequence>
        <xsd:element type="xsd:dateTime" name="Time"/>
        <xsd:element type="Position_t" name="Position" minOccurs="0"/>
        <xsd:element type="xsd:double" name="AltitudeMeters" minOccurs="0"/>
        <xsd:element type="xsd:double" name="DistanceMeters" minOccurs="0"/>
        <xsd:element type="HeartRateInBeatsPerMinute_t" name="HeartRateBpm" minOccurs="0"/>
        <xsd:element type="CadenceValue_t" name="Cadence" minOccurs="0"/>
        <xsd:element type="SensorState_t" name="SensorState" minOccurs="0"/>
        <xsd:element type="Extensions_t" name="Extensions" minOccurs="0">
            <xsd:annotation>
        </xsd:element>
    </xsd:sequence>

有关如何创建xsd的任何指示都会有所帮助。感谢

2 个答案:

答案 0 :(得分:1)

如果您想在不进行修改的情况下导入部分XML文件,则无需设计自己的xsd。您所需要做的就是:

  • 从Garmit获取XSD文件(假设它有效并与您的数据保持一致)和您的XML文件
  • 在SSIS包中,添加数据流任务,XML源,选择XML文件作为源,Garmin XSD文件作为模式文件
  • 或者,您可以尝试这一点,XML Source可以从提供的XML设计(猜测)XSD。我推荐这只作为一个起点,因为猜测XSD非常近似,仅基于提供的样本;它可以根据样本误解节点和属性类型。
  • XML Source通常会生成大量输出,因此您必须检查数据查看器哪一个包含您需要的数据。

使用此原型后,您可以根据需要改变包装。

答案 1 :(得分:1)

您没有提供XML文件......但我找到了一个sample of a TCX-file hereanother one here。读这个不需要架构。我希望,当您看到以下示例时,您可以解决您的问题:

    DECLARE @xml XML=
    N'<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">
  <Activities>
    <Activity Sport="Running">
      <Id>2015-01-25T12:14:34Z</Id>
      <Lap StartTime="2015-01-25T12:14:34Z">
        <TotalTimeSeconds>507.0989990</TotalTimeSeconds>
        <DistanceMeters>1000.0000000</DistanceMeters>
        <MaximumSpeed>2.5790000</MaximumSpeed>
        <Calories>95</Calories>
        <AverageHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
          <Value>155</Value>
        </AverageHeartRateBpm>
        <MaximumHeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
          <Value>173</Value>
        </MaximumHeartRateBpm>
        <Intensity>Resting</Intensity>
        <TriggerMethod>Distance</TriggerMethod>
        <Track>
          <Trackpoint>
            <Time>2015-01-25T12:14:34Z</Time>
            <Position>
              <LatitudeDegrees>50.8918607</LatitudeDegrees>
              <LongitudeDegrees>16.7403161</LongitudeDegrees>
            </Position>
            <AltitudeMeters>233.1999969</AltitudeMeters>
            <DistanceMeters>0.0000000</DistanceMeters>
            <HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
              <Value>88</Value>
            </HeartRateBpm>
            <Extensions>
              <TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2" CadenceSensor="Footpod">
                <Speed>0.0000000</Speed>
              </TPX>
            </Extensions>
          </Trackpoint>
        </Track>
        <Track />
        <Extensions>
          <FatCalories xmlns="http://www.garmin.com/xmlschemas/FatCalories/v1">
            <Value>0</Value>
          </FatCalories>
          <LX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2">
            <AvgSpeed>1.9720000</AvgSpeed>
          </LX>
        </Extensions>
      </Lap>
    </Activity>
  </Activities>
</TrainingCenterDatabase>';

WITH XMLNAMESPACES(DEFAULT 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2')
select Act.value(N'@Sport','nvarchar(max)') AS Activity_Sport
      ,Act.value(N'(Id)[1]','datetime') AS Activity_Id
      ,Lap.value(N'@StartTime','datetime') AS Lap_StartTime
      ,Lap.value(N'(TotalTimeSeconds)[1]','decimal(20,8)') AS Lap_TotalTimeSeconds
      --more fields
      ,Lap.value(N'(Calories)[1]','int') AS Lap_TotalTimeSeconds
      ,Lap.value(N'(AverageHeartRateBpm/Value)[1]','int') AS Lap_AverageHeartRateBpm
      ,Lap.value(N'(MaximumHeartRateBpm/Value)[1]','int') AS Lap_MaximumHeartRateBpm
      ,Lap.value(N'(Intensity)[1]','nvarchar(max)') AS Lap_Intensity
      --more fields
      ,TrP.value(N'(Time)[1]','datetime') AS Track_Trackpoint_Time
      ,TrP.value(N'(Position/LatitudeDegrees)[1]','decimal(20,8)') AS Track_Trackpoint_Position_LatitudeDegrees
      --more fields
FROM @xml.nodes(N'/TrainingCenterDatabase/Activities/Activity') AS A(Act)
OUTER APPLY Act.nodes(N'Lap') AS B(Lap)
OUTER APPLY Lap.nodes(N'Track/Trackpoint') AS C(TrP)

结果

Activity_Sport  Activity_Id             Lap_StartTime           Lap_TotalTimeSeconds    ....
Running         2015-01-25 12:14:34.000 2015-01-25 12:14:34.000 507.09899900            ....