Solved

formating xml content

Posted on 2002-06-11
7
208 Views
Last Modified: 2013-11-19

I have a xml tag like this in my xml object,

<description>
<i>Hello</i><b>hai</b>
</description>

note:here <i>Hello</i><b>hai</b> is not the child tag or sibling tag of description.It is the content of the description tag.

Now What I want is when I put <xsl:value-of select="description"/> or <xsl:copy-of select="description"/> in my xsl file and transform it using XSLT, I want it in the formatted way.(i.e hai should be italic and hello should be bold).

But it is coming as such, since it is the content of the description tag.

Can anybody help?

regards
Shanmugaraja

0
Comment
Question by:dsraja
  • 4
  • 3
7 Comments
 

Expert Comment

by:dglachs
ID: 7072520
Hello,

There is one big mistake: Your code portion "<i>Hello</i><b>hai</b>" is stored the following way as content of your description node:

&lt;i&gt;Hello&lt;/i&gt;&lt;b&gt;hai&lt;/b&gt;

this means: an xml document may not contain any <> characters in a "PCDATA" node. The characters <,>,&,",' are translated as mentioned.

One possible solution for you is to use a CDATA-Node such as:

<![CDATA[<i>Hello</i><b>hai</b>]]>

Those nodes are not parsed and may contain not well-formed content. The other possible solution is to store the "i"-node with content and the "b"-node with content as part of your xml-object.

Hope this helps

regards
Dietmar
0
 

Author Comment

by:dsraja
ID: 7072587
Hello Dietmar,

Thanks for your suggestions.

If I put the node as <![CDATA[<i>Hello</i><b>hai</b>]]>

I am getting it as such, instead I want it in Italic and Bold format when I use <xsl:value-of select="description"/>
0
 

Author Comment

by:dsraja
ID: 7072679
Hello Dietmar,

Thanks for your suggestions.

If I put the node as <![CDATA[<i>Hello</i><b>hai</b>]]>

I am getting it as such, instead I want it in Italic and Bold format when I use <xsl:value-of select="description"/>
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Expert Comment

by:dglachs
ID: 7072765
Hello again,

you are trying to output the code via XSLT. This is a problem since the XSLT Parser may output only well formed content, and, although we have now a CDATA-Node with <,>,& characters, the XSL Parser converts the characters to &lt; and so on.

I suggest the following: Use your own "bold", "invert" elements in your xml-file such as:

<description>
<bold>Bold</bold><invert>Inverted</invert>
</description>

Then you can use the following style-sheet to get the desired result:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html"/>
<xsl:template match="description">
<html>
     <head>
          <title></title>
     </head>
     <body><xsl:apply-templates/></body>
</html>
</xsl:template>
<xsl:template match="bold">
<b><xsl:value-of select="."/></b>
</xsl:template>
<xsl:template match="invert">
<i><xsl:value-of select="."/></i>
</xsl:template>
</xsl:stylesheet>


Hope this helps (again)

Best regards
Dietmar
0
 

Author Comment

by:dsraja
ID: 7074860
Hello Dietmar,

I'll tell you the exact problem.

I don't have the control over the content of the description.It is coming from the database.

The description is formatted using a tool and entered into the database.it can be of any html format like,
<p align="center"> <font color="red" size="3"> etc..

So those values are stored into the database using htmlspecialchars function of php.so that may be like &lt;$gt;&amp; etc..

What I will do is, I read it from the database as such and build the description tag using $xmlobj->new_child() method. I got to show it in the page as such they have formatted using the tool(i.e the same design,color alignmemnt etc..)

I hope the content will be of well formatted one.I just need to translate all html entities.

So what can be done??

Also can you suggest me a method with which I can replace newline into <br/>

Regards,
Shanmugaraja
0
 

Accepted Solution

by:
dglachs earned 20 total points
ID: 7074908
Hello Shanmugaraja,

as with the first idea - use a CDATASection. This gives you the opportunity to store "unknown" HTML-Content.

An example:

$cdata= $doc->create_cdata_section( $content );
$description->append_child( $cdata );

Now you should have an xml structure like this:

<description><![CDATA[<p><font ... >]]></description>

To retrieve the content of the cdata section use the following:

$cdata = $description->first_child();
$content = $cdata->get_content();

Retrieving the CDATASection within a stylesheet will go like this.
<xsl:template match="description">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>

The second attribute of "value-of" should prevent from translating the characters again.

Best regards
Dietmar




0
 

Author Comment

by:dsraja
ID: 7075239
Hello Dietmar,

Thanks for your valuable suggestion.Atlast it worked for me.

I put <xsl:value-of select="description" disable-output-escaping="yes">, which has given me the formatted output.

With thanks and Regards,
Shanmugaraja
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

774 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