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
232 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

828 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