Jump to content

Please help- XML & XSLT


getmizanur

Recommended Posts

elloo,

 

I'm new to XML and XSLT and I've thought to learn more about it by trying do something basic. In this exercise I try to query the company.xml file using xpath and trying to save the result in result.xml.

 

When I run my code I get this in result.xml:

<?xml version="1.0" encoding="UTF-8"?>
<annual>
<company>
	<turnover id="2001">100,000</turnover>
	<turnover id="2002">200,000</turnover>
	<turnover id="2003">300,000</turnover>
	<employee id="001">Dicovery</employee>
</company>
<company>
	<turnover id="2001">300,000</turnover>
	<turnover id="2002">300,000</turnover>
	<turnover id="2003">400,000</turnover>
	<employee id="002">World</employee>
</company>
</annual>

 

However my output should be this in result.xml

<?xml version="1.0" encoding="UTF-8"?>
<annual>
<company>
	<turnover id="2001">100,000</turnover>
	<turnover id="2002">200,000</turnover>
	<turnover id="2003">300,000</turnover>
	<employee id="001">Dicovery</employee>
</company>
</annual>

 

 

 

########Here Is My Code###############

test.xsl

<?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"/>
<xsl:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
<xsl:output doctype-public="-//W3//DTD XHTML 1.0 Strict//EN"/>

<xsl:template match="annual">
	<html>
		<head>
			<title><![CDATA[Annual Data & Report]]></title>
		</head>
		<body>
			<ul>
				<xsl:apply-templates select="company"/>
			</ul>
		</body>
	</html>
</xsl:template>

<xsl:template match="company">
	<li><xsl:apply-templates/></li>
</xsl:template>
</xsl:stylesheet>

 

company.xml

<?xml version="1.0" encoding="UTF-8"?>
<annual>
<company>
	<turnover id="2001">100,000</turnover>
	<turnover id="2002">200,000</turnover>
	<turnover id="2003">300,000</turnover>
	<employee id="001">Dicovery</employee>
</company>
<company>
	<turnover id="2001">300,000</turnover>
	<turnover id="2002">300,000</turnover>
	<turnover id="2003">400,000</turnover>
	<employee id="002">World</employee>
</company>
</annual>

 

 

<?php
define("DEBUGGING", true);
define("IS_WARNING_FATAL", true);
define("SITE_GENERIC_ERROR_MESSAGE", "This site has taken a long break, I suggest you do the same");

set_error_handler("fusionad_error_handler", E_ALL);

function fusionad_error_handler($errNo, $errStr, $errFile, $errLine) {
$backtrace = print_backtrace(2);
$error_message = "\nERROR: $errNo \nTEXT: " . $errStr .
		"\nLOCATION: " . $errFile . " Line " . $errLine . "\n" .
		date("F j, Y, g:i:a") . "\nBacktrace:\n" . $backtrace . "\n\n";

if(($errNo == E_WARNING && IS_WARNING_FATAL == false) || 
		($errNo == E_NOTICE || $errNo == E_USER_NOTICE)) {
	if(DEBUGGING == true) {
		echo "<pre>" . $error_message . "</pre>";
	}
}else {
	if(DEBUGGING == true)
		echo "<pre>" . $error_message . "</pre>";
	else{
		echo "<pre>" . SITE_GENERIC_ERROR_MESSAGE . "</pre>";
	}
	exit;
}
}

function print_backtrace($irrelevantTrace) {
$s = '';
$MAXSTRLEN = 64;
$traceArr = debug_backtrace();
for($i=0; $i<$irrelevantTrace; $i++) {
	array_shift($traceArr);
}
foreach($traceArr as $arr) {
	if(!empty($arr['class']))
		$s = $arr['class'] . ".";
	$args = array();
	if(!empty($arr['args'])){
		foreach($arr['args'] as $v) {
			if(is_null($v)) {
				$args[] = 'null';
			}else if (is_array($v)) {
				$args[] = "Array[" . sizeof($v) . "]";
			}else if (is_object($v)) {
				$args[] = "Object: " . get_class($v);
			}else if (is_bool($v)) {
				$args[] = ($v ? 'true' : 'false');
			}else {
				$v = (string)@$v;
				$str = htmlspecialchars(substr($v, 0, $MAXSTRLEN));
				if(strlen($v) > $MAXSTRLEN)
					$str .= "...";
				$args[] = "\"" . $str . "\"";
			}
		}
	}
	$s .= $arr['function'] . "(" . implode(", ", $args) . ")";
	$Line = (isset($arr['line']) ? $arr['line'] : "unknown");
	$File = (isset($arr['file']) ? $arr['file'] : "unknown");
	$s .= sprintf(" # line %4d, file: %s", $Line, $File);
	$s .= "\n";
}
return $s;
}

class Test {
/* private members */
private $mXmlDoc;
private $mXpath;
private $mXsltPorc;
private $mXml;
private $mXslt;
private $mHtml;

/* public members */
public $mOutput;

/* default constructor */
public function __construct($xml) {
	$this->mXmlDoc = new DomDocument();
	if(!($this->mXmlDoc->load($xml))) {
		trigger_error();
	}
}

/**
 * Function sets the XML output file name
 *
 * @param $xml - filename of the output file
 */
public function output($xml) {
	if(isset($xml) && strlen($xml) > 0) {
		$this->mOutput = $xml;
	}else{
		trigger_error();
	}
}

/**
 * Function prints XML data
 *
 * @ param $xml - filename string
 */
public function xmlQuery($xpath) {
	$this->mXpath = new DomXPath($this->mXmlDoc);		
	$node_list = $this->mXpath->query($xpath, $this->mXmlDoc);

	foreach($node_list as $node) {
		echo "<pre>" . $this->mXmlDoc->saveXML($node) . "</pre>";
	}

	$this->mXmlDoc->save($this->mOutput);
}

/**
 * Function convert XML to HTML
 *
 * @param $xml - xml filename
 * @param $xsl - xsl filename
 */
public function xml2html($xml, $xsl) {
	// create xslt processor
	$this->mXsltProc = new XsltProcessor();

	$this->mXml = new DomDocument();
	if(!($this->mXml->load($xml))) {
		trigger_error();
	}

	$this->mXslt = new DomDocument();
	$this->mXslt->load($xsl);

	$this->mXsltProc->importStylesheet($this->mXslt);

	if(!($this->mHtml = $this->mXsltProc->transformToXML($this->mXml))) {
		trigger_error();
	}

	return $this->mHtml;
}
}
$xmlProc = new Test("company.xml");
$xmlProc->output("result.xml");
$xmlProc->xmlQuery("/annual/company[employee[@id = '001']");
echo $xmlProc->xml2html($xmlProc->mOutput, "test.xsl");

Link to comment
https://forums.phpfreaks.com/topic/173325-please-help-xml-xslt/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.