Solved

formating xml content

Posted on 2002-06-11
7
206 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

896 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

16 Experts available now in Live!

Get 1:1 Help Now