<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
  xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
  exclude-result-prefixes="w wx">

  <xsl:output indent="yes"/>

  <!-- Convert the first wx:sub-section element to a root <article> element -->
  <!-- ASSUMPTION: Document uses one and only one "Heading 1" paragraph
       (top-level section in outline view) -->
  <xsl:template match="wx:sub-section[parent::wx:sect]">
    <xsl:processing-instruction name="mso-application">
      <xsl:text>progid="Word.Document"</xsl:text>
    </xsl:processing-instruction>
    <xsl:processing-instruction name="xml-stylesheet">
      <xsl:text>type="text/xsl" href="article2wordml.xsl"</xsl:text>
    </xsl:processing-instruction>
    <article>
      <xsl:apply-templates/>
    </article>
  </xsl:template>

  <!-- Convert <wx:sub-section> elements to <section> elements -->
  <xsl:template match="wx:sub-section">
    <section>
      <xsl:apply-templates/>
    </section>
  </xsl:template>

  <!-- Convert <w:p> paragraphs to <para> paragraphs -->
  <xsl:template match="w:p">
    <para>
      <xsl:apply-templates mode="para-content"/>
    </para>
  </xsl:template>

  <!-- ...except for the first paragraph in a sub-section (Heading 1,2,3,...);
       the heading will be the <title> of the section -->
  <xsl:template match="wx:sub-section/w:p[1]">
    <title>
      <xsl:apply-templates mode="para-content"/>
    </title>
  </xsl:template>

  <!-- Don't copy text nodes except when explicitly requested (see below) -->
  <xsl:template match="text()"/>
  <xsl:template match="text()" mode="para-content"/>

  <!-- default behavior for text runs; copy the text through -->
  <xsl:template match="w:r" mode="para-content">
    <xsl:copy-of select="w:t/text()"/>
  </xsl:template>

  <!-- turn a run with the "Emphasis" character style into <emphasis> -->
  <xsl:template match="w:r[w:rPr/w:rStyle/@w:val='Emphasis']"
                mode="para-content">
    <emphasis>
      <xsl:copy-of select="w:t/text()"/>
    </emphasis>
  </xsl:template>

  <!-- turn a run with the "Strong" character style into <strong> -->
  <xsl:template match="w:r[w:rPr/w:rStyle/@w:val='Strong']"
                mode="para-content">
    <strong>
      <xsl:copy-of select="w:t/text()"/>
    </strong>
  </xsl:template>

  <!-- Turn a w:hlink element into an HTML-style hyperlink -->
  <xsl:template match="w:hlink" mode="para-content">
    <a href="{@w:dest}">
      <xsl:apply-templates mode="para-content"/>
    </a>
  </xsl:template>

</xsl:stylesheet>
