php - 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...
解决方案
考虑让 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;
推荐阅读
- php - 使用 phpmailer 库在 PHP 中 smtp connect() 失败
- julia - 如何在 Julia 中更改级别
- react-native - TypeError:当我在 createDrawerNavigator 中使用自己的组件作为 contentComponent 时,无法读取未定义的属性“默认”
- vue.js - 使用缩放插件在 chart.js 中获取当前缩放信息
- python - 执行我的自定义最大似然代码时出现意外行为。它跳转到未调用的函数
- vba - VBA - 如果将某些数字传递给另一个子,则溢出错误
- authentication - 如何在httpUnit中设置认证参数?
- java - oauth 2.0 没有弹簧启动
- postgresql - 在 Postgresql 中将数据复制到视图中的方法
- scala - 通过 Spark-Shell 运行时获取分区日志