使用XSLT合并两个XML文件 [英] Merging two XML files with XSLT

查看:130
本文介绍了使用XSLT合并两个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找问题的解决方案.我发现了一些类似的问题和答案,但都没有一个适合我的问题.

I've searched for a solution for my problem. I found some similar questions and answers but none of them fitted to my problem.

我是XML新手,以前从未使用过XSLT.我有Linux,可以使用xsltproc或xmllint(或最好的).

I'm an XML newbie and never used XSLT before. I have Linux and could use xsltproc or xmllint (or whatever would be best).

问题很容易.我必须具有相同布局的XML文件.开头是一个文件中包含的节点的计数器.我只需要添加两个文件的计数器,然后将两个文件中的所有节点作为一个列表即可. (排序会更好.)

The problem is rather easy. I have to XML files with identical layout. At the beginning is a counter for the nodes included in one file. I just need the counters of both files added and then all nodes from both files as a single list. (Sorted would be even better.)

示例: a.xml

<?xml version="1.0" standalone="yes"?>
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/List.xsd">
  <publshInformation>
    <Publish_Date>12/17/2014</Publish_Date>
    <Record_Count>115</Record_Count>
  </publshInformation>
  <Entry>
    <uid>9639</uid>
    <firstName>Bob</firstName>
....
  </Entry>
</List>

b.xml

<?xml version="1.0" standalone="yes"?>
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/List.xsd">
  <publshInformation>
    <Publish_Date>12/17/2014</Publish_Date>
    <Record_Count>100</Record_Count>
  </publshInformation>
  <Entry>
    <uid>4711</uid>
    <firstName>John</firstName>
....
  </Entry>
</List>

结果: out.xml

Result: out.xml

<?xml version="1.0" standalone="yes"?>
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/List.xsd">
  <publshInformation>
    <Publish_Date>12/17/2014</Publish_Date>
    <Record_Count>215</Record_Count>
  </publshInformation>
  <Entry>
    <uid>4711</uid>
    <firstName>John</firstName>
....
  </Entry>
  <Entry>
    <uid>9639</uid>
    <firstName>Bob</firstName>
....
  </Entry>
</List>

我该如何处理?我之所以没有在这里发布XSLT,是因为它们不起作用,原因是我的技能有限.感谢您的任何建议!

How can I manage that? I don’t post my XSLTs here because they are not working and that’s because of my limited skills. Thanks for any suggestions!

推荐答案

以这种方式尝试.这里的想法是将XSL转换应用于文档a.xml,并将路径作为参数传递给b.xml文件.

Try it this way. The idea here is that you apply the XSL transformation to document a.xml, and pass the path to the b.xml file as a parameter.

您可能想要更改节点以进行更合理的排序.

You will probably want to change the node/s to sort on to something more reasonable.

请注意使用前缀来寻址XML源中的节点,因为它们都在命名空间中.

Note the use of a prefix to address the nodes in your XML sources, since they are all in a namespace.

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://tempuri.org/List.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="doc2" select="'b.xml'" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ns1:Record_Count">
    <xsl:copy>
        <xsl:value-of select=". + document($doc2)/ns1:List/ns1:publshInformation/ns1:Record_Count" />
    </xsl:copy>
</xsl:template>

<xsl:template match="ns1:List">
    <xsl:copy>
        <xsl:apply-templates select="*|document($doc2)/ns1:List/ns1:Entry">
            <xsl:sort select="ns1:firstName" data-type="text" order="ascending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这篇关于使用XSLT合并两个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆