我有两个xml
a.xml
b.xml
下面是a.xml
文件
<proj_details>
<proj_detail>
<Username>Username</Username>
<ID>ID</ID>
<Place>Ocean</Place>
<City>Urban</City>
<!-- some more fields -->
</proj_detail>
<proj_detail>
<Username>abc@abc.com</Username>
<ID>QoChiX5FlqU7VB2MKw5Aa</ID>
<Place>Road</Place>
<City>Rural</City>
<!-- some more fields -->
</proj_detail>
<proj_detail>
<Username>abc@abc.com</Username>
<ID>uzGjRVxnZPtB9sfiXtga</ID>
<Place>Mud</Place>
<City>SemiRural</City>
<!-- some more fields -->
</proj_detail>
<proj_detail>
<Username>abc@abc.com</Username>
<ID>YChiX5FluqhuyVB2OPw5Aa</ID>
<Place>Sea</Place>
<City>Sealine</City>
<!-- some more fields -->
</proj_detail>
<proj_detail>
<Username>abc@abc.com</Username>
<ID>RD37oO2jgW5p6f76W8up</ID>
<Place>Lane</Place>
<City>Leftsidelane</City>
<!-- some more fields -->
</proj_detail>
</proj_details>
<!-- more fields -->
下面是b.xml
<FilebDetail>
<Number>34</Number>
<CONS>abc@abc.com</CONS>
<PROD>xyz@xyz.com</PROD>
<ID>QoChiX5FlqU7VB2MKw5Aa</ID>
<Name>Directory/notepad</Name>
<Count>43</Count>
<time>43550.53333336</time>
<!-- some more fields-->
</FilebDetail>
<FilebDetail>
<Number>35</Number>
<CONS>abc@abc.com</CONS>
<PROD>cvb@cvb.com</PROD>
<ID>uzGjRVxnZPtB9sfiXtga</ID>
<Name>Speed/morning</Name>
<Count>42</Count>
<time>43550.5778</time>
<!-- some more fields-->
</FilebDetail>
<FilebDetail>
<Number>31</Number>
<CONS>abc@abc.com</CONS>
<PROD>cvb@cvb.com</PROD>
<ID>RD37oO2jgW5p6f76W8up</ID>
<Name>Weatherincity</Name>
<Count>12</Count>
<time>43550.498611111114</time>
<!-- some more fields-->
</FilebDetail>
</FilebDetails>
现在,我想获得类似于下面的输出
<proj_detail>
<Number>34</Number>
<CONS>abc@abc.com</CONS>
<PROD>xyz@xyz.com</PROD>
<ID>QoChiX5FlNyqe1FuqU7VB2M</ID>
<Name>Directory/notepad</Name>
<Count>12</Count>
<time>43550.498611111114</time>
<Place>Road</Place>
<City>Rural</City>
<!-- some more fields -->
</proj_detail>
<proj_detail>
<Number>35</Number>
<CONS>abc@abc.com</CONS>
<PROD>cvb@cvb.com</PROD>
<ID>uzGjRVxnxmP00zrnZPtB9sM</ID>
<Name>Speed/morning</Name>
<Count>42</Count>
<time>43550.5778</time>
<Place>Mud</Place>
<City>SemiRural</City>
</proj_detail>
<!-- some more fields -->
<proj_detail>
<Number>31</Number>
<CONS>abc@abc.com</CONS>
<PROD>cvb@cvb.com</PROD>
<ID>RD37oO2jg4rlerxW5p6f76</ID>
<Name>Weatherincity</Name>
<Count>12</Count>
<time>43550.498611111114</time>
<Place>Lane</Place>
<City>Leftsidelane</City>
</proj_detail>
<!-- some more fields -->
</proj_details>
在两个文件中,ID都很常见,因此需要进行比较。比较之后,我们将打印两个文件中的相应字段。
我对xslt相当陌生。任何xslt版本都可以使用。
任何有关这方面的指导都会很有帮助。
谢谢
答案 0 :(得分:0)
如果您具有一对一的关系,合并将很简单。使用a.xml
作为输入并以b.xml
作为参数运行此XSLT 2.0样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="update"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="proj_detail">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates
select="$update/FilebDetails/FilebDetail[ID = current()/ID]/(* except ID)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
请注意:您必须注意XSLT处理器用于将节点作为参数传递的机制,否则将需要使用document()
函数。
通过此基本转换,您可以添加诸如使用key()
函数的优化(您将不得不使用XSLT 2.0 -arity 3- key()
函数)。同样,您可以通过ID
和“字段”名称声明该键,并假设$update
参数是一个更新流,因此您将选择最后一个,依此类推。