We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27889
    • 415 Posts
    For user which use non utf-8 file (iso-latin) there is an error :
    Error: domxml_open_mem() [function.domxml-open-mem]: Input is not proper UTF-8, indicate encoding ! Bytes: 0xE9 0x65 0x6E 0x6E

    I have put :
    $xmlDocString=utf8_encode($xmlDocString);
    at line 65 just before $xmlDoc = domxml_open_mem($xmlDocString); and it solve the problem.
      MODx Sites & Prestations: http://dp-site.fr [Last MODx Site]
      MODx Repository: [HOME] [MetaTagsExtra] / Current Dev: [xFDM]
      • 21056
      • 327 Posts
      Hi Michal

      I’ve made a few changes and improvements to v 0.2, posted below, specifically adding some error handling and ability to get XSL from a modx document.

      Would you like to add this to the repository?

      <?php
      
      /*
         Snippet Name: xslt
         Short Desc: Transform a remote XML feed using a local XSL file
         Created By: Michal Charemza
         Modified By: Nick Crossland
         Last modified: 25/06/2007
         Version: 0.3
         License: GPL http://www.gnu.org/copyleft/gpl.html
      
         Example call: [!xslt? &xmlURL=`http://www.domain.com/feed.rss` &xslFile=`[(filemanager_path)]/assets/templates/mytemplate/testStyle.xsl` &xslParams='someExampleParameter:3,otherParemeter:yes'!]
      
         Parameters:
          &xmlURL:    URL of remote XML file to be transformed. If this contains ? or & or = characters, it will need to be URL encoded
          &xslFile:   Location of XSL file on the webserver. Can also be a ModX doc ID.
          &xslParams: Comma separated list of parameterName:parameterValue pairs
                      that are given to the XSL processor
      	&error_msg:	Message to be returned if the XML feed is unavailable
      	&log_errors: boolean - log any errors retrieving or parsing the XML into ModX's event log
      			
      */
      
       /***************************************************************************
      *                  Preferences
      ****************************************************************************/
      
      $error_msg = isset($error_msg)?$error_msg:'The feed is temporarily unavailable.';
      $log_errors = isset($log_errors)?$log_errors:true;
      
       /***************************************************************************
      *                  Check which options specified in snippet call
      ****************************************************************************/
      
      $xslParamsSpecified = isset($xslParams);
      
      
      /***************************************************************************
      *                Process XSL Parameters from Snippet Call if given
      ****************************************************************************/
      
      // Fill paramArray with variable name => value pairs
      $paramArray = array();
      if ($xslParamsSpecified) {
          $keyValuePairs = explode(',', $xslParams);
          $numParams = count($keyValuePairs);
          for ($i =  0; $i <$numParams; $i += 1) {
              $pair = explode(':', $keyValuePairs[$i]);
              $pairArray = array($pair[0] => $pair[1]);
              $paramArray = array_merge($paramArray, $pairArray);
          }
      }
      
      
      /***************************************************************************
      *                          Retrieve remote XML File
      ****************************************************************************/
      // If the URL has been encoded
      $xmlURL = urldecode($xmlURL); 
      
      // Create curl session
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $xmlURL);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      
      // Return xml string
      $xmlDocString = curl_exec ($ch);
      
      // Close curl session
      curl_close($ch);
      
      
      // Fix for some cases when an rss feed doesn't have a namespace (causes XML parser to fail)
      $xmlDocString = str_replace('<rss version="2.0">', '<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">', $xmlDocString);
      
      
      // Create domDocument object from XML string
      $error = '';
      if (!$xmlDoc = @domxml_open_mem($xmlDocString,DOMXML_LOAD_PARSING,&$error)) {
      
      	// Log the error in ModX's event log, if we have chosen to
      	if ($log_errors) {
      		$error_text = print_r($error, true);
      		$error_message = "
      		Error text:
      		$error_text
      		
      		XML doc contents:
      		$xmlDocString 
      		";
      		$modx->logEvent(0, 3, $error_message, 'XSL snippet parsing error');
      	}
      
       	// Return a friendly error message to the user
      	return $error_msg;
      }
      
      
      /***************************************************************************
      *                           Get local XSL File
      ****************************************************************************/
      
      // Has a modx doc ID been supplied?
      if (is_numeric($xslFile)) {
      	$doc_content = $modx->getDocument(intval($xslFile), 'content', 1);
      	$xslDoc = domxml_xslt_stylesheet ($doc_content['content']); 
      } else {
      	// Create domDocument object from XSL file
      	$xslDoc  = domxml_xslt_stylesheet_file ($xslFile);
      }
      
      
      /***************************************************************************
      *              Translate XML using XSL and given parameters
      ****************************************************************************/
      
      $translated = $xslDoc->process($xmlDoc, $paramArray);
      
      
      /***************************************************************************
      *                                  Output
      ****************************************************************************/
      
      // Output translated XML to string
      $output = $translated->dump_mem();
      
      // Remove XML prologue
      $output = strstr($output,"\n");
      $output = substr($output, 1);
      
      return $output;
      ?>
        Author: ManagerManager plugin - customise your ModX manager interface

        Rckt - web development, Sheffield, UK
        • 401
        • 2 Posts
        For some reason I can’t get this snippet to work. I am new to programming and Modx, so I could be missing something obvious. This is the error message that I get:

        PHP error debug
        Error: domxml_xslt_stylesheet_file() [function.domxml-xslt-stylesheet-file]: I/O
        Error type/ Nr.: Warning - 2
        File: /home/[site]/public_html/manager/includes/document.parser.class.inc.php(748) : eval()’d code
        Line: 69

        when I use this snippet call:

        [[xslt? &xmlURL=`http://www.nytimes.com/services/xml/rss/nyt/Technology.xml`
        &xslFile=`[filepath]/assets/snippets/xsl/listTitleLinks.xsl`]]

        I am using Modx 0.9.5 and have tried a number of different snippet calls using [! at the beginning and end, using a different xsl file, etc. I thought I had the wrong address for xsl file, but I don’t think that is it either. Any ideas?

        Thanks for the help.
          • 21056
          • 327 Posts
          [filepath]/assets/snippets/xsl/listTitleLinks.xsl

          Do you actually have filepath in square brackets, or have you just swapped this in for posting?
            Author: ManagerManager plugin - customise your ModX manager interface

            Rckt - web development, Sheffield, UK
            • 401
            • 2 Posts
            Quote from: ncrossland at Aug 20, 2007, 09:18 AM

            [filepath]/assets/snippets/xsl/listTitleLinks.xsl

            Do you actually have filepath in square brackets, or have you just swapped this in for posting?

            I just swapped the [filepath] in for posting. I had someone else look at the actual filepath I used to make sure I hadn’t made any mistakes.

            Thanks for the response.
              • 14540
              • 9 Posts
              No matter what I try, I don’t seem able to get this snippet to work.

              [!xslt? &xmlURL=`http://tata.nl/xml/news.xml` &xslFile=`[(filemanager_path)]gn/simple.xml` &xslParams=``!]

              This call resides in the content part of a page, with a template.

              For some reason it displays nothing, not even the empty template.

              Thanks for any help.

              btw I use modx 0.96, could that be the problem?
                • 30269
                • 119 Posts
                It works fine with 096; the path might be the issue, I used:
                &xslFile=`assets/xsl/nhpr_style_v01.xsl` 
                and it did the job for me wink
                  • 21106
                  • 53 Posts
                  Hi guys.

                  I’m new to CMSs ModX PHP and stuff..

                  When I try to save this snippet, I get this error.

                  Not Acceptable
                  An appropriate representation of the requested resource /manager/index.php could not be found on this server.


                  I can save other snippets... Just this one is giving me that error.. made some search and this issue has something to do with mod_security (similar to the issue experienced with TinyMCE..

                  Any ideas? (I just want to know why...hehehe.)

                  Thanks in advance.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Some code in that snippet isn’t making it past the mod_security on your server. It scans all POST input and rejects what it’s been configured to reject. The errors this produces vary wildly from server to server.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 21106
                      • 53 Posts
                      Quote from: sottwell at Mar 15, 2008, 06:03 AM

                      Some code in that snippet isn’t making it past the mod_security on your server. It scans all POST input and rejects what it’s been configured to reject. The errors this produces vary wildly from server to server.

                      Thanks for the info. That’s better. So it’s the webhost.
                      Thanks again.