<?xml version="1.0" encoding="UTF-8"?>
<!-- pipeline_xml2csv.xsl, version 0.9, 2010-09-15 -->
<!-- pipeline_xml2csv.xsl is the master stylesheet including 7 step stylesheets in a pipeline. -->
<!--Published with tutorial at www.xmlplease.com/pipeline_xml2csv -->
<!-- Made by Jesper Tverskov, Adelaide, Australia. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <!--
STEP-1: s1_user-defined.xsl
Add user-defined templates of exceptions to delete, rename or add attributes and elements.

STEP-2: s2_testing.xsl
Tests and warns the user, if input XML contains mixed content, or if "_" and "." are used in attribute or element names (not necessarily a problem).

STEP-3: s3_flatten.xsl
Automatically flattens XML, attributes and elements, to three levels. "_" and "." are used as delimiters in flattened element names.

STEP-4: s4_equalizing.xsl
Automatically adds missing optional elements: <element/>, if they are missing from a sub-set of item elements.

STEP-5: s5_user-defined.xsl
Add user-defined templates of exceptions to fine-tune the work of step-3 if necessary (typically to get better element names).

STEP-6: s6_testing.xsl
Tests if item elements have the same children in the same order: That is if input or modified input can be successfully converted to CSV. 

STEP-7: s7_xml2csv.xsl
The final transformation of input or modified input xml file to CSV.
-->

    <!-- 
    Param values:
    0 = skip.
    1 = use included step stylesheet.
    2 = and serialize step to file. -->

    <xsl:param name="param_s1_user-defined" select="2"/>
    <xsl:param name="param_s2_testing" select="2"/>
    <xsl:param name="param_s3_flatten" select="2"/>
    <xsl:param name="param_s4_equalizing" select="2"/>
    <xsl:param name="param_s5_user-defined" select="2"/>
    <xsl:param name="param_s6_testing" select="2"/>
    <xsl:param name="param_s7_xml2csv" select="2"/>

    <xsl:include href="s1_user-defined.xsl"/>
    <xsl:include href="s2_testing.xsl"/>
    <xsl:include href="s3_flatten.xsl"/>
    <xsl:include href="s4_equalizing.xsl"/>
    <xsl:include href="s5_user-defined.xsl"/>
    <xsl:include href="s6_testing.xsl"/>
    <xsl:include href="s7_xml2csv.xsl"/>

    <xsl:template match="/">

        <!-- STEP-1: s1_user-defined.xsl -->
        <xsl:variable name="s1_user-defined">
            <xsl:choose>
                <xsl:when test="$param_s1_user-defined = 1 or $param_s1_user-defined = 2">
                    <xsl:apply-templates mode="s1_user-defined"/>
                </xsl:when>
                <xsl:when test="$param_s1_user-defined = 0">
                    <xsl:copy-of select="."/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">param_s1_user-defined must have value
                        0|1|2</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$param_s1_user-defined = 2">
            <xsl:result-document href="temp_s1_user-defined.xml" method="xml" indent="yes">
                <xsl:copy-of select="$s1_user-defined"/>
            </xsl:result-document>
        </xsl:if>

        <!-- STEP-2: s2_testing.xsl -->
        <xsl:variable name="s2_testing">
            <xsl:choose>
                <xsl:when test="$param_s2_testing = 1 or $param_s2_testing = 2">
                    <xsl:apply-templates mode="s2_testing" select="$s1_user-defined"/>
                </xsl:when>
                <xsl:when test="$param_s2_testing = 0">
                    <xsl:copy-of select="$s1_user-defined"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">param_s2_testing must have value
                        0|1|2</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$param_s2_testing = 2">
            <xsl:result-document href="temp_s2_testing.xml" method="xml" indent="yes">
                <xsl:copy-of select="$s2_testing"/>
            </xsl:result-document>
        </xsl:if>

        <!-- STEP-3: s3_flatten.xsl -->
        <xsl:variable name="s3_flatten">
            <xsl:choose>
                <xsl:when test="$param_s3_flatten = 1 or $param_s3_flatten = 2">
                    <xsl:apply-templates mode="s3_flatten" select="$s2_testing"/>
                </xsl:when>
                <xsl:when test="$param_s3_flatten = 0">
                    <xsl:copy-of select="$s2_testing"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">param_s3_flatten must have value
                        0|1|2</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$param_s3_flatten = 2">
            <xsl:result-document href="temp_s3_flatten.xml" method="xml" indent="yes">
                <xsl:copy-of select="$s3_flatten"/>
            </xsl:result-document>
        </xsl:if>

        <!-- STEP-4: s4_equalizing.xsl -->
        <xsl:variable name="s4_equalizing">
            <xsl:choose>
                <xsl:when test="$param_s4_equalizing = 1 or $param_s4_equalizing = 2">
                    <xsl:apply-templates mode="s4_equalizing" select="$s3_flatten"/>
                </xsl:when>
                <xsl:when test="$param_s4_equalizing = 0">
                    <xsl:copy-of select="$s3_flatten"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">param_s4_equalizing must have value
                        0|1|2</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$param_s4_equalizing = 2">
            <xsl:result-document href="temp_s4_equalizing.xml" method="xml" indent="yes">
                <xsl:copy-of select="$s4_equalizing"/>
            </xsl:result-document>
        </xsl:if>

        <!-- STEP-5: s5_user-defined.xsl -->
        <xsl:variable name="s5_user-defined">
            <xsl:choose>
                <xsl:when test="$param_s5_user-defined = 1 or $param_s5_user-defined = 2">
                    <xsl:apply-templates mode="s5_user-defined" select="$s4_equalizing"/>
                </xsl:when>
                <xsl:when test="s5_user-defined = 0">
                    <xsl:copy-of select="$s4_equalizing"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">param_s5_user-defined must have value
                        0|1|2</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$param_s5_user-defined = 2">
            <xsl:result-document href="temp_s5_user-defined.xml" method="xml" indent="yes">
                <xsl:copy-of select="$s5_user-defined"/>
            </xsl:result-document>
        </xsl:if>

        <!-- STEP-6: s6_testing.xsl -->
        <xsl:variable name="s6_testing">
            <xsl:choose>
                <xsl:when test="$param_s6_testing = 1 or $param_s6_testing = 2">
                    <xsl:apply-templates mode="s6_testing" select="$s5_user-defined"/>
                </xsl:when>
                <xsl:when test="$param_s6_testing = 0">
                    <xsl:copy-of select="$s5_user-defined"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">param_s6_testing must have value
                        0|1|2</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$param_s6_testing = 2">
            <xsl:result-document href="temp_s6_testing.xml" method="xml" indent="yes">
                <xsl:copy-of select="$s6_testing"/>
            </xsl:result-document>
        </xsl:if>

        <!-- STEP-7: s7_xml2csv -->
        <xsl:variable name="s7_xml2csv">
            <xsl:choose>
                <xsl:when test="$param_s7_xml2csv = 1 or $param_s7_xml2csv = 2">
                    <xsl:apply-templates mode="s7_xml2csv" select="$s6_testing"/>
                </xsl:when>
                <xsl:when test="$param_s7_xml2csv = 1">
                    <xsl:copy-of select="$s6_testing"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:message terminate="yes">param_s7_xml2csv must have value
                        0|1|2</xsl:message>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="$param_s7_xml2csv = 2">
            <xsl:result-document href="temp_s7_xml2csv.txt" method="text">
                <xsl:copy-of select="$s7_xml2csv"/>
            </xsl:result-document>
        </xsl:if>
        <xsl:copy-of select="$s7_xml2csv"/>
    </xsl:template>
</xsl:stylesheet>
