<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    
    hResume2Odt.xsl
    
    By Brook Ellingwood (brook.ellingwood@gmail.com).
    
    This (totally arbitrary) version number: 1.0.
    
    Published: 2009-05-02
    
    This XSL transform takes the XHTML found at http://beaconstandard.com/brookellingwood/resume.php and replicates
    its styling as closely as possible in an Open Document Text's content.xml format.
    
    This is NOT a generic transformation for any hResume implementation, although such a thing is certainly possible.
    
-->

<!-- 
    Note that the default namespace and the xhtml: namespace both need to have the same uri to avoid issues.
-->
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dom="http://www.w3.org/2001/xml-events"
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
    xmlns:math="http://www.w3.org/1998/Math/MathML"
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:ooo="http://openoffice.org/2004/office" xmlns:oooc="http://openoffice.org/2004/calc"
    xmlns:ooow="http://openoffice.org/2004/writer"
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
    xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"></xsl:output>
    <!-- 
        Start at the document root, put in the boilerplate XML, then only apply templates only within the declared 'resume' div. Maybe this could
        be the 'hresume' div, but I put 'hresume' on the body element instead. Hmmm. 
    -->
    <xsl:template match="/">
        <office:document-content office:version="1.0"
            xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:dom="http://www.w3.org/2001/xml-events"
            xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
            xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
            xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
            xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
            xmlns:math="http://www.w3.org/1998/Math/MathML"
            xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
            xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
            xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
            xmlns:ooo="http://openoffice.org/2004/office"
            xmlns:oooc="http://openoffice.org/2004/calc"
            xmlns:ooow="http://openoffice.org/2004/writer"
            xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
            xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
            xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
            xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
            xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
            xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xlink="http://www.w3.org/1999/xlink"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <office:scripts></office:scripts>
            <office:font-face-decls>
                <style:font-face style:name="Tahoma1" svg:font-family="Tahoma"></style:font-face>
                <style:font-face style:font-family-generic="roman" style:font-pitch="variable"
                    style:name="Times New Roman"
                    svg:font-family="&apos;Times New Roman&apos;"></style:font-face>
                <style:font-face style:font-adornments="Bold" style:font-family-generic="roman"
                    style:font-pitch="variable" style:name="Times New Roman2"
                    svg:font-family="&apos;Times New Roman&apos;"></style:font-face>
                <style:font-face style:font-adornments="Bold Italic"
                    style:font-family-generic="roman" style:font-pitch="variable"
                    style:name="Times New Roman3"
                    svg:font-family="&apos;Times New Roman&apos;"></style:font-face>
                <style:font-face style:font-adornments="Regular" style:font-family-generic="roman"
                    style:font-pitch="variable" style:name="Times New Roman1"
                    svg:font-family="&apos;Times New Roman&apos;"></style:font-face>
                <style:font-face style:font-family-generic="swiss" style:font-pitch="variable"
                    style:name="Arial" svg:font-family="Arial"></style:font-face>
                <style:font-face style:font-adornments="Bold" style:font-family-generic="swiss"
                    style:font-pitch="variable" style:name="Arial1" svg:font-family="Arial"></style:font-face>
                <style:font-face style:font-adornments="Bold" style:font-family-generic="swiss"
                    style:font-pitch="variable" style:name="Geneva1" svg:font-family="Geneva"></style:font-face>
                <style:font-face style:font-adornments="Bold Italic"
                    style:font-family-generic="swiss" style:font-pitch="variable"
                    style:name="Geneva2" svg:font-family="Geneva"></style:font-face>
                <style:font-face style:font-adornments="Regular" style:font-family-generic="swiss"
                    style:font-pitch="variable" style:name="Geneva" svg:font-family="Geneva"></style:font-face>
                <style:font-face style:font-family-generic="swiss" style:font-pitch="variable"
                    style:name="Hoefler Text1" svg:font-family="&apos;Hoefler Text&apos;"></style:font-face>
                <style:font-face style:font-adornments="Bold" style:font-family-generic="swiss"
                    style:font-pitch="variable" style:name="Hoefler Text"
                    svg:font-family="&apos;Hoefler Text&apos;"></style:font-face>
                <style:font-face style:font-adornments="Regular" style:font-family-generic="swiss"
                    style:font-pitch="variable" style:name="Hoefler Text2"
                    svg:font-family="&apos;Hoefler Text&apos;"></style:font-face>
                <style:font-face style:font-adornments="Bold" style:font-family-generic="swiss"
                    style:font-pitch="variable" style:name="Verdana1" svg:font-family="Verdana"></style:font-face>
                <style:font-face style:font-adornments="Bold Italic"
                    style:font-family-generic="swiss" style:font-pitch="variable"
                    style:name="Verdana" svg:font-family="Verdana"></style:font-face>
                <style:font-face style:font-adornments="Regular" style:font-family-generic="swiss"
                    style:font-pitch="variable" style:name="Verdana2" svg:font-family="Verdana"></style:font-face>
                <style:font-face style:font-family-generic="system" style:font-pitch="variable"
                    style:name="Arial2" svg:font-family="Arial"></style:font-face>
                <style:font-face style:font-family-generic="system" style:font-pitch="variable"
                    style:name="MS Mincho" svg:font-family="&apos;MS Mincho&apos;"></style:font-face>
                <style:font-face style:font-family-generic="system" style:font-pitch="variable"
                    style:name="Tahoma" svg:font-family="Tahoma"></style:font-face>
            </office:font-face-decls>
            <office:automatic-styles>
                <style:style style:family="paragraph" style:name="P1"
                    style:parent-style-name="Resume_20_contact">
                    <style:text-properties style:font-name="Hoefler Text1"></style:text-properties>
                </style:style>
                <style:style style:family="paragraph" style:name="P2"
                    style:parent-style-name="Resume_20_list_20_alt_20_line">
                    <style:paragraph-properties fo:background-color="transparent">
                        <style:background-image></style:background-image>
                    </style:paragraph-properties>
                </style:style>
                <style:style style:family="text" style:name="T1">
                    <style:text-properties style:font-name="Hoefler Text1"></style:text-properties>
                </style:style>
                <style:style style:family="text" style:name="T2">
                    <style:text-properties fo:font-size="10pt" fo:font-weight="normal"
                        style:font-size-asian="10pt" style:font-size-complex="10pt"
                        style:font-weight-asian="normal" style:font-weight-complex="normal"
                    ></style:text-properties>
                </style:style>
                <style:style style:family="text" style:name="T3">
                    <style:text-properties fo:color="#000000"></style:text-properties>
                </style:style>
                <style:style style:family="text" style:name="T4">
                    <style:text-properties fo:font-style="italic" style:font-style-asian="italic"
                        style:font-style-complex="italic"></style:text-properties>
                </style:style>
            </office:automatic-styles>
            <office:body>
                <office:text>
                    <office:forms form:apply-design-mode="false" form:automatic-focus="false"></office:forms>
                    <text:sequence-decls>
                        <text:sequence-decl text:display-outline-level="0" text:name="Illustration"></text:sequence-decl>
                        <text:sequence-decl text:display-outline-level="0" text:name="Table"></text:sequence-decl>
                        <text:sequence-decl text:display-outline-level="0" text:name="Text"></text:sequence-decl>
                        <text:sequence-decl text:display-outline-level="0" text:name="Drawing"
                        ></text:sequence-decl>
                    </text:sequence-decls>
                    <xsl:apply-templates select="xhtml:html/xhtml:body/xhtml:div[@id='resume']"
                    ></xsl:apply-templates>
                </office:text>
            </office:body>
        </office:document-content>
    </xsl:template>
    <xsl:template match="xhtml:body/xhtml:div[@id='resume']">
        <xsl:apply-templates select="xhtml:div[contains(@class,'section')]"></xsl:apply-templates>
    </xsl:template>
    <!-- 
        If we're in a cover letter, there's a recipient address to format.
    -->
    <xsl:template match="xhtml:div[@class='vcard coverRecipient']">
        <text:p text:style-name="Text_20_body"> </text:p>
        <text:p text:style-name="Text_20_body">
            <xsl:value-of select="normalize-space(xhtml:span[contains(@class, 'fn')])"
            ></xsl:value-of>
        </text:p>
        <text:p text:style-name="Text_20_body">
            <xsl:value-of
                select="normalize-space(xhtml:div[@class='adr']/xhtml:div[@class='street-address'])"
            ></xsl:value-of>
        </text:p>
        <text:p text:style-name="Text_20_body">
            <xsl:value-of
                select="normalize-space(xhtml:div[@class='adr']/xhtml:div[@id='cityStateZip'])"></xsl:value-of>
            <xsl:text><![CDATA[                     ]]></xsl:text>
            <xsl:value-of select="normalize-space(xhtml:div[@class='adr']/xhtml:div[@class='date'])"
            ></xsl:value-of>
        </text:p>
        <text:p text:style-name="Text_20_body"> </text:p>
    </xsl:template>
    <!-- 
        h2
    -->
    <xsl:template match="xhtml:h2">
        <xsl:param name="prevId"></xsl:param>
            <xsl:choose>
                <xsl:when test="$prevId='cover'">
                    <text:h text:outline-level="2" text:style-name="Heading_20_2">
                        </text:h>
                    <text:h text:outline-level="2" text:style-name="P2">
                        <xsl:call-template name="normalizeSpace"></xsl:call-template>
                    </text:h>
                </xsl:when>
                <xsl:otherwise>
                    <text:h text:outline-level="2" text:style-name="Heading_20_2">
                        
                    <xsl:call-template name="normalizeSpace"></xsl:call-template>
                        </text:h>
                </xsl:otherwise>
            </xsl:choose>
    </xsl:template>
    <!-- 
        h3. I have to account for internal spans, which are there both for the hResume microformat and
        for formatting loveliness.
    -->
    <xsl:template match="xhtml:h3">
        <text:p text:style-name="Heading_20_3_20_location_20_and_20_dates">
            <xsl:value-of
                select="normalize-space(xhtml:span[contains(@class, 'org')] | xhtml:span[@class='activity'])"></xsl:value-of>
            <text:span text:style-name="T2">
                <xsl:for-each select=".//xhtml:span[@class='adr']">
                    <xsl:text>&#x9;</xsl:text>
                    <xsl:call-template name="normalizeSpace"></xsl:call-template>
                </xsl:for-each>
                <xsl:for-each select=".//xhtml:span[@class='empDates']">
                    <xsl:text>&#x9;</xsl:text>
                    <xsl:apply-templates></xsl:apply-templates>
                </xsl:for-each>
            </text:span>
        </text:p>
    </xsl:template>
    <xsl:template match="xhtml:span[@class='empDates']/text()">
        <xsl:call-template name="normalizeSpace"></xsl:call-template>
    </xsl:template>
    <xsl:template match="xhtml:abbr[not(contains(@class,'invisible'))]">
        <xsl:call-template name="normalizeSpace"></xsl:call-template>
    </xsl:template>
    <!-- 
        h4
    -->
    <xsl:template match="xhtml:h4">
        <text:h text:outline-level="4" text:style-name="Heading_20_4">
            <xsl:call-template name="normalizeSpace"></xsl:call-template>
        </text:h>
    </xsl:template>
    <!--
        Each div.section (Experience, Education, etc.) becomes unnecessary in ODT, so we just pass through
    -->
    <xsl:template match="xhtml:div[contains(@class, 'section')]">
        <xsl:apply-templates>
            <xsl:with-param name="prevId" select="(preceding::node())/@id"></xsl:with-param>
        </xsl:apply-templates>
    </xsl:template>
    <!-- 
        li's indicating subsections (jobs, colleges, etc.) become fo:blocks.
    -->
    <xsl:template match="xhtml:ol/xhtml:li[contains(@class,'sub')]">
        <!-- 
                    I generate a dot indicating each li's position in the list. In the XHTML right now, the dots are
                    static, which makes me sad. I could do them with Javascript, but I'll probably leave them as they are
                    until I redo the whole thing with a server-side transform.
        -->
        <text:p text:style-name="Position_20_dots">
            <xsl:text>· </xsl:text>
            <xsl:for-each select="preceding-sibling::xhtml:li[contains(@class,'sub')]">
                <xsl:text>· </xsl:text>
            </xsl:for-each>
        </text:p>
        <xsl:apply-templates></xsl:apply-templates>
    </xsl:template>
    <!-- 
        I suppress the static dots in the XHTML, because the generated ones are much cooler.
    -->
    <xsl:template match="xhtml:span[@class='posCount']"></xsl:template>
    <!-- 
        p into p
    -->
    <xsl:template match="xhtml:p">
        <text:p text:style-name="Text_20_body">
            <xsl:call-template name="normalizeSpace"></xsl:call-template>
        </text:p>
    </xsl:template>
    <xsl:template match="xhtml:div[@class='description']/xhtml:p">
        <text:p text:style-name="Text_20_body_20_indent">
            <xsl:call-template name="normalizeSpace"></xsl:call-template>
        </text:p>
    </xsl:template>
    <!-- 
        div.description
    -->
    <xsl:template match="xhtml:div[@class='description']">
        <xsl:apply-templates></xsl:apply-templates>
    </xsl:template>
    <!-- 
        I zebra stripe li's inside div.description by applying different background colors based on their position
        in the list. In the XHTML, not only is this currently hardcoded, it's actually being done with inline styling
        on the li's. Super lame. I should at least make that come from the external CSS.
    -->
    <xsl:template match="xhtml:div[@class='description']/xhtml:ul/xhtml:li[position() mod 2 =1]">
        <text:p text:style-name="Resume_20_list_20_alt_20_line">
            <text:s>
                <xsl:text> </xsl:text>
            </text:s>
            <xsl:call-template name="normalizeSpace"></xsl:call-template>
        </text:p>
    </xsl:template>
    <xsl:template match="xhtml:div[@class='description']/xhtml:ul/xhtml:li[position() mod 2 =0]">
        <text:p text:style-name="Resume_20_list">
            <text:s>
                <xsl:text> </xsl:text>
            </text:s>
            <xsl:call-template name="normalizeSpace"></xsl:call-template>
        </text:p>
    </xsl:template>
    <!-- 
        Preserve inline italics for publication titles. !! not working because of how I'm using the named template "normalizeSpace" !!
    -->
    <xsl:template match="xhtml:a[@style='font-style:italic']">
        <text:span text:style-name="T4">
            <xsl:apply-templates></xsl:apply-templates>
        </text:span>
    </xsl:template>
    <xsl:template name="normalizeSpace">
        <xsl:value-of select="normalize-space(translate(., '&#x0A;', ' '))"></xsl:value-of>
    </xsl:template>
    <xsl:template match="text()"></xsl:template>
</xsl:stylesheet>
