Solved

How do I format, in XSLT, an XML tag that contains both HTML and my own XML markup?

Posted on 2004-08-01
7
233 Views
Last Modified: 2013-11-19
I'm a complete beginner to XSL so bear with me!...


I have the following XML (in short):


<section>
      <sec_title>Case discussion</sec_title>
      <bodytext>
            <p><i>html and text here...</i></p>            
            
            <fig>
                  <figtitle>Title of figure 1</figtitle>
                  <fig_num>1</fig_num>
                  <graphic>figure1.gif</graphic>
                  <copyright>&#169; 2004 Copyright Line</copyright>
            </fig>
                        
            <p>more html and text here...</p>
            <p>and some more html and text here...</p>
            
            <fig>
                  <figtitle>Title of figure 2</figtitle>
                  <fig_num>2</fig_num>
                  <graphic>figure2.gif</graphic>
                  <copyright>&#169; 2004 Copyright Line</copyright>
            </fig>
            
            <p><b>blah...</b></p>
            <p><b>blah...</b></p>
            <p><b>blah...</b></p>
      </bodytext>
</section>

<section>
...
</section>


The question is...

How do I get the XSLT to render:

1) the HTML within the <bodytext> tag.  This must format the text in bold, italics and insert paragraphs where appropriate.

2) the <fig> tags within the <bodytext> tag.  The aim here is to parse the filename within the <graphic> tag into an <img> tag.
I know that this can be done by using an AVT such as: <img src='{bodytext/fig/graphic}'/> but I am having trouble getting the XSLT to display the image in the correct postition within the text

Any help would be much appreciated!

0
Comment
Question by:Steve23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:alikoank
ID: 11691829
you should begin with separating your data and formatting.

use XML to represent your data.

use XSL to format this data.

try to avoid putting formatting tags into your XML, like <b><i> etc... this tags should belong to your XSL file.
0
 

Author Comment

by:Steve23
ID: 11691926
Thanks alikoank

Problem is that the DTD and XML was written by someone else and has become fairly standard and cannot be modified.

My task is to create the XSL for what seems to be badly-formed XML.

Is there any way around this problem without having to modify the XML or its DTD?

0
 
LVL 4

Expert Comment

by:alikoank
ID: 11692415
well, the problem here is p and i and other tags are also valid xml tags, so you should declare them as templates. Try following XSL

----------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>

<xsl:template match="/">
  <html>
    <head>
    </head>
    <body>
      <xsl:apply-templates select="section"/>      
    </body>
  </html>
</xsl:template>

<xsl:template match="p">
  <p><xsl:value-of select="."/></p>
</xsl:template>

<xsl:template match="fig">
  <xsl:element name="img">
    <xsl:attribute name="src">
      <xsl:value-of select="graphic"/>
    </xsl:attribute>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
0
Industry Leaders: 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:Steve23
ID: 11701568
Thanks, things seem to be coming together now!

Had a play around with your code and came up with the following...

<!----------------------------->

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

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

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

<xsl:template match="fig">
  <xsl:element name="img">
    <xsl:attribute name="src">
      <xsl:value-of select="graphic"/>
    </xsl:attribute>
  </xsl:element>
</xsl:template>

<xsl:template match="bodytext">      
      <xsl:apply-templates select="p|i|b|fig"/>
</xsl:template>

<xsl:for-each select="section">
      <xsl:value-of select="sec_title"/>
      <xsl:apply-templates select="bodytext"/>
</xsl:for-each>

<!----------------------------->

This seems to do the trick.  Just want to know if this is along the right lines before I start to code the whole thing (the XML contains a lot of other tags as well).
0
 
LVL 4

Accepted Solution

by:
alikoank earned 250 total points
ID: 11704302
yep. You're doing well :-)

you do not need to use xsl:for-each for "section" part. You can do it also as in following

-------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="ISO-8859-1"/>


<xsl:template match="/">
  <html>
    <head>
    </head>
    <body>
      <xsl:apply-templates select="section" />
    </body>
  </html>
</xsl:template>

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

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

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

<xsl:template match="fig">
  <xsl:element name="img">
    <xsl:attribute name="src">
      <xsl:value-of select="graphic"/>
    </xsl:attribute>
  </xsl:element>
</xsl:template>

<xsl:template match="bodytext">    
     <xsl:apply-templates select="p|i|b|fig"/>
</xsl:template>

<xsl:template match="section">
     <xsl:value-of select="sec_title"/>
     <xsl:apply-templates select="bodytext"/>
</xsl:template>

-----------------------------------------------------

but its totally up to you.
0
 

Author Comment

by:Steve23
ID: 11704532
Thanks for the help alikoank.

The points are yours!
0
 
LVL 4

Expert Comment

by:alikoank
ID: 11704747
cheers :-)
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

740 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