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
223 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now