Monthly Archives: January 2005

Bypassing URL file-access is disabled

For some odd reason this host has disabled URL file-access.
So i needed something simple to bypass this problem:

function fetch_url($url)
{
    if (preg_match("#^http://(.*?)/(.*)$#", $url, $matches))
    {
        $host = $matches[1];
        $uri = $matches[2];
        $msg = "GET /$uri HTTP/1.0\r\nHost: $host\r\n\r\n";

        $fp = fsockopen($host, 80, $errno, $errstr, 10);
        fwrite($fp, $msg);
        $ignore = true;
        while (!feof($fp))
        {
            $read = fgets($fp, 1024);
            if (!$ignore)
            {
                $contents .= $read;
            }
            if (preg_match("#^Content-Type: .*?\r\n$#", $read))
            {
                $ignore = false;
            }
        }
        fclose($fp);
        return $contents;
    }
}

Playing with XML and XSL

// add stuff to an xml document in php4
$doc = domxml_open_mem($xml);
$root = $doc->document_element();
$inner = $doc->create_element('inner');
$root = $root->append_child($inner);

// add stuff to an xml document in php5
$doc = new DomDocument('1.0', 'UTF-8');
$doc->loadXML($xml);
$root = $doc->getelementsByTagName('resultset')->item(0);
$inner = $doc->createElement('inner');
$root = $root->appendChild($inner);

XHTML does not allow to have an empty list, <ul></ul>. Therefore we need to test first if there are any nodes we want to put in that list. The code to do this looks like:

<xsl:for-each select="//resultset/entity">
  <div class="mainitem">
  <div class="maintitle"><xsl:value-of select="title"/></div>
  <div class="maincontent">
    <xsl:if test="count(items/item) &gt; 0">
      <ul>
        <xsl:for-each select="items/item">
        <li><a href="{link}"><xsl:value-of select="title"/></a></li>
        </xsl:for-each>
      </ul>
    </xsl:if>
  </div>
  </div>
</xsl:for-each>

XSLT annoyances

Today i’ve finally made the switch. My code generates XML and then i translate it to XHTML with XSLT. However, if i write

<textarea name="foo"></textarea>

it will be translated to:

<textarea name="foo"/>.

A not so good workaround is to write: (Notice the space in the xsl:text)

<textarea name="foo"><xsl:text> </xsl:text></textarea>

UPDATE on 2005-01-20 05:42
The solution is to use html as output method instead of xml.

<xsl:output method='html'/>

UPDATE on 2005-01-21 02:15
You may also want to make sure HTML tags do not get transformed:

<xsl:value-of select="attribute[@name='content']" disable-output-escaping="yes"/>