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
237 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

726 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