You are here: Re: PHP saving embedded HTML in XML « PHP Programming Language « IT news, forums, messages
Re: PHP saving embedded HTML in XML

Posted by dcrackel on 02/21/07 18:23

On Feb 21, 12:37 pm, Ian Taylor <m...@ocset.reverse.previous.word.net>
wrote:
> dcrac...@gmail.com wrote:
> > I hope there is a simple solution to this, but I've been unable to
> > find it.
>
> > $dom = new DomDocument();
> > $dom->load("test.xml");
>
> > $test = $dom->getElementsByTagName("test");
> > $test->nodeValue = "<b>test</b>";
> > $dom->save("test.xml");
>
> > I would like the node in the xml file to look like:
> > <test><b>test</b></test>
>
> > Rather than the encoded version.
> > &lt;b&gt;test&lt;/b&gt;
>
> > What can be done to accomplish this task?
>
> > Thank you
> > dwain
>
> From what I can see, it's not really possible - if it was possible, and
> you tried to re-read the resulting xml file back, you would end up with
> something like:
>
> ...
> <test>
> <b>
> test
> </b>
> </test>
> ...
>
> where <b> is a child node of the <test> node, rather than part of the
> value of the <test> node.
>
> Therefore, entities like < and > do need to be encoded / decoded
> (possible with html_entity_decode()) when dealing with xml.
>
> If, however, you *are* looking to add <b> as a child node of <test>, I'm
> sure there are functions to handle that ( probably something like $child
> = $test->append_child('b') )- Hide quoted text -
>
> - Show quoted text -


Thank you for the advice,

I'm looking to save HTML inside the XML, for that example that text
may be viewed bold when shown in a browser.

------------------------------------ Test.xml
<?xml version="1.0"?>
<ROOT>
<TEST>
<NAME>TEST PRODUCT 01</NAME>
<DESC>This is a test product, this is <b>bold</b>.</DESC>
<PRICE>9.99</PRICE>
</TEST>
</ROOT>

------------------------------------ Test.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:output method="html"/>

<xsl:template match="/">
<html>
<xsl:for-each select="ROOT/TEST">
<P>
<hr />
<xsl:value-of select="NAME" /> <br />
<xsl:copy-of select="DESC |text()"/> <br />
<xsl:value-of select="PRICE" /> <br />
<hr />

</P>
</xsl:for-each>

</html>
</xsl:template>
</xsl:stylesheet>

------------------------------------------------------- Test.php
<?php

$xsl = new DomDocument();
$xsl->load("test.xsl");

$inputdom = new DomDocument();
$inputdom->load("test.xml");

$proc = new XsltProcessor();
$xsl = $proc->importStylesheet($xsl);

$newdom = $proc->transformToDoc($inputdom);
print $newdom->saveXML();
?>

----------------------------- Result
<?xml version="1.0" standalone="yes"?>
<html><P><hr/>TEST PRODUCT 01<br/>

<DESC>This is a test product, this is <b>bold</b>.</DESC>

<br/>9.99<br/><hr/></P></html>



-------------------------------- The problem

When a XML document is saved by PHP it looks like

<?xml version="1.0"?>
<ROOT>
<TEST>
<NAME>TEST PRODUCT 01</NAME>
<DESC>This is a test product, this is &lt;b&gt;bold&lt;/b&gt;.</
DESC>
<PRICE>9.99</PRICE>
</TEST>
</ROOT>



I need to be able to save the XML Doc after altering it in PHP:
EX:
$node->nodeValue = "This is a test product, this is <b>bold</b>.";
$dom->save("test.xml");

without encoding the "<b>"'s.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация