<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : textFromPages.xsl
    Created on : November 1, 2009, 9:51 AM
    Author     : Davin Granroth
    Description:
        Apple's Pages application stores the contents of documents in the
        package for each Pages file. If you unzip the index.xml.gz file,
        you get the raw XML that contains the Pages document source.
        This XSL file can be applied to that index.xml document to extract the
        text from it as HTML. It is then more easily copy and pasted as needed.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:sfa="http://developer.apple.com/namespaces/sfa"
    xmlns:sf="http://developer.apple.com/namespaces/sf"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sl="http://developer.apple.com/namespaces/sl">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title><xsl:call-template name="documentTitle"/></title>
            </head>
            <body>
                <xsl:call-template name="getText"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="documentTitle">
        Text from
        <!-- This doc title value may be empty if the metadata wasn't set in Pages. -->
        <xsl:value-of select="/sl:document/sf:metadata/sf:title/sf:string/@sfa:string"/>
    </xsl:template>

    <xsl:template name="getText">
        <!--
            Using a precise XPath to avoid also grabbing ghost text
            from /section-prototypes.
        -->
        <xsl:for-each select="/sl:document/sf:text-storage/sf:text-body/sf:section/sf:layout/sf:p">
            <p><xsl:value-of select="."/></p>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

