XSL error handling in PHP5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • al3x4nder@gmail.com

    #1

    XSL error handling in PHP5

    I`m need hanling XSLT errors in my script,
    before I`m use Sablotron, that has nice
    interface for it:
    ------------8<------------------
    function ProduceXHTML($x ml, $xsl){
    $xh = xslt_create();
    xslt_set_encodi ng ($xh, CODE_PAGE);
    $arguments = array('/_xml' => $xml, '/_xsl' => $xsl);
    $result = @xslt_process($ xh, 'arg:/_xml', 'arg:/_xsl', NULL,
    $arguments);
    $this->error = (int)xslt_errno ($xh);
    if($this->error){
    $errorMessage = sprintf("Cannot process XSLT document
    [code %d]: %s",
    xslt_errno($xh) , xslt_error($xh) );
    $this->AddErrorMessag e(5, $errorMessage);
    }

    xslt_free($xh);
    }

    ------------8<------------------

    but now I`m ported my project
    in PHP5 and use XSLlib like this:

    ------------8<------------------
    function ProduceXHTML($x sltemplate){
    $xh = new XSLTProcessor() ;
    $xml = new DOMDocument();
    $xsl = new DOMDocument();

    $xsl->loadXML($thi s->LoadXSLT(TEMPL ATES_ROOT.$xslt emplate));
    $xml->loadXML($thi s->xml);

    $xh->importStyleShe et($xsl);
    $xhtml=$xh->transformToXML ($xml);

    if($this->IsDebug()){
    header('Content-type: text/xml');
    die($this->xml);
    }
    if ($xhtml) {
    $xhtml = str_replace("&a mp;", "&", $xhtml);
    echo $xhtml;
    } else {
    /* commented, because PHPv5 used
    printf("Cannot process XSLT document [code %d]: %s",
    xslt_errno($xh) , xslt_error($xh) );
    */
    return false; // invalid XSL
    }
    unset($xh);

    }
    ------------8<------------------
    anybody know how I can track errors now?

    thanks and sorry for my English

  • Tony Marston

    #2
    Re: XSL error handling in PHP5

    I solved this problem years ago:

    1) Have a standard error handler which can be invoked using trigger_error() ;
    I call mine 'errorHandler'.

    2) Create an alternative error handler for XML/XSL processing in PHP 5. I
    call mine 'XML_errorHandl er'

    function XML_errorHandle r ($errno, $errstr, $errfile, $errline,
    $errcontext)
    // deal with errors from XML or XSL functions.
    {
    // pass these details to the standard error handler
    errorHandler (E_USER_ERROR, $errstr, $errfile, $errline,
    $errcontext);
    } // XML_errorHandle r

    3) Just before performing and XML/XSL processing switch to the alternative
    error handler:

    set_error_handl er('XML_errorHa ndler');

    4) Don't forget to switch back again afterwards:

    set_error_handl er('errorHandle r');

    All this code can be downloaded from my website in my sample application at


    --
    Tony Marston
    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




    <al3x4nder@gmai l.com> wrote in message
    news:1145378276 .288512.274260@ v46g2000cwv.goo glegroups.com.. .[color=blue]
    > I`m need hanling XSLT errors in my script,
    > before I`m use Sablotron, that has nice
    > interface for it:
    > ------------8<------------------
    > function ProduceXHTML($x ml, $xsl){
    > $xh = xslt_create();
    > xslt_set_encodi ng ($xh, CODE_PAGE);
    > $arguments = array('/_xml' => $xml, '/_xsl' => $xsl);
    > $result = @xslt_process($ xh, 'arg:/_xml', 'arg:/_xsl', NULL,
    > $arguments);
    > $this->error = (int)xslt_errno ($xh);
    > if($this->error){
    > $errorMessage = sprintf("Cannot process XSLT document
    > [code %d]: %s",
    > xslt_errno($xh) , xslt_error($xh) );
    > $this->AddErrorMessag e(5, $errorMessage);
    > }
    >
    > xslt_free($xh);
    > }
    >
    > ------------8<------------------
    >
    > but now I`m ported my project
    > in PHP5 and use XSLlib like this:
    >
    > ------------8<------------------
    > function ProduceXHTML($x sltemplate){
    > $xh = new XSLTProcessor() ;
    > $xml = new DOMDocument();
    > $xsl = new DOMDocument();
    >
    > $xsl->loadXML($thi s->LoadXSLT(TEMPL ATES_ROOT.$xslt emplate));
    > $xml->loadXML($thi s->xml);
    >
    > $xh->importStyleShe et($xsl);
    > $xhtml=$xh->transformToXML ($xml);
    >
    > if($this->IsDebug()){
    > header('Content-type: text/xml');
    > die($this->xml);
    > }
    > if ($xhtml) {
    > $xhtml = str_replace("&a mp;", "&", $xhtml);
    > echo $xhtml;
    > } else {
    > /* commented, because PHPv5 used
    > printf("Cannot process XSLT document [code %d]: %s",
    > xslt_errno($xh) , xslt_error($xh) );
    > */
    > return false; // invalid XSL
    > }
    > unset($xh);
    >
    > }
    > ------------8<------------------
    > anybody know how I can track errors now?
    >
    > thanks and sorry for my English
    >[/color]


    Comment

    • al3x4nder@gmail.com

      #3
      Re: XSL error handling in PHP5


      Tony Marston wrote:[color=blue]
      > I solved this problem years ago:
      >
      > 1) Have a standard error handler which can be invoked using trigger_error() ;
      > I call mine 'errorHandler'.
      >
      > 2) Create an alternative error handler for XML/XSL processing in PHP 5. I
      > call mine 'XML_errorHandl er'
      >
      > function XML_errorHandle r ($errno, $errstr, $errfile, $errline,
      > $errcontext)
      > // deal with errors from XML or XSL functions.
      > {
      > // pass these details to the standard error handler
      > errorHandler (E_USER_ERROR, $errstr, $errfile, $errline,
      > $errcontext);
      > } // XML_errorHandle r
      >
      > 3) Just before performing and XML/XSL processing switch to the alternative
      > error handler:
      >
      > set_error_handl er('XML_errorHa ndler');
      >
      > 4) Don't forget to switch back again afterwards:
      >
      > set_error_handl er('errorHandle r');
      >
      > All this code can be downloaded from my website in my sample application at
      > http://www.tonymarston.net/php-mysql...plication.html
      >
      > --
      > Tony Marston
      > http://www.tonymarston.net
      > http://www.radicore.org[/color]

      thank you very match!
      it`s cool way!
      I`m tested it and find it very nice!

      Comment

      Working...