POUWIEL|COM

JeroenPouwiel

XSL Transformations

Transforming xml documents by including external xml files into a main xml document.
The application used is the C xml parser provided in the Oracle instant client: https://docs.oracle.com/



transcode_xls.bat


xml -s C:\xml\_include\template\transcode.xsl -o C:\xml\output\output.xml C:\xml\root_document.xml


root_document.xml


<?xml version="1.0" encoding="ISO8859-1" ?>
<root xmlns:xi="http://www.w3.org/2001/XInclude">
  <section_1></section_1>
  <section_2></section_2>
</root>


section_1.xml

<?xml version="1.0" standalone="no" ?>
<root>
  <data id="gaz"><p>section_1 text</p></data>
  <subsection_1.1>
    <p>Subsection 1.1 text</p> <!-- This will be replaced -->
  </subsection_1.1>
  <subsection_1.2>
    <p>Subsection 1.2 text</p> <!-- This will be replaced -->
  </subsection_1.2>
</root>


section_1-1.xml

<?xml version="1.0" standalone="no" ?>
<root>
  <!-- <subsection_1.1> -->
    <p>Inserted subsection 1.1 text</p>
  <!-- </subsection_1.1> -->
</root>


section_1-2.xml

<?xml version="1.0" standalone="no" ?>
<root>
  <!-- <subsection_1.2> -->
    <p>Inserted subsection 1.2 text</p>
    <data1 id="foo"><p>some text</p></data1>
    <data2 id="baz">2025-01-01 17:56:59</data2>
  <!-- </subsection_1.2> -->
</root>


section_2.xml

<?xml version="1.0" standalone="no" ?>
<root>
  <!-- <section_2> -->
    <address id="bro">
      <name>John Doe</name>
      <phone>082747298420408</phone>
      <street>Somewehere st. 3536</street>
    </address>
    <subsection_2-1 /> <!-- The transformation will translate the single key to <subsection_2-1></subsection_2-1> -->
  <!-- </section_2> -->
</root>


section_2-1.xml

<?xml version="1.0" standalone="no" ?>
<root>
  <!-- <subsection_2.1> -->
    <p>Subsection 2.1 text</p>
    <p>More subsection 2.1 text</p>
    <subsection_2.1.1>
      <!-- This comment will be replaced with the contents of subsection_2-1-1.xml -->
    </subsection_2.1.1>
  <!-- </subsection_2.1> -->
</root>


section_2-1-1.xml

<?xml version="1.0" standalone="no" ?>
<root>
  <!-- <subsection_2.1.1> -->
      <p>Sub-Subsection 2.1.1 text</p>
      <p>Yet another layer</p>
      <p>More subsection 2.1.1 text</p>
    <!-- </subsection_2.1.1>  -->
</root>


transcode.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="section_1">
        <xsl:copy>
            <xsl:apply-templates select="document('C:\xml\_include\sections\section_1\section_1.xml')/*/*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="subsection_1.1">
        <xsl:copy>
            <xsl:apply-templates select="document('C:\xml\_include\sections\section_1\subsection_1-1\subsection_1-1.xml')/*/*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="subsection_1.2">
        <xsl:copy>
            <xsl:apply-templates select="document('C:\xml\_include\sections\section_1\subsection_1-2\subsection_1-2.xml')/*/*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="section_2">
        <xsl:copy>
            <xsl:apply-templates select="document('C:\xml\_include\sections\section_2\section_2.xml')/*/*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="subsection_2.1">
        <xsl:copy>
            <xsl:apply-templates select="document('C:\xml\_include\sections\section_2\subsection_2-1\subsection_2-1.xml')/*/*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="subsection_2.1.1">
        <xsl:copy>
            <xsl:apply-templates select="document('C:\xml\_include\sections\section_2\subsection_2-1\subsection_2-1-1\subsection_2-1-1.xml')/*/*" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>


output.xml

Leave a Reply

You must be logged in to post a comment.

Categories