/******************************************************************
This example will build a stylesheet using the DOM API and
then perform a transformation using XML data we have manipulated
within another domDocument. The stylesheet built can be seen in
the file stylesheet.xsl. The data set used can be seen in the
the file xmldata.xml.
Normally stylesheets are prebuilt and loaded into a domDocument,
but the example illustrates many of the functions in the DOM API.
Taking this into consideration, the code was not shortened nor
optimized using user functions when written.
******************************************************************/
/* Set the xsl namespace url for re-use */
$xslns = "http://www.w3.org/1999/XSL/Transform";
/******************************************************************
Begin building the Stylesheet
BEGIN BUILDING STYLESHEET
******************************************************************/
$stylesheet = new domDocument;
/* Create the stylesheet node */
$root = $stylesheet->createElementNS($xslns, "xsl:stylesheet");
$stylesheet->appendChild($root);
$root->setAttribute("version", "1.0");
/* Create the output method node */
$output = $stylesheet->createElementNS($xslns, "xsl:output");
$output->setAttribute("method", "html");
$root->appendChild($output);
/******************************************************************
Create a template which we can call during transformation
BEGIN INCLUDED TEMPLATE
******************************************************************/
$template= $stylesheet->createElementNS($xslns, "xsl:template");
$template->setAttribute("name", "anchor_tags");
$root->appendChild($template);
/* Create a xsl parameter node setting attribute "name" to "url" */
$param = $stylesheet->createElementNS($xslns, "xsl:param");
$param->setAttribute("name", "url");
$template->appendChild($param);
/* Create a xsl parameter node setting attribute "name" to "company_name" */
$param = $stylesheet->createElementNS($xslns, "xsl:param");
$param->setAttribute("name", "company_name");
$template->appendChild($param);
/* Create a xsl parameter node setting attribute "name" to "target" */
$param = $stylesheet->createElementNS($xslns, "xsl:param");
$attr = new domAttr("name");
// $param->setAttribute("name", "target");
// $template->appendChild($param);
$param->setAttributeNode($attr);
$attr->nodeValue = "target";
$template->appendChild($param);
/* Create a xsl choose node */
$xslchoose = $template->appendChild($stylesheet->createElementNS($xslns, "xsl:choose"));
/* Add first xsl test case for the choose node
If parameter "url" passed to the template is empty then output
the value of the company_name paramter followed by a
*/
$xslwhen = $stylesheet->createElementNS($xslns, "xsl:when");
$xslwhen->setAttribute("test", "normalize-space(\$url)=''");
$xslvalue = $xslwhen->appendChild($stylesheet->createElementNS($xslns, "xsl:value-of"));
$xslvalue->setAttribute("select", "\$company_name");
$xslwhen->appendChild(new domElement("br"));
$xslchoose->appendChild($xslwhen);
/* If the test case above evaluates to false use this as the default case.
Output an tag using the url parameter as the value of the href attribute.
If parameter "target" passed to the template is not empty then output
the target attribute using the target parameter as the value.
*/
$xslwhen = $xslchoose->appendChild($stylesheet->createElementNS($xslns, "xsl:otherwise"));
$ahref = $xslwhen->appendChild(new domElement("a"));
$xslwhen->appendChild(new domElement("br"));
$xslattr = $ahref->appendChild($stylesheet->createElementNS($xslns, "xsl:attribute"));
$xslattr->setAttribute("name", "href");
$xslvalue = $xslattr->appendChild($stylesheet->createElementNS($xslns, "xsl:value-of"));
$xslvalue->setAttribute("select", "\$url");
$xslif = $ahref->appendChild($stylesheet->createElementNS($xslns, "xsl:if"));
$xslif->setAttributeNode(new domAttr("test", "normalize-space(\$target) != ''"));
$xslattr = $xslif->appendChild($stylesheet->createElementNS($xslns, "xsl:attribute"));
$xslattr->setAttribute("name", "target");
$xslvalue = $xslattr->appendChild($stylesheet->createElementNS($xslns, "xsl:value-of"));
$xslvalue->setAttribute("select", "\$target");
$xslif = $ahref->appendChild($stylesheet->createElementNS($xslns, "xsl:value-of"));
$xslif->setAttribute("select", "\$company_name");
/******************************************************************
END INCLUDED TEMPLATE
******************************************************************/
/******************************************************************
Create the main template which matches on the document root element
BEGIN MAIN TEMPLATE
******************************************************************/
$template= $stylesheet->createElementNS($xslns, "xsl:template");
$template->setAttribute("match", "/");
$root->appendChild($template);
$html = $template->appendChild(new domElement("html"));
$body = $html->appendChild(new domElement("body"));
$paragraph = $body->appendChild(new domElement("p"));
$xslvalueof = $stylesheet->createElementNS($xslns, "xsl:value-of");
$xslvalueof->setAttribute("select", "companies/title");
$paragraph->appendChild($xslvalueof);
/* Create a for-each loop and pass each company node to the anchor_tags template */
$xslforeach = $stylesheet->createElementNS($xslns, "xsl:for-each");
$body->appendChild($xslforeach);
$xslforeach->setAttribute("select", "companies/company");
$xsltemplate = $stylesheet->createElementNS($xslns, "xsl:call-template");
$xsltemplate->setAttribute("name", "anchor_tags");
$xslforeach->appendChild($xsltemplate);
/* Pass the paramters, which are pulled from the company child nodes, to the anchor_tags template */
$param = $xsltemplate->appendChild($stylesheet->createElementNS($xslns, "xsl:with-param"));
$param->setAttribute("name", "url");
$xslvalue = $param->appendChild($stylesheet->createElementNS($xslns, "xsl:value-of"));
$xslvalue->setAttribute("select", "url");
$param = $xsltemplate->appendChild($stylesheet->createElementNS($xslns, "xsl:with-param"));
$param->setAttribute("name", "company_name");
$xslvalue = $param->appendChild($stylesheet->createElementNS($xslns, "xsl:value-of"));
$xslvalue->setAttribute("select", "name");
$param = $xsltemplate->appendChild($stylesheet->createElementNS($xslns, "xsl:with-param"));
$param->setAttribute("name", "target");
$xslvalue = $param->appendChild($stylesheet->createElementNS($xslns, "xsl:value-of"));
$xslvalue->setAttribute("select", "target");
/******************************************************************
END MAIN TEMPLATE
******************************************************************/
/******************************************************************
END BUILDING STYLESHEET
******************************************************************/
/* Loading an xml document from a string */
$dom = domDocument::loadXML('
List of Links
unknown
_new
International PHP Magazine
http://www.php-mag.net
unknown
_new
PHP
http://www.php.net
_top
unknown
_new
Libxml
http://www.xmlsoft.org
_new
unknown
_new
W3C DOM Level 3 Specifications
www.w3.org/TR/DOM-Level-3-Core/
_new
');
/* Create a new xslt processor and import the stylesheet we created */
$proc = new xsltprocessor();
$proc->importStylesheet($stylesheet);
/* Output the transformation */
print "Transformed Data:\n";
print $proc->transformToXML($dom);