简体   繁体   English

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

[英]copying content of one xml file into another xml file using xslt

I have a xml file where attributes of a tag is a src for other xml file. 我有一个xml文件,其中标记的属性是其他xml文件的src。

 <a>
    <b>
       <c src="other1.xml" name="other1"></c>
       <c src="other2.xml" name="other2"></c>
       <c src="other3.xml" name="other3"></c> 
   </b>
 </a>

I want to change content of this xml file into following following format 我想将此xml文件的内容更改为以下格式

<a>
    <b>
       <other1> content of other1.xml </other1>
       <other2> content of other2.xml </other2>
       <other3> content of other3.xml </other3>
   </b>
</a>

I tried using xsl:variable and storing value of src inside it but i am getting error. 我尝试使用xsl:variable并在其中存储src的值,但出现错误。

Somebody please suggest solution....even hints will be appreciated 有人请提出解决方案....甚至提示将不胜感激

This should do it: 应该这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="c">
    <xsl:element name="{@name}">
      <xsl:apply-templates select="document(@src)" />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

With the following files as other1.xml, other2.xml and other3.xml: 使用以下文件作为other1.xml,other2.xml和other3.xml:

<I xmlns="hello">
  <am some="" xml="" />
</I>


<I xmlns="hello">
  <amAlso xml="" />
</I>


<I>
  <am xml="as well" />
</I>

And run with your sample XML as input, the result is: 并将示例XML作为输入运行,结果是:

<a>
  <b>
    <other1>
      <I xmlns="hello">
        <am some="" xml="" />
      </I>
    </other1>
    <other2>
      <I xmlns="hello">
        <amAlso xml="" />
      </I>
    </other2>
    <other3>
      <I>
        <am xml="as well" />
      </I>
    </other3>
  </b>
</a>

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

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