<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xd"
    version="1.0">
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Oct 17, 2010</xd:p>
            <xd:p><xd:b>Author:</xd:b> db@dbalzer.net</xd:p>
            <xd:p>Add transformation rules incrementally as examples become 
            more complex. This one adds a Variant entity.</xd:p>
        </xd:desc>
    </xd:doc>
    <xsl:template match="/">
        <html>
            <title>Transformed view of test record in EN 15907 XML encoding</title>
            <style type="text/css">
                p { font-family: sans-serif; font-size:11pt; padding:0; 
                    margin-bottom:2pt; margin-top:0 }
                p.labelr { text-align:right }
                code { font-family: sans-serif; font-weight:bold; color:#044800 }
                hcode { font-family: sans-serif; color:#044800 }
                td { vertical-align:top }
                div.topbar { background-color:#D9D9D9; padding:3pt }
                div.work { background-color:#F1F1D9; padding:3pt }
                div.variant { background-color:#D9F1D9; padding:3pt }
                div.manifestation { background-color:#F1E0E0; padding:3pt }
            </style>
        </html>
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="ns1:CinematographicWork" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <div class="topbar">
            <p>Transformed view of test record in EN 15907 XML encoding</p>
        </div>
        <div class="work">
            <table>
                <tr>
                    <td><p class="labelr"><strong>Cinematographic Work</strong></p></td>
                    <td><p><hcode><xsl:value-of select="ns1:Identifier" /></hcode>
                from: 
                <hcode><xsl:value-of select="ns1:RecordSource/ns1:SourceName" /></hcode>
                    </p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Identifying Title:</p></td>
                    <td><p>                    
                <code><xsl:apply-templates select="ns1:IdentifyingTitle"/></code>
                    </p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Country of Reference:</p></td>
                    <td><p><xsl:apply-templates select="ns1:CountryOfReference"/>
                    </p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Year of Reference:</p></td>
                    <td><p>                    
                        <code><xsl:value-of select="ns1:YearOfReference"/></code>
                    </p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Director(s):</p></td>
                    <td><p><xsl:call-template name="directors" /></p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Production Company/ies:</p></td>
                    <td><p><xsl:call-template name="production" /></p></td>
                </tr>
            </table>
            <xsl:apply-templates select="ns1:Variant" />
            <xsl:apply-templates select="ns1:Manifestation" />
        </div>
    </xsl:template>
    
    <xsl:template match="ns1:Variant"  xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <div class="variant">
            <table>
                <tr>
                    <td><p class="labelr"><strong>Variant</strong></p></td>
                    <td><p><hcode><xsl:value-of select="ns1:Identifier" /></hcode>
                    </p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Variant type:</p></td>
                    <td><p><code><xsl:value-of select="@variantType" /></code></p></td>
                </tr>
            </table>                
        <xsl:apply-templates select="ns1:Manifestation" />
        </div>
    </xsl:template>
    
    <xsl:template match="ns1:Manifestation" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <div class="manifestation">
            <table>
                <tr>
                    <td><p class="labelr"><strong>Manifestation</strong></p></td>
                    <td><p><hcode><xsl:value-of select="ns1:Identifier" /></hcode>
                    </p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Manifestation type:</p></td>
                    <td><p><code><xsl:value-of select="@manifestationType" /></code></p></td>
                </tr>
                <tr>
                    <td><p class="labelr">Language:</p></td>
                    <td><p><code><xsl:value-of select="ns1:Language" /></code></p></td>
                </tr>
                <xsl:call-template name="extent"/>
                <xsl:apply-templates select="ns1:Format"/>
            </table>                
        </div>            
    </xsl:template>
    
    <xsl:template match="ns1:Format" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <tr>
            <td><p class="labelr">Format:</p></td>
            <td><p>
                <xsl:if test="ns1:Gauge">
                    <code><xsl:value-of select="ns1:Gauge"/></code>
                </xsl:if>
                <xsl:if test="ns1:SoundSystem/ns1:HasSound='true'">
                    <xsl:if test="ns1:Gauge">
                         <xsl:text>, </xsl:text>
                     </xsl:if>
                     <code>Sound</code>
                </xsl:if>
                <xsl:if test="ns1:SoundSystem/ns1:HasSound='false'">
                    <xsl:if test="ns1:Gauge">
                        <xsl:text>, </xsl:text>
                    </xsl:if>
                    <code>Silent</code>
                </xsl:if>
                <xsl:if test="ns1:Colour">
                    <xsl:text>, </xsl:text>
                    <code><xsl:value-of select="ns1:Colour/ns1:Chromatism"/></code>
                </xsl:if>
            </p></td>            
        </tr>
    </xsl:template>
    
    <xsl:template name="extent" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <xsl:if test="ns1:Extent[@unit='m']">
            <tr>
                <td><p class="labelr">Length:</p></td>
                <td><p><code><xsl:value-of select="ns1:Extent"/>
                    <xsl:text> </xsl:text>
                    <xsl:value-of select="ns1:Extent/attribute::unit"/>
                </code></p>
                </td>
            </tr>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="directors" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <xsl:for-each select="ns1:AgentRelationship[ns1:Activity='Director']">
            <xsl:if test="position()>1"> ; </xsl:if>
            <code><xsl:value-of select="ns1:AgentName" /></code>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="production" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <xsl:for-each select="ns1:AgentRelationship[ns1:Activity='Production Company']">
            <xsl:if test="position()>1"> ; </xsl:if>
            <code><xsl:value-of select="ns1:AgentName" /></code>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="ns1:IdentifyingTitle" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <xsl:if test="substring-before(.,'_') ">
            <i><xsl:value-of select="substring-before(.,'_')" /></i>
            <xsl:value-of select="substring-after(.,'_')" />
        </xsl:if>
        <xsl:if test="not(substring-before(.,'_')) ">
            <xsl:value-of select="." />
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="ns1:CountryOfReference" xmlns:ns1="http://filmstandards.org/schemas/EN15907-d1">
        <xsl:for-each select="ns1:Country">
            <xsl:if test="position()>1"> / </xsl:if>
            <code><xsl:value-of select="ns1:RegionCode" /></code>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>