Solved

formating xml content

Posted on 2002-06-11
7
211 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
[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
 

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

636 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