Search code examples
xmlxsltstylesheet

Copy XML file content and write XSLT processing log output in separate file


I have a XSL transformation where I correct the spelling of an attribute. This works fine. But when I skip the correction (because a corrected attribute already exists) then I want to log that in a separate file.

These 2 tasks work separately but when I try to bring them together then only the log file is written but I want both outputs - the resulting XML and the log file. How to fix it?

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/> 
<xsl:variable name="processingFileName" select="base-uri()" />


<!------- This corrects the spelling in the XML ---------->
<xsl:template match="node()|@*" >       
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"  />
    </xsl:copy>
</xsl:template>

<xsl:template match="Attribute[@Name = 'Clor']" >
    <xsl:if test="not(../Attribute[@Name = 'Color'])">
        <Attribute Name="Color" Value="{@Value}" />
    </xsl:if>       
</xsl:template>


<!------- This logs skipped parts in a log file ---------->
<xsl:template match="/" >
    <xsl:result-document method="text" href="{$processingFileName}.log">
        <xsl:apply-templates mode="logging" select="/export/parts/part/Attribute" />
    </xsl:result-document>      
</xsl:template>

<xsl:template match="Attribute[@Name = 'Clor']" mode="logging">         
    <xsl:if test="(../Attribute[@Name = 'Color'])">
        <xsl:value-of select="../@PartNo" />
        <xsl:text>&#13;&#10;</xsl:text>
    </xsl:if>       
</xsl:template>     

Solution

  • Make sure the default mode is used as well for your document descendants by changing

    <xsl:template match="/" >
        <xsl:result-document method="text" href="{$processingFileName}.log">
            <xsl:apply-templates mode="logging" select="/export/parts/part/Attribute" />
        </xsl:result-document>      
    </xsl:template>
    

    to

    <xsl:template match="/" >
        <xsl:apply-templates/>
        <xsl:result-document method="text" href="{$processingFileName}.log">
            <xsl:apply-templates mode="logging" select="/export/parts/part/Attribute" />
        </xsl:result-document>      
    </xsl:template>