简体   繁体   English

使用xslt将一个xml的内容复制到另一个

[英]Copying content of one xml into other using xslt

I am trying to copy a content from one xml to other xml using Xslt. 我正在尝试使用Xslt将内容从一个xml复制到另一个xml。

I need to copy content of file1 我需要复制file1的内容

<?xml version="1.0"?>
<products author="Jesper">
  <product>
    <name>Delta</name>
    <price>800</price>
    <stock>
        <price>13a</price>
    </stock>
    <place>Denmark</place>
  </product>
</products>

to file 2. File2 has similar tags but order is jumbled, 到文件2。File2具有类似的标记,但是顺序混乱,

<?xml version="1.0"?>
<products author="Jesper">
  <product>
    <stock>
        <price>13d</price>
    </stock>
    <price>700</price>
    <place>Copenhagen</place>
     <name>Beta</name>
  </product>
</products>

expected output 预期产量

<products author="Jesper">
  <product>
    <stock>
        <price>13a</price>
    </stock>
    <price>800</price>
    <place>Denmark</place>
     <name>Delta</name>
  </product>
</products>

so basically I need to Iterate through file1 using for-each and then find the matching tag in file2 and copy the tag value. 所以基本上我需要使用for-each遍历file1,然后在file2中找到匹配的标签并复制标签值。 Not sure about an efficient way to do so ... Double iterating is inefficient. 不知道这样做的有效方法。双重迭代效率很低。 Any suggestion will be helpful. 任何建议都会有所帮助。

This is a very broad question, but I'll try to give you some pointers that should get you started. 这是一个非常广泛的问题,但是我将尝试为您提供一些入门指南。 You will probably want to use the doc() function to load the files since XSLT only allows you to iterate over a single "main" file. 您可能要使用doc()函数加载文件,因为XSLT仅允许您迭代单个“主”文件。 doc() loads a new file into a variable that you can apply templates to and so on. doc()将新文件加载到一个变量中,您可以将其应用于模板等等。 If you are concerned about the iteration performance, you should learn about xsl:key and the key() function, which build indexes that will help with that. 如果您担心迭代性能,则应了解xsl:keykey()函数,它们将建立有助于此工作的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM