创建XML内联xmlns

时间:2018-12-17 21:45:42

标签: python xml lxml

我正在Python中使用lxml来创建XML输出,最终项目将需要将xml导入Strava,因此在导入之前我需要正确的模式。下面是我创建的模式,我的问题是,如果我包含如下所示的XMLNS文本,我似乎无法正确设置格式。

<Extensions>
   <TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2">
    <Speed>0.0</Speed>
   </TPX>
</Extensions>

到目前为止,我的代码是:

def Trackpoint():
    E = lxml.builder.ElementMaker()
    Trackpoint = E.Trackpoint
    Time = E.Time
    Postion = E.Postion
    LatitudeDegrees = E.LatitudeDegrees
    LongitudeDegrees = E.LongitudeDegrees
    AltitudeMeters = E.AltitudeMeters
    DistanceMeters = E.DistanceMeters
    Cadence = E.Cadence
    Extensions = E.Extensions
    TPX = E.TPX
    Speed = E.Speed

    the_doc = Trackpoint(
        Time('2017-11-23T20:44:33Z'),
        Postion(
            LatitudeDegrees('32.2996560'),
            LongitudeDegrees('-64.7868500'),
        ),
        AltitudeMeters('16.4'),
        DistanceMeters('0.0'),
        Cadence('0'),
        Extensions(
            TPX(
                Speed('0.0'),
            )
        )
    )

    print lxml.etree.tostring(the_doc, pretty_print=True)

这是当前输出:

<Trackpoint>
  <Time>2017-11-23T20:44:33Z</Time>
  <Postion>
    <LatitudeDegrees>32.2996560</LatitudeDegrees>
    <LongitudeDegrees>-64.7868500</LongitudeDegrees>
  </Postion>
  <AltitudeMeters>16.4</AltitudeMeters>
  <DistanceMeters>0.0</DistanceMeters>
  <Cadence>0</Cadence>
  <Extensions>
    <TPX>
      <Speed>0.0</Speed>
    </TPX>
  </Extensions>
</Trackpoint>

1 个答案:

答案 0 :(得分:0)

您可以在ElementMaker中指定名称空间。您还可以在nsmap(命名空间映射)中使用None使其不加前缀。

示例...

from lxml import etree, builder


def Trackpoint():
    E = builder.ElementMaker()
    AE_E = builder.ElementMaker(namespace="http://www.garmin.com/xmlschemas/ActivityExtension/v2",
                                nsmap={None: "http://www.garmin.com/xmlschemas/ActivityExtension/v2"})
    Trackpoint = E.Trackpoint
    Time = E.Time
    Postion = E.Postion
    LatitudeDegrees = E.LatitudeDegrees
    LongitudeDegrees = E.LongitudeDegrees
    AltitudeMeters = E.AltitudeMeters
    DistanceMeters = E.DistanceMeters
    Cadence = E.Cadence
    Extensions = E.Extensions
    TPX = AE_E.TPX
    Speed = AE_E.Speed

    the_doc = Trackpoint(
        Time('2017-11-23T20:44:33Z'),
        Postion(
            LatitudeDegrees('32.2996560'),
            LongitudeDegrees('-64.7868500'),
        ),
        AltitudeMeters('16.4'),
        DistanceMeters('0.0'),
        Cadence('0'),
        Extensions(
            TPX(
                Speed('0.0'),
            )
        )
    )

    print etree.tostring(the_doc, pretty_print=True)


Trackpoint()

输出...

<Trackpoint>
  <Time>2017-11-23T20:44:33Z</Time>
  <Postion>
    <LatitudeDegrees>32.2996560</LatitudeDegrees>
    <LongitudeDegrees>-64.7868500</LongitudeDegrees>
  </Postion>
  <AltitudeMeters>16.4</AltitudeMeters>
  <DistanceMeters>0.0</DistanceMeters>
  <Cadence>0</Cadence>
  <Extensions>
    <TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2">
      <Speed>0.0</Speed>
    </TPX>
    <Speed>test</Speed>
  </Extensions>
</Trackpoint>