简体   繁体   中英

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.

 <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

<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.

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:

<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:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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