【问题标题】:How to build an xml template with variable attributes from scratch如何从头开始构建具有可变属性的 xml 模板
【发布时间】:2014-04-06 17:20:14
【问题描述】:

我的目标是构建一个带有变量属性占位符的 xml 模板。由于某些原因,模板不会将新数据放入其占位符中。

这是一个例子:

x=2*5
xmlTemplate="""
<personal reference="500.txt">
    <others:sequence>
        <feature:name="name" age="age" dob="dob"/>
    </others:sequence>
</personal>""".format(name='Michael', age=x, dob=15/10/1900)
print xmlTemplate

输出:

<personal reference="500.txt">
    <others:sequence>
        <feature:name="name" age="age" dob="dob"/>
    </others:sequence>
</personal>

理想输出:

<personal reference="500.txt">
    <others:sequence>
        <feature:name="Michael" age="10" dob="15/10/1900"/>
    </others:sequence>
</personal>

有什么想法吗?谢谢。

【问题讨论】:

    标签: python xml


    【解决方案1】:

    要在 Python 中创建 XML 文档,使用 Yattag 库似乎更容易。

    from yattag import Doc
    
    doc, tag, text = Doc().tagtext()
    
    x = 2*5
    
    with tag('personal', reference = "500.txt"):
        with tag('others:sequence'):
            doc.stag('feature', name = "Michael", age = str(x), dob = "15/10/1900")
    
    print(doc.getvalue())
    

    上面的代码在运行时会生成如下的XML:

    <personal reference="500.txt">
      <others:sequence>
        <feature name="Michael" age="10" dob="15/10/1900" />
      </others:sequence>
    </personal>
    

    注意:为了便于阅读,上面的示例中添加了缩进,因为getvalue 返回的是单行,标签之间没有空格。要生成格式化文档,use the indent function

    【讨论】:

      【解决方案2】:

      您的模板需要花括号:

      x=2*5
      xmlTemplate="""
      <personal reference="500.txt">
          <others:sequence>
              <feature:name="{name}" age="{age}" dob="{dob}"/>
          </others:sequence>
      </personal>""".format(name='Michael', age=x, dob='15/10/1900')
      print xmlTemplate
      

      产量

      <personal reference="500.txt">
          <others:sequence>
              <feature:name="Michael" age="10" dob="15/10/1900"/>
          </others:sequence>
      </personal>
      

      format method 替换花括号中的名称。例如,比较

      In [20]: 'cheese'.format(cheese='Roquefort')
      Out[20]: 'cheese'
      
      In [21]: '{cheese}'.format(cheese='Roquefort')
      Out[21]: 'Roquefort'
      

      我看到你有lxml。优秀的。在这种情况下,您可以use lxml.builder to construct XML。这将帮助您创建有效的 XML:

      import lxml.etree as ET
      import lxml.builder as builder
      E = builder.E
      F = builder.ElementMaker(namespace='http://foo', nsmap={'others':'http://foo'})
      
      x = 2*5
      xmlTemplate = ET.tostring(F.root(
          E.personal(
              F.sequence(
                  E.feature(name='Michael',
                         age=str(x),
                         dob='15/10/1900')
                  ), reference="500.txt")),
                                pretty_print=True)
      print(xmlTemplate)
      

      产量

      <others:root xmlns:other="http://foo">
        <personal reference="500.txt">
          <others:sequence>
            <feature dob="15/10/1900" age="10" name="Michael"/>
          </others:sequence>
        </personal>
      </others:root>
      

      这个字符串可以被 lxml 解析:

      doc = ET.fromstring(xmlTemplate)
      print(doc)
      # <Element {http://foo}root at 0xb741866c>
      

      【讨论】:

      • 非常感谢。我第一次把大括号不带引号,没有用。然后我删除了它,我一直都知道我需要花括号,但不知道如何使用它们。
      • 我在解析输出 xml 时遇到问题。我使用 Lxml 并且它不会解析。你可以试一试吗?谢谢。
      • 需要定义命名空间前缀others。我在上面添加了一些代码,展示了如何在根元素中定义others
      • .format 会用撇号或引号解释变量吗? (description="Created by \"John Doe\" in 2016")
      • @StevenVascellaro:str.format 只执行文字字符串替换。要自动escape quotation marks,使用lxml.builder 很容易。
      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多