...

View Full Version : DOM XML stop escaping



kieran491
12-18-2008, 08:30 AM
Hi there i am trying to use DOM to put data into an xml tree and pass that off to xsl what i was wondering is how do you pass XML to dom and not have it escaped for example



$Template->paragraph("
<strong>
Welcome to Site, an Australian based game server provider.
</strong>");




&lt;strong&gt;
Welcome to Site, an Australian based game server provider.
&lt;/strong&gt;


What i would like is



<strong>
Welcome to Site, an Australian based game server provider.
</strong>

Fou-Lu
12-18-2008, 10:25 AM
That will need to be cdata wrapped. If you embed a tag within an xml document it treats it as a new element, which is not what you want. The other option is to leave it as is and convert it back using htmlspecialchars_decode.
To change it, you'll need to alter the $Template object's method: paragraph.

kieran491
12-18-2008, 02:23 PM
i am using xsl to display the final result so i am not sure how well htmlspecialchars_decode will preform under heavy load

Fou-Lu
12-18-2008, 03:25 PM
Then wrap it in a cdata block. I think dom xml has it (dom xml is a little on the old side), but if it doesn't you should be able to wrap <![CDATA[<stuff>]]> tags around you're input, and put it in as a text node.

kieran491
12-18-2008, 03:30 PM
I have tryed using the cdata block trick it still seems to come accross encoded either by XSLTProcessor or DOMDocument

oesxyl
12-18-2008, 04:03 PM
I have tryed using the cdata block trick it still seems to come accross encoded either by XSLTProcessor or DOMDocument
did you try this way?


$Template->paragraph("<![CDATA[<strong>Welcome to Site, an Australian based game server provider.</strong>]]>");


what extension did you use dom or dom xml and what php version?

best regards

kieran491
12-18-2008, 04:17 PM
i am using "PHP Version 5.2.6"
and i am using DOM

the actual code that turns the strings and array into xml is



private function xml(){
$document = $this->xml->createElement('document');
$title = $this->xml->createElement('title',$this->title);
$body = $this->xml->createElement('body');
foreach($this->paragraphs as $value){
$paragraph = $this->xml->createElement('paragraph',$value);
$body->appendChild($paragraph);
}
$document->appendChild($title);
$document->appendChild($body);

foreach($this->section as $Value){
$document->appendChild($Value->output());
}
$this->xml->appendChild($document);
return $this->xml;
}


and



private function xml(){
$section = $this->xml->createElement('section');
$title = $this->xml->createElement('title',$this->title);
$body = $this->xml->createElement('body');
if(count($this->body) > 0){
foreach($this->body as $value){
$paragraph = $this->xml->createElement('paragraph',$value);
$body->appendChild($paragraph);
}
}else{
$body->appendChild($this->xml->createElement('paragraph'));
}

oesxyl
12-18-2008, 04:39 PM
this is an example how you can use createCDATASection:


<?php
$doc = new DOMDocument('1.0','utf-8');
if($doc){
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$node = $doc->createElement('root');
$doc->appendChild($node);
// from here
$cdata = $doc->createCDATASection('<p>test</p>');
$node->appendChild($cdata);
echo $doc->saveXML();
}
?>

seems that you can deal with that by your own, :)

http://www.php.net/manual/en/domdocument.createelement.php

best regards

kieran491
12-18-2008, 05:00 PM
I have updated the code to reflect the sugested ideas the output i get from php xml wise is



<?xml version="1.0" encoding="UTF-8"?>
<document><title>Welcome</title><body><paragraph><![CDATA[
<strong>
Welcome to Site, an Australian based game server provider.
</strong>
<br />
We supply the gaming community of Australia with high bandwidth, low latency quality servers at competitive prices.
<br />
We could say we are one of the best server providers in Australia, like all those other companies do but we believe in honesty at gExtreme. If there is a server problem, we won't beat around the bush, instead we will let you know and get it fixed right away!
<br />
<br />
Everyone wants the best server they can get for their money, but do you know what to look for? Contact our support team and let us know about the plans for your server and we will let you know what kind of services you require, whether we offer it or not.
<br />]]></paragraph></body><section template="index"><title>Test Document</title><body><paragraph><![CDATA[This is a simple test document.]]></paragraph><paragraph><![CDATA[Heres some more document that can be printed]]></paragraph></body></section></document>



XSL wise though



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title><meta http-equiv="Content-Language" content="English"></meta><meta name="author" content=".com"></meta><meta name="description" content="counter strike source servers"></meta><meta name="keywords" content=",counter,strike,source,servers,australia,brisbane"></meta><meta name="Robots" content="index,follow"></meta><link rel="stylesheet" type="text/css" href="css/main.css" media="screen"></link></head><body><div id="hd"><ul><li id="logo"><a href="http://gextreme.com" title="Go to Home page">

</a></li></ul></div><div id="ct"><div id="ctr"><dl><dt><strong>
Counter Strike:Source Special
</strong></dt><dd><br></br>
Get the "" tag removed from your server name
absolutely free if you signup now!
<br></br><span class="termsapply">
* Terms and conditions may apply
</span><br></br></dd></dl><h2>
Ping Test Server
</h2><ol><li><strong>

Server Name
</strong>
: .com // Ping Test
</li><li><strong>
Max Players
</strong>
: 4
</li><li><strong>
Map
</strong>
: de_aztec
</li></ol><div style="text-align: center"><a href="steam://connect/202.60.72.168:27015/"><strong>

Connect
</strong></a></div><br></br><h2>
Random Testimony
</h2><div style="text-align: center">
im surprised ur servers run my zombie mod server so good. ty
<br></br><br></br><span class="trueblack">
Written By: REC. John Doyle
</span></div></div><ul id="men"><li id="m1"><a href="index.php" title="" class="act">
Home
</a></li><li id="m2"><a href="forum.php" title="">
Forums
</a></li><li id="m3"><a href="order.php" title="">

Order
</a></li><li id="m4"><a href="faq.php" title="">
FAQ
</a></li><li id="m5"><a href="contact.php" title="">
Contact
</a></li></ul><div id="ctc"><h1>Welcome</h1><p><paragraph xmlns="">
&lt;strong&gt;
Welcome to Site, an Australian based game server provider.
&lt;/strong&gt;

&lt;br /&gt;
We supply the gaming community of Australia with high bandwidth, low latency quality servers at competitive prices.
&lt;br /&gt;
We could say we are one of the best server providers in Australia, like all those other companies do but we believe in honesty at gExtreme. If there is a server problem, we won't beat around the bush, instead we will let you know and get it fixed right away!
&lt;br /&gt;
&lt;br /&gt;
Everyone wants the best server they can get for their money, but do you know what to look for? Contact our support team and let us know about the plans for your server and we will let you know what kind of services you require, whether we offer it or not.
&lt;br /&gt;</paragraph></p><h2 xmlns="">Test Document</h2>

<p xmlns=""><paragraph>This is a simple test document.</paragraph></p>
<p xmlns=""><paragraph>Heres some more document that can be printed</paragraph></p></div></div><div id="ft">
Copyright 2008 gExtreme.com.
</div></body></html>





private function xml(){
$document = $this->xml->createElement('document');
$title = $this->xml->createElement('title',$this->title);
$body = $this->xml->createElement('body');
foreach($this->paragraphs as $value){
$CDATA = $this->xml->createCDATASection($value);
$paragraph = $this->xml->createElement('paragraph');
$paragraph->appendChild($CDATA);
$body->appendChild($paragraph);
}
$document->appendChild($title);
$document->appendChild($body);

foreach($this->section as $Value){
$document->appendChild($Value->output());
}
$this->xml->appendChild($document);
return $this->xml;
}




private function xml(){
$section = $this->xml->createElement('section');
$title = $this->xml->createElement('title',$this->title);
$body = $this->xml->createElement('body');
if(count($this->body) > 0){
foreach($this->body as $value){
$CDATA = $this->xml->createCDATASection($value);
$paragraph = $this->xml->createElement('paragraph');
$paragraph->appendChild($CDATA);
$body->appendChild($paragraph);
}
}else{
$body->appendChild($this->xml->createElement('paragraph'));
}

$section->setAttribute('template',$this->template);
$section->appendChild($title);
$section->appendChild($body);
return $section;
}


I am still getting results that just dont make sence

oesxyl
12-18-2008, 05:31 PM
xslt is guilty, :) If I remember you have disable-output-escaping="no", change it to yes and check again.

best regards

kieran491
12-18-2008, 05:43 PM
are we talking about http://au2.php.net/manual/en/book.xsl.php?

i cant seem to find anywhere in the manual refering to that particular varible or function

oesxyl
12-18-2008, 05:49 PM
are we talking about http://au2.php.net/manual/en/book.xsl.php?

i cant seem to find anywhere in the manual refering to that particular varible or function
no, about you xslt file from your post in xml forum. I remember something like:


<xsl:value-of disable-output-escaping="..."

you can use disable-output-escaping="yes" with both xsl:value-of and xsl:copy-of.

The php output from your previous post seems ok.

best regards

kieran491
12-18-2008, 05:59 PM
I put the <xsl:copy-of select="." disable-output-escaping="yes" /> in there but i still get escaped charecters just like before

oesxyl
12-18-2008, 06:15 PM
I put the <xsl:copy-of select="." disable-output-escaping="yes" /> in there but i still get escaped charecters just like before
what do you use to transform, transformToDoc or transformToXML?

best regards

kieran491
12-18-2008, 06:22 PM
i am using transformToXML



public function __toString(){
$XSLDom = new DOMDocument();
$XSLDom->load($this->path . '/' . $this->file . '.xsl');
$this->xsl->importStylesheet($XSLDom);
return $this->xsl->transformToXML($this->xml());
}


To change the XML to a xhtml ouput

oesxyl
12-18-2008, 07:07 PM
i am using transformToXML



public function __toString(){
$XSLDom = new DOMDocument();
$XSLDom->load($this->path . '/' . $this->file . '.xsl');
$this->xsl->importStylesheet($XSLDom);
return $this->xsl->transformToXML($this->xml());
}

not sure what happend there, try to switch transformToXML with transformToDoc and if not work switch back. The original problem is solved because the xml output is inside the CDATA section as you wish.
I don't know why you want this because xsl:copy-of don't need that but anyway the problem is inside the xslt file.

best regards

kieran491
12-19-2008, 04:34 PM
The code i am using is....



<?php
/**
*
* @access public
* @author Kieran
*
*/
Class Template {
/**
* Used to point at the directory where the file is located
* @var string
*/
protected $path = null;
/**
* Used to point a the file minus the extension (.xsl)
* @var string
*/
protected $file = null;

/**
* DOMDocument
* @var DOMDocument
*/
protected $xml = null;
/**
* XSLTProcessor to contain the xsl
* @var XSLTProcessor
*/
protected $xsl = null;

/**
* title
* @var string
*/
protected $title = null;
/**
* paragraphs
* @var mixed
*/
protected $paragraphs = array();
/**
* Contains all the referances to Section and what template needs to be applyed to them
* @var mixed
*/
protected $section = array();

/**
* @access public
* @return void
*/
public function __construct(){
$this->xml = new DOMDocument('1.0', 'UTF-8');
$this->xsl = new XSLTProcessor();
}

public function __get($index){
return $this->section($index);
}

/**
* Sets the path to the view directory
* @access public
* @param string $path
* @return void
*/
public function setPath($path){
if(!is_dir($path)){
throw new Exception('View path is invalid `' . $path . '`');
}
$this->path = $path;
}

/**
* Sets the XSL file in the view directory
* @access public
* @param $XSL
* @return void
*/
public function setXSL($XSL){
if(!is_readable($this->path . '/' . $XSL . '.xsl')){
throw new Exception('View file is invalid `' . $path . '`');
}
$this->file = $XSL;
}

/**
* Creates new section if they do not exists if they do exists they return the sections
* @access public
* @param string $name
* @return section
*/
public function section($name){
if(isset($this->section[$name])){
return $this->section[$name];
}else{
return $this->section[$name] = new Section($this->xml);
}
}

/**
* Sets the Site title
* @access public
* @param $title
* @return void
*/
public function title($title){
$this->title = $title;
}

/**
* Sets content
* @access public
* @param $content
* @return void
*/
public function paragraph($content){
$this->paragraphs[] = $content;
}

/**
* proccess all the text data into XML
* @access private
* @return resource
*/
private function xml(){
$document = $this->xml->createElement('document');
$title = $this->xml->createElement('title',$this->title);
$body = $this->xml->createElement('body');
foreach($this->paragraphs as $value){
$CDATA = $this->xml->createCDATASection($value);
$paragraph = $this->xml->createElement('paragraph');
$paragraph->appendChild($CDATA);
$body->appendChild($paragraph);
}
$document->appendChild($title);
$document->appendChild($body);

foreach($this->section as $Value){
$document->appendChild($Value->output());
}
$this->xml->appendChild($document);
return $this->xml;
}

/**
* Returns the xml document as a string
* @return string
*/
public function debugXml(){
return $this->xml->saveXML($this->xml());
}

/**
* Proccess the page and replaces xml with the template
* @access public
* @return string
*/
public function __toString(){
$XSLDom = new DOMDocument();
$XSLDom->load($this->path . '/' . $this->file . '.xsl');
$this->xsl->importStylesheet($XSLDom);
return $this->xml->save($this->xsl->transformToXml($this->xml()));
}
}

class Section {
protected $xml = null;
protected $template = '';
protected $title = '';
protected $body = array();

public function __construct(DOMDocument $xml){
$this->xml = $xml;
}

public function title($title){
$this->title = $title;
}

public function paragraph($content){
$this->body[] = $content;
}

public function template($template){
$this->template = $template;
}

private function xml(){
$section = $this->xml->createElement('section');
$title = $this->xml->createElement('title',$this->title);
$body = $this->xml->createElement('body');
if(count($this->body) > 0){
foreach($this->body as $value){
$CDATA = $this->xml->createCDATASection($value);
$paragraph = $this->xml->createElement('paragraph');
$paragraph->appendChild($CDATA);
$body->appendChild($paragraph);
}
}else{
$body->appendChild($this->xml->createElement('paragraph'));
}

$section->setAttribute('template',$this->template);
$section->appendChild($title);
$section->appendChild($body);
return $section;
}

public function output(){
return $this->xml();
}

public function __toString(){
return $this->xml->saveXML($this->xml());
}
}
//header('Content-type: text/xml');
$Template = new Template();
$Template->setPath('.');
$Template->setXSL('untitled');

$Template->title('Welcome');
$Template->paragraph("
<strong>
Welcome to Site, an Australian based game server provider.
</strong>
<br />
We supply the gaming community of Australia with high bandwidth, low latency quality servers at competitive prices.
<br />
We could say we are one of the best server providers in Australia, like all those other companies do but we believe in honesty at gExtreme. If there is a server problem, we won't beat around the bush, instead we will let you know and get it fixed right away!
<br />
<br />
Everyone wants the best server they can get for their money, but do you know what to look for? Contact our support team and let us know about the plans for your server and we will let you know what kind of services you require, whether we offer it or not.
<br />");
$Template->a->template('index');
$Template->a->title('Test Document');
$Template->a->paragraph('This is a simple test document.');
$Template->a->paragraph('Heres some more document that can be printed');
//print "<pre>";var_dump($Template);print "</pre>";
//print $Template->debugXml();
print $Template;

/*
$a = new section($xml);
$a->title('Test');
$a->paragraph('aaa');
$a->paragraph('aaa1');
$a->paragraph('aaa2');
$a->paragraph('aaa0');
$a->title('2');
print $a;*/
?>



the xsl file is...




<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>

</title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="author" content=".com" />
<meta name="description" content="counter strike source servers" />
<meta name="keywords" content=",counter,strike,source,servers,australia,brisbane" />
<meta name="Robots" content="index,follow" />
<link rel="stylesheet" type="text/css" href="css/main.css" media="screen" />
</head>
<body>
<div id="hd">
<ul>
<li id="logo">
<a href="http://.com" title="Go to Home page">

</a>
</li>
</ul>
</div>
<div id="ct">
<div id="ctr">
<dl>
<dt>
<strong>
Counter Strike:Source Special
</strong>
</dt>
<dd>
<br />
Get the &quot;&quot; tag removed from your server name
absolutely free if you signup now!
<br />
<span class="termsapply">
* Terms and conditions may apply
</span>
<br />
</dd>
</dl>
<h2>
Ping Test Server
</h2>
<ol>
<li>
<strong>
Server Name
</strong>
: .com // Ping Test
</li>
<li>
<strong>
Max Players
</strong>
: 4
</li>
<li>
<strong>
Map
</strong>
: de_aztec
</li>
</ol>
<div style="text-align: center">
<a href="steam://connect/202.60.72.168:27015/">
<strong>
Connect
</strong>
</a>
</div>
<br />
<h2>
Random Testimony
</h2>
<div style="text-align: center">
im surprised ur servers run my zombie mod server so good. ty
<br />
<br />
<span class="trueblack">
Written By: REC. John Doyle
</span>
</div>
</div>
<ul id="men">
<li id="m1">
<a href="index.php" title="" class="act">
Home
</a>
</li>
<li id="m2">
<a href="forum.php" title="">
Forums
</a>
</li>
<li id="m3">
<a href="order.php" title="">
Order
</a>
</li>
<li id="m4">
<a href="faq.php" title="">
FAQ
</a>
</li>
<li id="m5">
<a href="contact.php" title="">
Contact
</a>
</li>
</ul>
<div id="ctc">
<h1>
<xsl:value-of select="/document/title" disable-output-escaping="yes" />
</h1>
<xsl:for-each select="/document/body/paragraph">
<p>
<xsl:copy-of select="." disable-output-escaping="yes" />
</p>
</xsl:for-each>
<xsl:apply-templates select="document/section[@template = 'index']" mode="index" />
<xsl:apply-templates select="document/section[@template = 'faq']" mode="faq" />
</div>
</div>
<div id="ft">
Copyright 2008 .com.
</div>
</body>
</html>
</xsl:template>

<xsl:template match="section" mode="index">
<h2>
<xsl:value-of select="title" disable-output-escaping="yes" />
</h2>
<xsl:apply-templates select="body/paragraph" mode="index" />
</xsl:template>

<xsl:template match="body/paragraph" mode="index">
<p>
<xsl:copy-of select="." disable-output-escaping="yes" />
</p>
</xsl:template>

<xsl:template match="section" mode="faq">
<br />
<strong>
<xsl:value-of select="title" disable-output-escaping="yes" />
</strong>
<xsl:apply-templates select="body/paragraph" mode="faq" />
</xsl:template>

<xsl:template match="body/paragraph" mode="faq">
<blockquote>
<p>
<xsl:copy-of select="." disable-output-escaping="yes" />
</p>
</blockquote>
</xsl:template>
</xsl:stylesheet>


I really cant see whats wrong... but i still get the wrong output

oesxyl
12-19-2008, 09:53 PM
you can use disable-output-escaping="yes" with both xsl:value-of and xsl:copy-of.

I was wrong, don't work with xsl:copy-of.



<?php
public function __toString(){
$XSLDom = new DOMDocument();
$XSLDom->load($this->path . '/' . $this->file . '.xsl');
$this->xsl->importStylesheet($XSLDom);
// return $this->xml->save($this->xsl->transformToXml($this->xml()));
// replaced with:
return $this->xsl->transformToXml($this->xml);
}
?>




<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">

output can't be both html 4.0 and xhtml

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum