SSIS XML任务中的XSLT转换仅输出XML文档的某些节点

时间:2017-01-16 16:54:58

标签: xml xslt ssis

给出以下XML文档。

<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase
  xsi:schemaLocation="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd"
  xmlns:ns5="http://www.garmin.com/xmlschemas/ActivityGoals/v1"
  xmlns:ns3="http://www.garmin.com/xmlschemas/ActivityExtension/v2"
  xmlns:ns2="http://www.garmin.com/xmlschemas/UserProfile/v2"
  xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.garmin.com/xmlschemas/ProfileExtension/v1">
  <Activities>
    <Activity Sport="Other">
      <Id>2017-01-13T19:26:50.000Z</Id>
      <Lap StartTime="2017-01-13T19:26:50.000Z">
        <TotalTimeSeconds>600.0</TotalTimeSeconds>
        <DistanceMeters>0.0</DistanceMeters>
        <Calories>42</Calories>
        <AverageHeartRateBpm>
          <Value>89</Value>
        </AverageHeartRateBpm>
        <MaximumHeartRateBpm>
          <Value>100</Value>
        </MaximumHeartRateBpm>
        <Intensity>Active</Intensity>
        <TriggerMethod>Manual</TriggerMethod>
        <Track>
          <Trackpoint>
            <Time>2017-01-13T19:26:50.000Z</Time>
            <AltitudeMeters>-51.599998474121094</AltitudeMeters>
            <HeartRateBpm>
              <Value>84</Value>
            </HeartRateBpm>
            <Extensions>
              <ns3:TPX/>
            </Extensions>
          </Trackpoint>
          <Trackpoint>
            <Time>2017-01-13T19:26:51.000Z</Time>
            <AltitudeMeters>-51.599998474121094</AltitudeMeters>
            <HeartRateBpm>
              <Value>84</Value>
            </HeartRateBpm>
            <Extensions>
              <ns3:TPX/>
            </Extensions>
          </Trackpoint>
          <Trackpoint>
            <Time>2017-01-13T19:26:54.000Z</Time>
            <AltitudeMeters>-0.6000000238418579</AltitudeMeters>
            <HeartRateBpm>
              <Value>84</Value>
            </HeartRateBpm>
            <Extensions>
              <ns3:TPX/>
            </Extensions>
          </Trackpoint>
        </Track>
        <Extensions>
          <ns3:LX/>
        </Extensions>
      </Lap>
      <Creator xsi:type="Device_t">
        <Name>Garmin Forerunner 910XT</Name>
        <UnitId>3881635667</UnitId>
        <ProductID>1328</ProductID>
        <Version>
          <VersionMajor>3</VersionMajor>
          <VersionMinor>20</VersionMinor>
          <BuildMajor>0</BuildMajor>
          <BuildMinor>0</BuildMinor>
        </Version>
      </Creator>
    </Activity>
  </Activities>
  <Author xsi:type="Application_t">
    <Name>Garmin Connect API</Name>
    <Build>
      <Version>
        <VersionMajor>16</VersionMajor>
        <VersionMinor>23</VersionMinor>
        <BuildMajor>0</BuildMajor>
        <BuildMinor>0</BuildMinor>
      </Version>
    </Build>
    <LangID>en</LangID>
    <PartNumber>006-D2449-00</PartNumber>
  </Author>
</TrainingCenterDatabase>

以下的XSLT。 (我一直在尝试各种不同的版本,但我甚至无法导出任何东西)

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gar="http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="gar:TrainingCenterDatabase/gar:Activities/gar:Activity/gar:Track"/>
</xsl:template>
</xsl:stylesheet>

我正在试图仅导出XML的某些节点。

EDIT。在XML的命名空间中添加。 编辑。添加在命名空间和别名中。

我想要的是这个。

  <Trackpoint>
    <Time>2017-01-13T19:26:54.000Z</Time>
    <HeartRateBpm>84</HeartRateBpm>
  </Trackpoint>
  <Trackpoint>
    <Time>2017-01-13T19:26:54.000Z</Time>
    <HeartRateBpm>84</HeartRateBpm>
  </Trackpoint>
  <Trackpoint>
    <Time>2017-01-13T19:27:54.000Z</Time>
    <HeartRateBpm>85</HeartRateBpm>
  </Trackpoint>
  <Trackpoint>
    <Time>2017-01-13T19:28:54.000Z</Time>
    <HeartRateBpm>90</HeartRateBpm>
  </Trackpoint>
  etc.

感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

目前的尝试不起作用有两个原因:

  1. 输入XML使用的默认命名空间是 "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2",不是 "http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd"为 你在样式表中声明;

  2. Track元素的路径不正确 - 您错过了。{ Lap步骤。

  3. 修复这两个会得到一个结果,它是Track元素的深层副本。

    但是,我怀疑这是你想要的结果,因为它将按复制 - 包括默认命名空间。为了获得无命名空间的结果,您需要创建新元素而不是从源复制。试试,例如:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:gar="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"
    exclude-result-prefixes="gar">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/gar:TrainingCenterDatabase">
        <Track>
            <xsl:for-each select="gar:Activities/gar:Activity/gar:Lap/gar:Track/gar:Trackpoint">
                <Trackpoint>
                    <Time>
                        <xsl:value-of select="gar:Time"/>                   
                    </Time>
                    <HeartRateBpm>
                        <xsl:value-of select="gar:HeartRateBpm/gar:Value"/>
                    </HeartRateBpm>
                </Trackpoint>
            </xsl:for-each>
        </Track>
    </xsl:template>
    
    </xsl:stylesheet>
    

    应用于您的输入示例,结果将是:

    <?xml version="1.0" encoding="UTF-8"?>
    <Track>
       <Trackpoint>
          <Time>2017-01-13T19:26:50.000Z</Time>
          <HeartRateBpm>84</HeartRateBpm>
       </Trackpoint>
       <Trackpoint>
          <Time>2017-01-13T19:26:51.000Z</Time>
          <HeartRateBpm>84</HeartRateBpm>
       </Trackpoint>
       <Trackpoint>
          <Time>2017-01-13T19:26:54.000Z</Time>
          <HeartRateBpm>84</HeartRateBpm>
       </Trackpoint>
    </Track>