【问题标题】:lxml and python: how to set "global" entitieslxml 和 python:如何设置“全局”实体
【发布时间】:2018-05-29 10:09:04
【问题描述】:

我正在使用 lxml 在 python 中解析一个 xml 文件。 xml 的目的是配置我的代码。由于我需要使用许多略有不同的不同配置,因此我使用了xi:include 功能。

能够在“主”xml 文件中声明一些实体并让包含的实体知道它们会非常有用。 这是我的工作示例:

ma​​in.xml

<?xml version="1.0"?>
<!DOCTYPE doc> 
<configuration xmlns:xi="http://www.w3.org/2001/XInclude" name="foo" version="0.1" >
 <xi:include href="./external.xml" />
</configuration>

external.xml

<?xml version="1.0"?>
<!DOCTYPE doc [
       <!ENTITY bar "example">
       ]>
<objects>
   <object name="&bar;" />
</objects>

我想做的是在 main.xml 中声明 bar,可以这样做吗? 提前致谢, 马特奥

【问题讨论】:

  • 我想不出一个好的解决方案。如果从 external.xml 中删除实体声明,则包含将失败。被包含的文档在被包含之前被解析,未解析的实体引用会产生错误。
  • 嗨@mzjn,感谢您的评论。我找到了解决方法。我在一个新的 xml 中收集了所有不同的配置,并将它们包装成一些虚构的节点,我通过 xpointer 功能选择这些节点。如果这可能引起普遍兴趣,我可以在这里举一个例子
  • 如果您有解决问题的解决方案,我认为您应该将其发布为答案。

标签: python xml lxml dtd xinclude


【解决方案1】:

这不是我正在寻找的解决方案,但有点解决了我的问题:

ma​​in.xml

<?xml version="1.0"?>
<!DOCTYPE doc>
<configurations>
    <configuration>
    <xi:include href="./external/confs.xml" xpointer="xpointer(/ext/conf1)" />
    </configuration> 

    <configuration>
    <xi:include href="./external/confs.xml" xpointer="xpointer(/ext/conf2)" />
    </configuration> 
</configurations>

confs.xml

<?xml version="1.0"?>
<!DOCTYPE doc[
    <!ENTITY foo "bar">
]>
    <ext>
       <conf1>
           <inc name="apple_&foo;" />   
       </conf1>

       <conf2>
           <inc name="orange_&foo;" />
       </conf2>

    </ext> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多