xml使用xsl合并两个文件?
我需要合并两个相似的 xml 文件,但仅记录与常见标记匹配的记录,例如以下示例中的
:
file1.xml 是
<node>
<type>a</type>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
file2.xml,
<node>
<type>a</type>
<name>jill</name>
</node>
这样我的输出为
<node>
<type>a</type>
<name>jill</name>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
在 xsl 中执行此操作的基础知识是什么? 非常感谢。
I need to merge two similar xml files, but only records which match on common tags, e.g.<type>
in the following example:
file1.xml is
<node>
<type>a</type>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
file2.xml is
<node>
<type>a</type>
<name>jill</name>
</node>
so that I have an output of
<node>
<type>a</type>
<name>jill</name>
<name>joe</name>
</node>
<node>
<type>b</type>
<name>sam</name>
</node>
What are the basics of doing this, in xsl?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此样式表:
使用此输入(格式正确):
输出:
This stylesheet:
With this input (wellformed):
Output:
我认为值得添加一些我在这样做时学到的额外信息,以防它对任何其他初学者有用。我更改了测试代码名称,以便它们不会与 xsl 中使用的某些术语混淆。我不知道这是否是最好或最有效的做事方式,但它确实有效(有一些警告!)。
我想保留“info”节点,而原始代码丢失了它。编写单独的匹配模板将其保留在输出中。另外,按照我的编码方式,仅当该节点位于输入文件 (x1) 中时才会保留该节点。如果它位于 (x2) 文件中,则不会保留它。这必须与我编写迭代的方式一致。理想情况下,我想将其保留在任一输入文件中,但尚未弄清楚如何做到这一点。另外,我希望可以选择通过 msxsl 将文件名 x2 作为参数传递,而不是对其进行硬编码。肯定有办法做到这一点,但我还没有找到它。
xsl 文件:
因此,使用 msxls 命令: 使用
以下数据给出以下结果:
file x1.xml:
file x2.xml:
out.xml:
I thought it worth adding some extra info I've learned while doing this, in case it's of use to any other beginners. I've changed my test code names so that they aren't potentially confused with some of the terms used in the xsl. I've no idea if it's the best or most efficient way of doing things, but it works (with a few caveats!).
I wanted to keep the "info" node, and the original code lost it. Coding a separate match template keeps it in the output. Also, the way I coded it, this node is only kept if it is in the input file (x1). If it's in the (x2) file, then it doesn't get kept. This has to be with the way I've written the iterations. Ideally, I'd like to keep it from either input file, but haven't worked out how to do that yet. Also, I'd like to have the option of passing the filename x2 as a parameter, via msxsl, rather than have it hard coded. There surely must be a way of doing this, but I haven't managed to track it down yet.
xsl file:
So, using the msxls command:
Gives the following results with the data below:
file x1.xml:
file x2.xml:
out.xml:
一种方法是将第二个 xml 作为参数传递,
第二种更简单的方法是将一个根元素下的两个 xml 连接起来
,然后使用 2 进行合并
One way is to pass second xml as a parameter,
Second easier way is to concatenate both xmls under the one root element to
and then do merge it using 2