Preview:
$conn = pg_connect("host=localhost dbname=mi_base user=usuario password=clave");

$sql = "SELECT
  xmlelement(
    name libros,
    xmlagg(
      xmlelement(
        name libro,
        xmlforest(
          id,
          titulo,
          autor,
          anio
        )
      )
    )
  ) AS libros_xml
FROM libros";

$result = pg_query($conn, $sql);
$row = pg_fetch_assoc($result);

$xmlStr = $row['libros_xml'];
$xml = simplexml_load_string($xmlStr);
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter