1. Background
We have a application that depends on several XML configuration files. The XML files define things like connection settings, polling frequencies, multiple user accounts (using XSD nested types/sequences) etc.
我们有一个依赖于几个XML配置文件的应用程序。 XML文件定义诸如连接设置,轮询频率,多个用户帐户(使用XSD嵌套类型/序列)等内容。
I have the XSD schema for these XML files. Small excerpt below:
我有这些XML文件的XSD架构。摘录如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="FooExch" type="CConfigFooType"/>
<xs:complexType name="CConfigFooType">
<xs:sequence>
<xs:element name="_appID" type="xs:string"/>
...
<xs:element name="_logins" type="FooLoginsType" maxOccurs="unbounded"/>
<xs:element name="_passwords" type="FooPasswordType" maxOccurs="unbounded"/>
...
</xs:sequence>
</xs:complexType>
<xs:complexType name="FooLoginsType">
<xs:sequence>
<xs:element name="_name" type="xs:string"/>
<xs:element name="_adapterID" type="xs:int"/>
<xs:element name="_FooLogins" type="FooAccountType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="FooAccountType">
<xs:sequence>
<xs:element name="_FooAccount" type="xs:string"/>
<xs:element name="_mktFeed" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="FooPasswordType">
<xs:sequence>
<xs:element name="_name" type="xs:string"/>
<xs:element name="_password" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
2. Objective
Our aim is to make it easier for users to tweak or add to settings.
我们的目标是让用户更容易调整或添加设置。
I'd like to offer a script that took a YAML file, and based on the XSD outputted a XML configuration (with default fallbacks for missing values, perhaps in a default.yaml
file.).
我想提供一个带有YAML文件的脚本,并根据XSD输出XML配置(缺省值的缺省回退,可能在default.yaml文件中。)。
We'd also need the ability to "append" a YAML to the XML - as in, a user could have a YAML which just defines their user account, and we'd import this to the existing list of user accounts in an existing XML file.
我们还需要能够将YAML“附加”到XML中 - 例如,用户可以拥有一个仅定义其用户帐户的YAML,我们将其导入到现有XML中的现有用户帐户列表中文件。
3. Approach
So far I'm using GenerateDS (http://www.rexx.com/~dkuhlman/generateDS.html) to generate a Python wrapper from the XSD.
到目前为止,我正在使用GenerateDS(http://www.rexx.com/~dkuhlman/generateDS.html)从XSD生成Python包装器。
Using that, I can create a Python object representing the XML schema, then export them to XML.
使用它,我可以创建一个表示XML模式的Python对象,然后将它们导出到XML。
The issue now is how do I go from a YAML to the XML?
现在的问题是如何从YAML转向XML?
Ideally, I'd like a generic loop that just ran over each value, and added it to the Python representation.
理想情况下,我想要一个只运行每个值的泛型循环,并将其添加到Python表示中。
My initial thought was to use getattr(config_wrapper, "yaml_name") = yaml_value
and iterate over each value in the YAML, then catch AttributeError
for anything that wasn't in the XSD.
我最初的想法是使用getattr(config_wrapper,“yaml_name”)= yaml_value并迭代YAML中的每个值,然后捕获不在XSD中的任何内容的AttributeError。
My first question - are any issues to this approach, or is there perhaps a more Pythonic/cleaner way of doing this? Is there a smarter way of tackling this question?
我的第一个问题 - 这种方法有什么问题,或者是否有更多Pythonic /更清洁的方法呢?是否有更聪明的方法来解决这个问题?
My second question is - with the above part of the XSD, we need to have nested logins, with username/passwords. I know PyYAML offers nested collections, but I'm still not quite sure how this would tie into GenerateDS, or how I could make it generic enough to work reliable.
我的第二个问题是 - 对于XSD的上述部分,我们需要使用用户名/密码进行嵌套登录。我知道PyYAML提供了嵌套集合,但是我仍然不太确定它如何与GenerateDS相关联,或者我如何使它具有通用性以使其可靠工作。
Cheers, Victor
1 个解决方案
#1
1
I'd avoid the XSD, sounds like a lot of bureaucracy for little gain.
我会避开XSD,听起来像很多官僚机构,收益不大。
- Create YAML file with same hierarchy as the XML file.
- Load file with pyyaml
- Loop over result creating the equivalent tree in elementtree.
- Write tree to XML file
- Send XML to system, when it complains, fix YAML and return to step 2.
创建与XML文件具有相同层次结构的YAML文件。
使用pyyaml加载文件
循环结果在elementtree中创建等效树。
将树写入XML文件
将XML发送到系统,当它抱怨时,修复YAML并返回到步骤2。
#1
1
I'd avoid the XSD, sounds like a lot of bureaucracy for little gain.
我会避开XSD,听起来像很多官僚机构,收益不大。
- Create YAML file with same hierarchy as the XML file.
- Load file with pyyaml
- Loop over result creating the equivalent tree in elementtree.
- Write tree to XML file
- Send XML to system, when it complains, fix YAML and return to step 2.
创建与XML文件具有相同层次结构的YAML文件。
使用pyyaml加载文件
循环结果在elementtree中创建等效树。
将树写入XML文件
将XML发送到系统,当它抱怨时,修复YAML并返回到步骤2。