Search code examples
xmlxsltxpathxmlstarlet

XML - Replace xi:include with actual content from referenced files


Is there any software that can replace the <xi:include href="" /> instructions from an XML file with the actual content from the referenced files?

Or is there any way this can be done with a simple script, maybe in python?

Edit: Thanks E.Wiest. That solved my issue. The solution is indeed to use XMLstarlet and XSLT to transform the XML and replace xi:include with the content of the referenced files.

The XSLT from the link E.Wiest provided worked directly with no adjustments. This is the script I used:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" />

<xsl:template match="/">
<xsl:apply-templates select="." mode="copy-no-namespaces"/>
</xsl:template>

<xsl:template match="*" mode="copy-no-namespaces">
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()" mode="copy-no-namespaces"/>
    </xsl:element>
</xsl:template>

<xsl:template match="comment()| processing-instruction()" mode="copy-no-namespaces">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

The only issue I have with XMLstarlet is that it outputs the transformed XML to CMD prompt screen, I couldn't find any option to specify and output file. There is only an option to replace the source XML file. But I don't want to do this.

As a workaround solution, I wrote a short python script to retrieve the output and save it in a different XML file.


Solution

  • "The only issue I have with XMLstarlet is that it outputs the transformed XML to CMD prompt screen, I couldn't find any option to specify and output file"

    You can redirect output like this

    xmlstarlet sel -m ..... example.xml   > output.xml