我有两个格式如下的XML文件 档案1
<info>
<organization_id>123</organization_id>
<total_patients>5</total_patients>
</info>
<patients>
<patient>
<guid>1111</guid>
</patient>
<patient>
<guid>2222</guid>
</patient>
</patients>
文件2
<info>
<organization_id>456</organization_id>
<total_patients>10</total_patients>
</info>
<patients>
<patient>
<guid>3333</guid>
</patient>
<patient>
<guid>4444</guid>
</patient>
</patients>
我想将2个文件合并为一个格式为
的文件输出文件
<info>
<total_patients>15</total_patients>
</info>
<organizations>
<organization>
<organization_id>123</organization_id>
<patients>
<patient>
<guid>1111</guid>
</patient>
<patient>
<guid>1111</guid>
</patient>
</patients>
</organization>
<organization>
<organization_id>456</organization_id>
<patients>
<patient>
<guid>4444</guid>
</patient>
<patient>
<guid>5555</guid>
</patient>
</patients>
</organization>
</organizations>
我将不胜感激任何建议。我一直在搜索谷歌,找不到任何类似的东西。谢谢
答案 0 :(得分:4)
进行一些调整以使您的输入和预期输出形成良好,我建议使用以下XSLT 2.0解决方案。调整是:
所以,转换以下文件......
<organization>
<info>
<organization_id>123</organization_id>
<total_patients>5</total_patients>
</info>
<patients>
<patient>
<guid>1111</guid>
</patient>
<patient>
<guid>2222</guid>
</patient>
</patients>
</organization>
包括以下共同输入文件......
<organization>
<info>
<organization_id>456</organization_id>
<total_patients>10</total_patients>
</info>
<patients>
<patient>
<guid>3333</guid>
</patient>
<patient>
<guid>4444</guid>
</patient>
</patients>
</organization>
...将在您调用XSLT引擎作为名为“org2”的参数时引用,应用以下XSLT2转换......
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0"
exclude-result-prefixes="xsl xs fn">
<xsl:output indent="yes" encoding="UTF-8" />
<xsl:param name="org2" />
<xsl:variable name="org2-doc" select="document($org2)" />
<xsl:template match="/">
<output>
<info>
<total_patients>
<xsl:number value="organization/info/total_patients +
$org2-doc/organization/info/total_patients" />
</total_patients>
</info>
<organizations>
<xsl:apply-templates />
<xsl:apply-templates select="$org2-doc/*" />
</organizations>
</output>
</xsl:template>
<xsl:template match="organization">
<organization>
<organization_id>
<xsl:number value="info/organization_id" />
</organization_id>
<xsl:copy-of select="patients" />
</organization>
</xsl:template>
</xsl:stylesheet>
...产生了预期的结果......
<?xml version="1.0" encoding="UTF-8"?>
<output>
<info>
<total_patients>15</total_patients>
</info>
<organizations>
<organization>
<organization_id>123</organization_id>
<patients>
<patient>
<guid>1111</guid>
</patient>
<patient>
<guid>2222</guid>
</patient>
</patients>
</organization>
<organization>
<organization_id>456</organization_id>
<patients>
<patient>
<guid>3333</guid>
</patient>
<patient>
<guid>4444</guid>
</patient>
</patients>
</organization>
</organizations>
</output>
答案 1 :(得分:2)
使用您熟悉的任何语言的XML DOM解析器。
解析两个文件,提取组织ID。
创建新的DOM树,新的<info>
节点,新的<organizations>
节点。对于每个组织,请复制相应组织节点下的<patients>
个节点。
序列化生成的XML。
(如果你这么倾向,你可以试试XSLT,但是这条路虽然可能更短,但可能会感觉某种自虐。)
不,我实在为你编写工作代码太懒了。