Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: konwersja XML -> HTML poprzez XSL
Forum PHP.pl > Forum > XML, AJAX > XML
morrison
jesli macie jakies doswiadczenie w tym temacie to bede wdzieczny za podpowiedzi. Zabieram sie za kolejny modul aplikacji i jeszcze nie do konca zapoznalem sie z tym, dlatego juz teraz chcialbym spytac o ewentualne sugestie. Mam okreslone pliki XML, ktore musza zostac zamienione w html. Jesli macie jakies ciekawe rozwiazania lub koncepcje to chetnie poznam.

Ponadto musze poruszyc temat edycji plikow xsl z poziomu aplikacji i mozliwosc modyfikowania tych plikow poprzez np przyciski, list rozwijane. Poza tym przy edytowaniu plikow xml musze udostepnic userowi mozliwosc sortowania wyswietlanych danych np wedlug zawartosci znacznika: <name>ktos</name>. Jak proponujecie to zrobic?

pozdr winksmiley.jpg
wassago
Wysztko zrobisz dzięki XSLT - pełno tutoriali na google jest. Powodzenia.
morrison
dzięki. cos znalazlem, ale to jeszcze nie to czego oczekuję. a czy moze mi ktos napisac teoretycznie jaka jest zasada wykonywania takich konwersji? Poniewaz mam kilka roznych rodzajow plikow na ktorych bede dokonywal konwersji danych. Przy konwersji z XML do HTML konieczny jest plik arkusza XSL? do czego on ma sluzyc i jak to dziala?
bieniek
Arkusz XSL zawiera definicje wszystkich przekształceń np:
  1. <?xml version="1.0" encoding="ISO-8859-2"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  3.  
  4. <xsl:template match="text()"><xsl:value-of select="."/></xsl:template>
  5. <xsl:template match="/">
  6. <html><head><title><xsl:value-of select="dokument/opis"/></title></head>
  7. <body>
  8. <basefont face="Verdana"/>
  9. <xsl:apply-templates/>
  10. </body></html>
  11. </xsl:template>
  12. </xsl:stylesheet>
morrison
Mam taki kod, ktory ma wykonac konwersję:
  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('/_xml' => $xml,'/_xsl' =>$xsl);
  24.  
  25.     $html = $this->_xsltProcess ('arg:/_xml', 'arg:/_xsl', null, $arguments);
  26.  
  27.     $this->_xsltFree();
  28.     return $html;
  29.   }
  30.  
  31.    function _xsltProcess($xml_arg,$xsl_arg,$xslcontainer = null,$args = null,$params = null) {
  32.     // Start with preparing the arguments
  33.     $xml_arg = str_replace('arg:', '', $xml_arg);
  34.     $xsl_arg = str_replace('arg:', '', $xsl_arg);
  35.  
  36.     // Create instances of the DomDocument class
  37.     $xml = new DomDocument;
  38.     $xsl = new DomDocument;
  39.  
  40.     // Load the xml document and the xsl template
  41.     $xml->loadXML($args[$xml_arg]);
  42.     $xsl->loadXML($args[$xsl_arg]);
  43.  
  44.     // Load the xsl template
  45.     $this->xsltproc->importStyleSheet($xsl);
  46.  
  47.     // Set parameters when defined
  48.     if ($params) {
  49.       foreach ($params as $param => $value) {
  50.         $xsltproc->setParameter("", $param, $value);
  51.       }
  52.     }
  53.  
  54.     // Start the transformation
  55.     $processed = $this->xsltproc->transformToXML($xml);
  56.  
  57.     // Put the result in a file when specified
  58.     if ($xslcontainer) {
  59.       return @file_put_contents($xslcontainer, $processed);
  60.     } else {
  61.       return $processed;
  62.     }
  63.   }
  64.  
  65.   function _xsltFree() {
  66.     unset($this->xsltproc);
  67.   }
  68. }
  69.  
  70. $parser = new bigXslt ();
  71. $html = $parser->createHtml ("file.xml", "file.xsl");
  72.  
  73. echo $html;
  74. ?>


Jednakze wyrzuca bledy:
Kod
Warning: DOMDocument::loadXML() [function.loadXML]: XML declaration allowed only at the start of the document in Entity, line: 2 in /home/pawel/public_html/konw.php on line 41

Warning: Invalid or inclomplete context in /home/pawel/public_html/konw.php on line 55

W czym jest problem?
bela
Coś Twój dokument XML nie podoba się smile.gif Pokaż go.
morrison
wiec na poczatku mialem testową wersję:
  1. <?xml version="1.0" encoding="ISO-8859-2"?>
  2. <unpublished>
  3. <author>23</author>
  4. <title>432</title>
  5. <note>234</note>
  6. <month>234</month>
  7. <year>2434</year>
  8. </unpublished>

ale teraz dalem plik docelowy bo wczesniej tez mi ten nie dzialal:
  1. <?xml version="1.0" encoding="ISO-8859-2"?>
  2. <entries xmlns:bibtex="http://bibtexml.sourceforge.net">
  3. <bibtex:entry bibtex:id="7/2004">
  4. <bibtex:unpublished>
  5. <bibtex:author>23</bibtex:author>
  6. <bibtex:title>432</bibtex:title>
  7. <bibtex:note>234</bibtex:note>
  8. <bibtex:month>234</bibtex:month>
  9. <bibtex:year>2434</bibtex:year>
  10. </bibtex:unpublished>
  11. </bibtex:entry>
  12. <bibtex:entry bibtex:id="20/2004">
  13. <bibtex:mastersthesis>
  14. <bibtex:author>44</bibtex:author>
  15. <bibtex:title>44</bibtex:title>
  16. <bibtex:school>44</bibtex:school>
  17. </bibtex:mastersthesis>
  18. </bibtex:entry>
  19. <bibtex:entry bibtex:id="21/2004">
  20. <bibtex:inproceedings>
  21. <bibtex:author>df</bibtex:author>
  22. <bibtex:title>df</bibtex:title>
  23. <bibtex:editor>df</bibtex:editor>
  24. </bibtex:inproceedings>
  25. </bibtex:entry>
  26. </entries>


wiec teraz z tym drugim plikiem juz nie ma bledow, wyswietla sie czysta strona. Ale na koncu pliku php mam deklaracje: "echo $html;" wiec czy nie powinienem uzyskad kodu htmla?
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.