<xsl:stylesheet extension-element-prefixes="saxon" version="1.0">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="//lecture">
     <html>
       <body>
         <xsl:apply-templates select="title"/>
         <xsl:call-template name="listOfCompounds"/>
         <xsl:apply-templates select="section"/>
       </body>
     </html>
   </xsl:template>

   <xsl:template match="title">
     <h2>
       <xsl:apply-templates/>
     </h2>
   </xsl:template>

   <xsl:template match="//section/title">
     <b>
       <xsl:apply-templates/>
     </b>
     <br/>
   </xsl:template>

   <xsl:template match="section">
     <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="para">
     <p>
       <xsl:apply-templates/>
     </p>
   </xsl:template>

   <xsl:template name="listOfCompounds">
     <xsl:text>List of compounds cited in the text: </xsl:text>
     <br/>
     <xsl:for-each select="//cmp">
       <xsl:value-of select="@myId"/>
       <br/>
     </xsl:for-each>
     <br/>
   </xsl:template>

   <xsl:template match="cmp">
     <xsl:variable name="id" select="@myId"/>
     <xsl:apply-templates select="document('testG_2.xml')//base/cmp[@myId=$id]" mode="printname"/>
   </xsl:template>

   <xsl:template match="cmp" mode="printname">
     <xsl:value-of select="@name"/>
   </xsl:template>
 </xsl:stylesheet>