我有两个要合并的 XML 文件。
XML1:
<hierachyAttributes>
<attribute>
<displayOrder>2</displayOrder>
<attributeID>Demographics</attributeID>
<children>
<attribute>
<displayOrder>1</displayOrder>
<attributeID>age</attributeID>
</children>
</attribute>
</hierachyAttributes>
XML2:
<diseaseAttributes>
<diseaseName>Cancer</diseaseName>
<diseaseID>1322843</diseaseID>
<metaAttributes>
<attribute>
<description>Age</description>
<displayName>Age (years)</displayName>
<attributeID>age</attributeID>
<type>Double</type>
<attributeCategory>Clinical</attributeCategory>
<displayInSummary>TRUE</displayInSummary>
<group>
<displayOrder>1</displayOrder>
<displayName>0 - < 10</displayName>
<minValue>0</minValue>
<minInclusive>TRUE</minInclusive>
<maxValue>10</maxValue>
<maxInclusive>FALSE</maxInclusive>
</group>
</valueGroups>
</attribute>
</metaAttributes>
</diseaseAttributes>
有没有办法像下面那样合并它们,即使根标签不同,在本例中是 hierachyAttributes 和 diseaseAttributes? 组合 XML:
<hierachyAttributes>
<diseaseAttributes>
<diseaseName>Cancer</diseaseName>
<diseaseID>1322843</diseaseID>
<metaAttributes>
<attribute>
<displayOrder>2</displayOrder>
<attributeID>Demographics</attributeID>
<children>
<attribute>
<displayOrder>1</displayOrder>
<attributeID>age</attributeID>
<description>Age</description>
<displayName>Age (years)</displayName>
<type>Double</type>
<attributeCategory>Clinical</attributeCategory>
<displayInSummary>TRUE</displayInSummary>
<group>
<displayOrder>1</displayOrder>
<displayName>0 - < 10</displayName>
<minValue>0</minValue>
<minInclusive>TRUE</minInclusive>
<maxValue>10</maxValue>
<maxInclusive>FALSE</maxInclusive>
</group>
</valueGroups>
</attribute>
</children>
</metaAttributes>
</diseaseAttributes>
</hierachyAttributes>
即,在 attributeID 相同的地方合并它们。我尝试了以下但它连接了一个接一个的 xml 文件。
#!/usr/bin/env python
import sys
from xml.etree import ElementTree
def run(files):
first = None
for filename in files:
data = ElementTree.parse(filename).getroot()
if first is None:
first = data
else:
first.extend(data)
if first is not None:
print ElementTree.tostring(first)
if __name__ == "__main__":
run(sys.argv[1:])
或者,如果标签被替换为并且我想要相同的输出但在一个根节点下,即 diseaseAttributes,我该如何实现?
最佳答案
您的第一个 XML 文件缺少结尾 </attribute>在 <children> 下标记.它们在结构方面也绝对糟糕 - 冗长得可笑且命名困惑,以至于我实际上认为我无法分辨出您要做什么。
第一个文件看起来好像只是在表达“属性”的关系树。这是我没有得到的第二个 - 它似乎包含属性“年龄”的定义和名称,它是什么类型的数据,但它是下面“癌症”的一部分。为什么?我的猜测是您将显示按年龄分割的结果,但为什么年龄与巨蟹座有关?如果你有年龄数据会发生什么冬季死于流感,是否有其独特的年龄属性?
实际上,我的第一个问题是……XML2 应该如何工作:
<disease-definitions>
<disease-definition id="1322843">
<name>Cancer</name>
<attribute-definitions>
<attribute id="age" category="Clinical">
<description>Age</description>
<displayName>Age (years)</name>
<type>Double</type>
<attribute-summary displayed="true">
<group>
<displayName>< 10</displayName>
<range type="half-open">
<min>0</min>
<max>10</max>
</range>
</group>
<group>
<displayName>10 - 20</displayName>
<range type="half-open">
<min>10</min>
<max>20</max>
</range>
</group>
</attribute-summary>
</attribute>
</attribute-definitions>
</disease-definition>
<disease-definition id="1322844">
<name>Influenza</name>
<attribute-definitions>
<attribute id="age" category="Clinical">
<description>Age</description>
<displayName>Age (years)</name>
<type>Double</type>
<attribute-summary displayed="true">
<group>
<displayName>Children</displayName>
<range type="half-open">
<min>0</min>
<max>18</max>
</range>
</group>
<group>
<displayName>Adults</displayName>
<range type="half-open">
<min>18</min>
<max>60</max>
</range>
</group>
<group>
<displayName>Elderly</displayName>
<range type="half-open">
<min>60</min>
</range>
</group>
</attribute-summary>
</attribute>
</attribute-definitions>
</disease-definition>
<disease-definitions>
因为这似乎就是您的意思,即使我做得更小也很可怕。而且我不确定层次结构信息如何适应那里。
属性及其层次结构是否仅用于显示数据?即便如此,这似乎更好
<attribute id="demographics">
<title>Demographics</title>
<children>
<child id="age" />
<child id="gender" />
</children>
</attribute>
<attribute id="epidemiology">
<title>Epidemiology</title>
<children>
<child id="reported-date" />
<child id="variant-strains" />
</children>
</attribute>
<attribute id="age">
<title>Age</title>
<description>Age in years</description>
<category>Clinical</category>
<data type="double">
<min-value>0</min-value>
</data>
</attribute>
<attribute id="gender">
<title>Gender</title>
<data type="options">
<one-of>
<option id="M">
<title>Male</title>
</option>
<option id="F">
<title>Female</title>
</option>
</one-pf>
</data>
</attribute>
然后
<disease-definitions>
<disease id="1322843">
<displayName>Cancer</displayName>
<disease-attributes>
<attribute ref-id="age">
<displayName>Age of death</displayName>
<displayed-in-summary>true</displayed-in-summary>
<display format="histogram">
<range max="10">Up to 10</range>
<range min="10" max="25">Teenagers & young adults</range>
<range min="25" max="55">Adults</range>
<range min="55">Elderly</range>
</display-data>
<display
</attribute>
<attribute ref-id="gender">
<displayName>Gender of death</displayName>
<displayed-in-summary>true</displayed-in-summary>
<display format="pie">
<slice option-id="M" background="#44F">Male deaths</slice>
<slice option-id="F" background="#F44">Female deaths</slice>
</display-data>
<display
</attribute>
</disease-attributes>
</disease>
<disease id="1322844">
<displayName>Influenza</displayName>
<disease-attributes>
<attribute ref-id="age">
<displayName>Age of death</displayName>
<displayed-in-summary>true</displayed-in-summary>
<display-data format="grouped">
<range max="10">Up to 10</range>
<range min="10" max="25">Teenagers & young adults</range>
<range min="25" max="55">Adults</range>
<range min="55">Elderly</range>
</display-data>
<display
</attribute>
</disease-attributes>
</disease>
</disease-definitions>
关于python - 在属性 ID 相同的地方合并 XML 文件 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27159906/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val