2

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

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.