Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Dynamiczne wyświetlanie xml'a
Forum PHP.pl > Forum > XML, AJAX > XML
tmk
Witam
otrzymuję iformacje w xml'u, chciałbym je prezentować na stroniew formie HTMLowej. Czy najlepiej to robić w php korzystając z parsujących funkcji wbudowanych czy może poprostu załączać css'a lub xslt?

a przy okazji, znacie jakieś porządne materiały dotyczące XSLT? i moze ogólnie prezentowania xmla w htmlowych znacznikach

z góry dzięki
bigZbig
Mozesz dolaczyc css albo xsl wprost - nowoczesne przegladarki powinny sobie z tym poradzic bezposrednio, ale lepszym rozwiazaniem jest dokonanie przeksztalcen w php i przesylanie do klienta wyniku w formacie html. Mozesz do tego uzyc ponizszej clasy ktora zwraca w wyniku kod html.

  1. <?php
  2. class bigXslt {
  3.  
  4. var $xsltproc;
  5.  
  6. function bigXslt () {
  7. if (PHP_VERSION >= 5) {
  8. $this->xsltproc = new XsltProcessor();
  9. } else {
  10. $this->xsltproc = xslt_create();
  11. }
  12. }
  13.  
  14. function createHtml ($xml, $xsl) {
  15.  
  16. $xml = (is_file($xml)) ? file_get_contents($xml) : $xml;
  17. $xsl = (is_file($xsl)) ? file_get_contents($xsl) : $xsl;
  18.  
  19. if ( (empty($xml) || !is_string($xml)) || (empty($xsl) || !is_string($xsl)) ) {
  20. return false;
  21. } 
  22.  
  23. $arguments = array(
  24. '/_xml' => $xml,
  25. '/_xsl' =>$xsl
  26. );
  27.  
  28. $html = $this->_xsltProcess ('arg:/_xml', 'arg:/_xsl', null, $arguments);
  29.  
  30. $this->_xsltFree();
  31. return $html; 
  32. }
  33.  
  34.  function _xsltProcess($xml_arg,$xsl_arg,$xslcontainer = null,$args = null,$params = null) {
  35. // Start with preparing the arguments
  36. $xml_arg = str_replace('arg:', '', $xml_arg);
  37. $xsl_arg = str_replace('arg:', '', $xsl_arg);
  38.  
  39. // Create instances of the DomDocument class
  40. $xml = new DomDocument;
  41. $xsl = new DomDocument;
  42.  
  43. // Load the xml document and the xsl template
  44. $xml->loadXML($args[$xml_arg]);
  45. $xsl->loadXML($args[$xsl_arg]);
  46.  
  47. // Load the xsl template
  48. $this->xsltproc->importStyleSheet($xsl);
  49.  
  50. // Set parameters when defined
  51. if ($params) {
  52. foreach ($params as $param => $value) {
  53. $xsltproc->setParameter(&#092;"\", $param, $value);
  54. }
  55. }
  56.  
  57. // Start the transformation
  58. $processed = $this->xsltproc->transformToXML($xml);
  59.  
  60. // Put the result in a file when specified
  61. if ($xslcontainer) {
  62. return @file_put_contents($xslcontainer, $processed);
  63. } else {
  64. return $processed;
  65. }
  66. }
  67.  
  68. function _xsltFree() {
  69. unset($this->xsltproc);
  70. }
  71. }
  72.  
  73. $parser = new bigXslt ();
  74. $html = $parser->createHtml (&#092;"file.xml\", \"file.xsl\");
  75. ?>
enceladus
Zgadzam się, że najlepiej przekształcić do HTML-a po stronie serwera, ale nie wiem czy php jest najwydajniejszym rozwiązaniem. Istnieją metody realizujące to na poziomie samego serwera WWW. Np przez:
Modxslt: http://www.mod-xslt2.com/
AxKit: http://www.axkit.org/
mod_publisher: http://apache.webthing.com/mod_publisher/
i wiele innych rozwiązań ....
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.