7

When parsing some an XSL XML file using docx4j, I keep receiving this error:

'The element type "img" must be terminated by the matching end-tag "</img>". Exception Error in Docx4JException'

I have tried all sorts of combinations to solve the issue but nothing seems to work apart from putting some text in between the img tags. I don't want the text to display. Is there anything else that can be done?

This is the piece of xsl that is causing the error:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:prettyprint="http://xml.apache.org/xslt" xmlns:xalan="http://xml.apache.org/xalan" version="1.0">
   <xsl:output method="html" />
   <!-- Main entry template -->
   <xsl:template match="Email">
      <html>
         <body>
            <img width="100" height="100" src="http://thumbs.dreamstime.com/x/sun-logo-6350903.jpg" border="0" class="MyImage" />
            <div style="font-family:Verdana, Arial; font-size:9.5pt; font-weight:normal">
               <xsl:variable name="PTPTotalAmt" select="Issue_PTPTotalAmount_C" />
               <xsl:variable name="LetterDate" select="LetterDate" />
               <xsl:variable name="LtrDate" select="substring($LetterDate, 1, 11)" />
               <br>
                  <xsl:text />
               </br>
               <xsl:value-of select="Contact_Title_R" />
               <xsl:text />
               <xsl:value-of select="Contact_LastName_X" />
               <br>
                  <xsl:text />
               </br>
               <xsl:value-of select="Contact_DispAddrLine1_X" />
               <br>
                  <xsl:text />
               </br>
               <xsl:value-of select="Contact_DispAddrLine3_X" />
               <br>
                  <xsl:text />
               </br>
               <xsl:value-of select="Contact_DispAddrLine4_X" />
               <br>
                  <xsl:text />
               </br>
               <xsl:value-of select="Contact_DispAddrLine5_X" />
               <br>
                  <xsl:text />
               </br>
               <xsl:value-of select="Contact_DispAddrPostCode_X" />
               <br>
                  <xsl:text />
               </br>
               <xsl:text />
               <xsl:text />
               <xsl:value-of select="$LtrDate" />
            </div>
            <br>
               <xsl:text />
            </br>
            <br>
               <xsl:text />
            </br>
            <br>
               <xsl:text />
            </br>
            <br>
               <xsl:text />
            </br>
            <div style="font-family:Verdana, Arial; font-size:8.5pt; font-weight:normal">
               <br>
                  <xsl:text>Address Here</xsl:text>
               </br>
            </div>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>
snowstreams
  • 575
  • 2
  • 13
  • 23

3 Answers3

5

Change your xsl:output element to output XML:

<xsl:output method="xml" indent="yes"/>

(The indent="yes" part isn't required but will help with reading the output.)

If the xsl:output method="xml" change alone does not work, try explicitly closing the img element. So, instead of <img/>, use <img></img>:

<img width="100" height="100" src="http://thumbs.dreamstime.com/x/sun-logo-6350903.jpg" border="0" class="MyImage"></img>

Explanation: HTML plays fast and loose wrt end tags. Downstream processing by docx4j wants properly terminated elements, and XSLT will generate properly terminated elements when you specify <xsl:output method="xml"/>.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks but I've tried that and it still results in the same error. The only thing that did work was then if I put some text in between the tags. Waste text But that doesnt seem to make sense that your forced to put text in between the image tags. – snowstreams Nov 06 '13 at 14:52
  • 1
    Ah, just noticed that you're using `method="html"`. Change it to `method="xml"`. Answer updated. – kjhughes Nov 06 '13 at 15:08
  • Thanks for that. I thought I might have had something stupid in a header somewhere. – snowstreams Nov 06 '13 at 15:57
3

If you want to add image and it is regularly asking for closing the tag you just write like this:

<img src="abc"/>
Flexo
  • 87,323
  • 22
  • 191
  • 272
Amit Verma
  • 31
  • 1
1

This works !! simple add the clossing tag to the Img Tag like this

<img src=""/>
Akitha_MJ
  • 3,882
  • 25
  • 20