Search

I’m pulling in some text from an RSS feed that contains escaped HTML (e.g., <em> is &lt;em&gt;.

I’m trying to use the HTML Truncate Advanced utility to truncate this text to a 500 characters (give or take) teaser. However, occasionally it produces some very bizarre results. In the case I’m struggling with now, it outputs unescaped HTML (which is fine by me), but the output is broken.

I’ve tried creating an xpathr gist with the node in question, but xpathr itself didn’t want to process it. On my local server, this is what I see in the HTML source:

I read math books for fun. I realize that, right away, this puts me in an unusual portion of the population. It’s not <em>just</em> my fancy math degree that makes these books attractive. However, I do think that there are some math books written for people interested in math (whether professionally or amateurly), and then there are math books written for people who, usually thanks to a bad experience in school, have sworn off math like they said they would swear off cheap booze. <em>Our Days Are…

Can I pre-process this node before I send it to the utility, or can I tweak the truncate utility to handle this somehow?

The XSLT utility expects an XML node to be passed. Your DS returns escaped data, thus it is treated as a very long string, hence your result. Somehow, you must return unescaped content in your datasource. Some custom PHP should do it.

sigh I was afraid you might say that. :D

I replaced the call to include in my datasource with this monster:

$classParams = get_object_vars($this);
foreach($classParams as $key => $value) {
    $dynamicXML->$key = $value;
}

$result = $dynamicXML->execute($param_pool);

$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadXML($result->generate());

$reviews = $doc->documentElement->getElementsByTagName('user_review');
foreach($reviews as $r) {
    $f = $doc->createDocumentFragment();
    $f->appendXML('<user_review>' . str_replace('&', '&#038;', $r->nodeValue) . '</user_review>');
    $r->parentNode->replaceChild($f, $r); 
}

return $doc->saveXML($doc->documentElement);

It’s ugly. But it works. Thanks for pointing me in the right direction.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details