<!-- 

addImageRefs.xsl: take WriterImageNames.xml output created by 
getImageNameData.xsl and insert the href values into the right 
places in a DocBook file created by OpenOffice Writer 3.1.

If this had to scale way up it would be worth using xsl:key to 
speed the lookups. See http://www.xml.com/lpt/a/933.

Bob DuCharme 2009-11-05 No warrantee expressed or implied.

 -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

<xsl:variable name="colorLookupDoc" select="document('WriterImageNames.xml')"/>

  <xsl:template match="inlinegraphic">
<!-- DocBook2DITA conversion converts inlinegraphic/@fileref into
     image/@href, so put the filename into inlinegraphic/@fileref -->
    <xsl:variable name="imgFileRef" select="substring-after(@fileref,':')"/> 
	 <xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
      <xsl:attribute name="flag">
        <xsl:value-of select="$imgFileRef"/>
      </xsl:attribute>
      <xsl:attribute name="fileref">
		<xsl:value-of select="$colorLookupDoc//imageLink[@name = $imgFileRef]/@href"/>
      </xsl:attribute>
      
    </xsl:copy>
  </xsl:template>


<!-- While we're at it, lose some empty elements -->
    <xsl:template match="para[normalize-space(.) = '' and not(*)]"/>

    <xsl:template match="sect1[normalize-space(.) = '']"/>


  <xsl:template match="@*|node()">
	 <xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	 </xsl:copy>
  </xsl:template>


</xsl:stylesheet>
