<?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" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:param name="TempCaptureFolder"/>
  <xsl:param name="FinalCaptureFolder"/>

  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="@file[parent::image]">
    <xsl:variable name="SrcImagePath" select="concat($TempCaptureFolder, '/')"/>
    <xsl:variable name="DstImagePath" select="concat($FinalCaptureFolder, '/')"/>
    <xsl:attribute name="file">
      <xsl:choose>
        <xsl:when test="starts-with(., $SrcImagePath)">
          <xsl:value-of select="$DstImagePath"/>
          <xsl:value-of select="substring-after(., $SrcImagePath)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:copy/>
  </xsl:template>
  
  <xsl:template match="text()[parent::violation]">
    <xsl:variable name="SrcImagePath" select="concat($TempCaptureFolder, '\')"/>
    <xsl:variable name="DstImagePath" select="concat($FinalCaptureFolder, '\')"/>
    <xsl:choose>
      <xsl:when test="contains(., $SrcImagePath)">
        <xsl:value-of select="substring-before(., $SrcImagePath)"/>
        <xsl:value-of select="$DstImagePath"/>
        <xsl:value-of select="substring-after(., $SrcImagePath)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="processing-instruction()|comment()">
    <xsl:copy/>
  </xsl:template>
  
</xsl:stylesheet>
