Page 1 of 1

remove include

Posted: Thu Nov 09, 2006 1:15 pm
by Kimme
Hi,

I have a xml file (my main file) with includes. I want to remove those include lines and put the code of those include files into my main file. How can I do that with xslt?

Example:

Main file:
<definitions>
<types>
<schema>
<include file="includefile.xml"/>
</schema>
<z>
<x>XXX</x>
</z>
<y>
<x>XXX</x>
</y>
</types>
</definitions>

Include file:
<definitions>
<types>
<schema>
<a>AAA</a>
<b>BBB</b>
<c>CCC</c>
</schema>
</types>
</definitions>

Result:
<definitions>
<types>
<schema>
<a>AAA</a>
<b>BBB</b>
<c>CCC</c>
</schema>
<z>
<x>XXX</x>
</z>
<y>
<x>XXX</x>
</y>
</types>
</definitions>

Posted: Thu Nov 09, 2006 10:12 pm
by jkmyoung
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="include">
<xsl:copy-of select="document(@file)//*[name()=name(current()/..)]/*"/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

The default template (match="*") just copies everything and recurses on it's children.
We create a template for include. When we reach this template, we use the document function to access the linked document.
The //*[name()=name(current()/..)]/* is to change the scope of the elements we copy from that document.