remove include

Here should go questions about transforming XML with XSLT and FOP.
Kimme
Posts: 1
Joined: Thu Nov 09, 2006 12:55 pm

remove include

Post 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>
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

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