[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

XSLT - copy xml node element with html tags

Posted on 2009-02-12
12
Medium Priority
?
1,753 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:mbjorge
  • 6
  • 6
12 Comments
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 1000 total points
ID: 23621757
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23621764
I also set some things right in the File handling (FILE has no FILE child and all that)
0
 

Author Comment

by:mbjorge
ID: 23622032
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mbjorge
ID: 23622088
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23622333
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
 

Author Comment

by:mbjorge
ID: 23622696
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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 total points
ID: 23622980
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
 

Author Comment

by:mbjorge
ID: 23626223
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23626289
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
 

Author Comment

by:mbjorge
ID: 23631240
I have sent the data to your email.  Many thanks

Cheers

MB
0
 

Author Closing Comment

by:mbjorge
ID: 31546009
Many thanks again.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23648168
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question