首页 > 解决方案 > Change the xslt file of an xml file using php

问题描述

hi every one i want to change dynamically the style sheet of my XML file using PHP some thing like this: the XML file register.xml
when i try to display it it shows me: the out put

OK here is what i want to do :
1) As i mentioned before i have an XML file named "regiter.xml"
2) Let say that i have 3 files with the extension .xsl ( style1.xsl,style2.xsl, style3.xsl )
3) And i have a sort of form tag that give the user the choice of how he want to display the XML file, he can choose style1 or 2 or 3.
4) I want to change the value of the attribute href="file.xsl" in the tag <?xml-stylesheet ?> of the file "register.xml" dynamically depending on the user choice.

my question is:
it is possible to do so?
if not can you suggest me another way to do this?
thank you...

标签: phpxmlxslt

解决方案


考虑让 PHP 运行 XSLT 脚本而不修改浏览器的处理指令以呈现样式表。

// LOAD XML SOURCE
$doc = new DOMDocument();
$doc->load('Input.xml');

// CONDITIONALLY SELECT XSLT SCRIPT
if (if(isset($_POST['button_1']))) {
    $selected_xsl = "/path/to/style1.xsl";

} elseif (if(isset($_POST['button_2']))) {
    $selected_xsl = "/path/to/style2.xsl";

} elseif (if(isset($_POST['button_3']))) {
    $selected_xsl = "/path/to/style3.xsl";

}   
$xsl = new DOMDocument();
$xsl->load($selected_xsl);

// INITIALIZE AND RUN TRANSFORMER
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 
$newXML = $proc->transformToXML($doc);

// OUTPUT NEW XML TO SCREEN
header("Content-type: text/xml");
echo $newXML;

推荐阅读