?
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
Medium Priority
?
240 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
Technology Partners: 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 750 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

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

764 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