XSLT - copy xml node element with html tags

I am trying to use XSLT copy-of tag to extract the contents of a node.  The contents of the XML node has some html tags and inner XML node.  The problem is there is an inner XML tag for the image <IPS_OBECT> and using "copy-of" only outputs the content of the node. I would like to transform the IPS_OBJECT node element  to html img tag. Any help would be appreciated.


MY XSLT:
<xsl:apply-templates select="ELEMENTS/EDITORELEMENT" mode="CONTACT"/>   
 
<xsl:template match="CONTENT/*" mode="CONTACT">
   <xsl:copy-of select="node()"/>
   <xsl:apply-templates select="IPS_OBJECT/FILE"/>
</xsl:template>
 
<xsl:template match="IPS_OBJECT/FILE[FILE/FILETYPE = 'image']" mode="CONTACT">
    <xsl:if test="[FILE/FILETYPE = 'image']">
         <img src="filestore/{FILENAME/}" width="" alt="{NAME}"/>
    </xsl:if>
</xsl:template>
 
XML CODE SNIPPET:
<ELEMENTS>
                     <EDITORELEMENT>
                        <ARTICLE_ID>12885</ARTICLE_ID>
                        <PRI>1</PRI>
                        <BREAKAFTER>false</BREAKAFTER>
                        <ID>14482</ID>
                        <CONTENT>
                           <body>
                              <table border="0" cellpadding="4" cellspacing="0">
                                 <tbody>
                                    <tr>
                                       <td height="100" width="100">
                                          <IPS_OBJECT NAMESPACE="articles_articlecontent_14482_" OBJECT_ID="34168" OBJECT_TYPE="Files.File">
                                             <FILE>
                                                <ID>34168</ID>
                                                <FILEFOLDER_ID>1135</FILEFOLDER_ID>
                                                <NAME>FEDGER</NAME>
                                                <SIZE>7897</SIZE>
                                                <FILENAME>Sjkartverket/Primaregnenettsider/FEDGER_web.jpg</FILENAME>
                                                <MIMETYPE>image/jpeg</MIMETYPE>
                                                <FILETYPE>image</FILETYPE>
                                                <ATTRIBUTES></ATTRIBUTES>
                                                <ATTRIBUTES>
                                                   <ATTRIBUTE ID="113">
                                                      <NAME>Bredde</NAME><LABEL>width</LABEL><VALUE></VALUE>
                                                   </ATTRIBUTE>
                                                   <ATTRIBUTE ID="114">
                                                      <NAME>H&oslash;yde</NAME><LABEL>height</LABEL><VALUE></VALUE>
                                                   </ATTRIBUTE>
                                                </ATTRIBUTES>
                                             </FILE>
                                          </IPS_OBJECT>
                                       </td>
                                       <td>Fedde<br> Managing Director<br> <a href="http://www.xxx.com">FEDGER's link</a> 
                                       </td>
                                    </tr>
                                    <tr>
                                       <td height="100" width="100">
                                          <IPS_OBJECT NAMESPACE="articles_articlecontent_14482_" OBJECT_ID="34169" OBJECT_TYPE="Files.File">
                                             <FILE>
                                                <ID>34169</ID>
                                                <FILEFOLDER_ID>1135</FILEFOLDER_ID>
                                                <NAME>OLSKJE</NAME>
                                                <SIZE>7545</SIZE>
                                                <FILENAME>Sjkartverket/Primaregnenettsider/OLSKJE_web.jpg</FILENAME>
                                                <MIMETYPE>image/jpeg</MIMETYPE>
                                                <FILETYPE>image</FILETYPE>
                                                <ATTRIBUTES></ATTRIBUTES>
                                                <ATTRIBUTES>
                                                   <ATTRIBUTE ID="113">
                                                      <NAME>Bredde</NAME><LABEL>width</LABEL><VALUE></VALUE>
                                                   </ATTRIBUTE>
                                                   <ATTRIBUTE ID="114">
                                                      <NAME>H&oslash;yde</NAME><LABEL>height</LABEL><VALUE></VALUE>
                                                   </ATTRIBUTE>
                                                </ATTRIBUTES>
                                             </FILE>
                                          </IPS_OBJECT>
                                       </td>
                                       <td>OLSKJE<br> Dir. PRIMAR<br> <a href="www.xxx.com">OLSKJE's link</a>
                                       </td>
                                    </tr>
                                    <tr>
                                       <td height="100" width="100">
                                          <IPS_OBJECT NAMESPACE="articles_articlecontent_14482_" OBJECT_ID="34170" OBJECT_TYPE="Files.File">
                                             <FILE>
                                                <ID>34170</ID>
                                                <FILEFOLDER_ID>1135</FILEFOLDER_ID>
                                                <NAME>SHASAC</NAME>
                                                <SIZE>10024</SIZE>
                                                <FILENAME>Sjkartverket/Primaregnenettsider/SHASAC_web.jpg</FILENAME>
                                                <MIMETYPE>image/jpeg</MIMETYPE>
                                                <FILETYPE>image</FILETYPE>
                                                <ATTRIBUTES></ATTRIBUTES>
                                                <ATTRIBUTES>
                                                   <ATTRIBUTE ID="113">
                                                      <NAME>Bredde</NAME><LABEL>width</LABEL><VALUE></VALUE>
                                                   </ATTRIBUTE>
                                                   <ATTRIBUTE ID="114">
                                                      <NAME>H&oslash;yde</NAME><LABEL>height</LABEL><VALUE></VALUE>
                                                   </ATTRIBUTE>
                                                </ATTRIBUTES>
                                             </FILE>
                                          </IPS_OBJECT>
                                       </td>
                                       <td>SHASAC<br> RENC/ENC Coordinator<br> <a href="www.xxx.com">SHASAC</a>
                                       </td>
                                    </tr>
                                 </tbody>
                              </table>
                           </body>
                        </CONTENT>
                     </EDITORELEMENT>
                  </ELEMENTS>

Open in new window

mbjorgeAsked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
well, you are giving the wrong message to the browser.
You say it is xhtml strict and then you do all sorts of things that make the xhtml not valid
- you have a body tag inside a div
- you have a CONTENT element that switches the default namespace
- you have a IPS_OBJECT element that should not be there

There is an issue creating xhtml with xslt1 processors by the way
it is not an allowed method.
If you serialise as html you will miss some end tags.
For xhtml, you preferably serialise as XML and drop the xml declaration
but I believe there are issues with IE7 if you have xhtml and it is not valid, you then get into some sort of quirks mode
I recommend that you first try to get it valid by removing the unnecessary stuff, add the proper namespace, and if it then doesn't work, revert all to html4, unless that is unacceptable

you give the impression that you use saxon extensions since you have that namespace declared.
if you use saxon 6, you might as well use saxon9 and use XSLT2. Then you can have the output method xhtml
0
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
In that case you can't just make a deep copy using copy-of.
You should handle every node with an identity copy template, except the OPS_OBJECT node which you need to threat specially
(emulating a pseudo deep copy)
    <xsl:template match="CONTENT">
        <xsl:apply-templates select="node()" mode="CONTACT"/>
    </xsl:template>
    
    <xsl:template match="node()" mode="CONTACT">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()" mode="CONTACT"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="IPS_OBJECT/FILE[FILETYPE = 'image']" mode="CONTACT">
        <img src="filestore/{FILENAME}" width="" alt="{NAME}"/>
    </xsl:template>
    <xsl:template match="IPS_OBJECT/FILE[not(FILETYPE = 'image')]" mode="CONTACT"/>

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
I also set some things right in the File handling (FILE has no FILE child and all that)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mbjorgeAuthor Commented:
Many thanks,

It works...nearly.  However, the images are not visible on IE but visible on Firefox. I also get "12885  1  false  14482".  What do you think the problem is on IE?

This is the source code it outputs:
<tr>
   <td height="90" width="90">
    <IPS_OBJECT NAMESPACE="articles_articlecontent_14482_" OBJECT_ID="34168" OBJECT_TYPE="Files.File" STYLE="width: 90px"><STYLE><PARAM NAME="width">90px</STYLE><img xmlns="http://www.w3.org/1999/xhtml" src="filestore/Sjkartverket/Primaregnenettsider/FEDGER_web.jpg" width="" alt="FEDGER"></img></IPS_OBJECT>
   </td>
  <td width="100">Fedde<br> Managing Director<br> <a href="#" target="_blank" >FEDGER's link</a></td>
</tr>

Open in new window

0
 
mbjorgeAuthor Commented:
Hi,

I got rid of the "12885  1  false  14482" but I still cannot get the img to show on IE6 and 7.  Works fine other browsers (Firefox, Opera, Google Chrome.

Kindly help


0
 
Geert BormansInformation ArchitectCommented:
seems a html issue,
did you try transforming outside the browser and opening the resulting HTML with the browser.
Can you post a relevant pece of the resulting html
do you serialise as html or as xml
the problem could be in the end tag
I am not sure that every browser excepts <img></img>, some prefer <img> (end tag ommission in the SGML DTD for html4)
you can get there by making the output serilaisation explicit by setting the xsl outpu

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
....
0
 
mbjorgeAuthor Commented:
It's serialised as html.  I'm using XHTML Strict.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" encoding="utf-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" xmlns:saxon="http://saxon.sf.net/" omit-xml-declaration="yes"/>

HTML code:
<div>
<CONTENT xmlns="">
 <body>
 <table border="0" cellpadding="2" cellspacing="0" width="190">
 <tbody>
  <tr>
  <td height="90" width="90">
  <IPS_OBJECT NAMESPACE="articles_articlecontent_14482_" OBJECT_ID="34168" OBJECT_TYPE="Files.File" STYLE="width: 90px"><STYLE><PARAM NAME="width">90px</STYLE><img xmlns="http://www.w3.org/1999/xhtml" src="http://www.primar.no/admin/filestore/Sjkartverket/Primaregnenettsider/FEDGER_web.jpg" width="" alt="Gerry Larsson-Fedde"></img></IPS_OBJECT>
   </td>
   <td width="100">Capt. Gerry Larsson-Fedde<br> Managing Director<br> <a href="psm://gerry.larsson-fedde@statkart.no" target="_blank" title="blocked::mailto:gerry.larsson-fedde@statkart.no">gerry.larsson-fedde@statkart.no</a><br/>
    </td>
  </tr>
  <tr>
  <td height="90" width="90">
  <IPS_OBJECT NAMESPACE="articles_articlecontent_14482_" OBJECT_ID="34169" OBJECT_TYPE="Files.File" STYLE="width: 90px"><STYLE><PARAM NAME="width">90px</STYLE><img xmlns="http://www.w3.org/1999/xhtml" src="http://www.primar.no/admin/filestore/Sjkartverket/Primaregnenettsider/OLSKJE_web.jpg" width="" alt="Kjell Olsen"></img></IPS_OBJECT>
   </td>
  <td>Kjell Olsen<br> Dir. PRIMAR<br> <a href="psm://kjell.olsen@statkart.no">kjell.olsen<br> @statkart.no</a><br> + 47 51 85 87 91
  </td>
</tr>
<tr>
  <td height="90" width="90">
   <IPS_OBJECT NAMESPACE="articles_articlecontent_14482_" OBJECT_ID="34170" OBJECT_TYPE="Files.File" STYLE="width: 90px"><STYLE><PARAM NAME="width">90px</STYLE><img xmlns="http://www.w3.org/1999/xhtml" src="http://www.primar.no/admin/filestore/Sjkartverket/Primaregnenettsider/SHASAC_web.jpg" width="" alt="Sach Sharma"></img></IPS_OBJECT>
  </td>
  <td>Capt. Sach Sharma<br> RENC/ENC Coordinator<br> <a href="mailto:sach.sharma@statkart.no">sach.sharma<br> @statkart.no</a><br> 
  </td>
 </tr>
 </tbody>
</table>
</body>
</CONTENT>
</div>

Open in new window

0
 
mbjorgeAuthor Commented:
Hi,

The XML is generated by the CMS (the body tag, the IPS_Object and all), I can't change that.  I am also using saxon 6 (Ican't change that either).  Is there a work around, I don't really want to use html4.
0
 
Geert BormansInformation ArchitectCommented:
I don't really care about the XML, that is something you can change in the XSLT.
But you see those tags in the resulting xhtml, you should remov those
(or are you feeding in into an templating engine?)

I recommend that you reorganise your XSLT diferently in order to remove the special tags, get the div inside the body, instead of outside, make XML the output method, strip the xml declaration (that you allready do)

If you want me to help you with that, send in a source XML file and the XSLT, so I can fix it for you
If there is sensitive data in there, find my email in my profile

cheers

Geert
0
 
mbjorgeAuthor Commented:
I have sent the data to your email.  Many thanks

Cheers

MB
0
 
mbjorgeAuthor Commented:
Many thanks again.
0
 
Geert BormansInformation ArchitectCommented:
Just as a post facta comment for others interested in this question.
The problem has been dealt with in the larger context of the full stylesheet via email.

The issue was indeed the namespace switch from xhtml namespace to null namespace in this piece of the result xhtml
<CONTENT xmlns="">
 <body>
resulting in the <img> not being in the xhtml namespace
(actually IE is doing the right thing NOT to show the image, all other browsers are wrong)

This can easily be solved in XSLT by using xsl:element and forcing the correct namespace
  <xsl:element name="img" namespace="http://www.w3.org/1999/xhtml">
    <xsl:attribute name="src">
      <xsl:text>http://www.primar.no/admin/filestore/</xsl:text>
      <xsl:value-of select="NAME"/>
    </xsl:attribute>


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.