Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

formating xml content

Posted on 2002-06-11
7
Medium Priority
?
212 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
Independent Software Vendors: 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!

 

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 80 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

715 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