<?xml version="1.0" encoding="UTF-8"?>
<xao-docs>
  <site name="XaoDocs" product="Xao" version="1.0.0-dev" homepage="https://xao.ndx.au" repo="https://github.com/ndx-au/Xao" examples-repo="https://github.com/ndx-au/xao-example-apps"/>
  <request slug="database-aggregation" theme="atlas" path="/xml/database-aggregation" xml-url="/xml/database-aggregation"/>
  <themes>
    <theme id="atlas" name="Atlas" tagline="Three-column reference desk with persistent sidebar and sticky page outline" audience="API and framework developers browsing across sections" structure="3-column grid: left navigation sidebar, center article, right on-this-page outline" css="/assets/atlas.css" active="true"/>
    <theme id="folio" name="Folio" tagline="Editorial journal spread with XPath-grouped numbered chapters and collapsible library" audience="Architects and evaluators reading long-form conceptual chapters" structure="Editorial cover header, collapsible library drawer, and sibling-grouped numbered chapter sections" css="/assets/folio.css" active="false"/>
    <theme id="console" name="Console" tagline="Operations dashboard with telemetry strip and collapsible H2 module accordions" audience="SREs, operators, and runtime engineers scanning sections quickly" structure="Command header, telemetry bar, compact nav rail, and expandable &lt;details&gt; module accordions" css="/assets/console.css" active="false"/>
    <theme id="reader" name="Reader (A11y)" tagline="WCAG AAA single-column linear reader with upfront summary, numbered TOC, and zero JS" audience="Screen reader users, keyboard-only navigation, and low-vision / cognitive accessibility" structure="Multi-landmark skip links, upfront document summary, inline numbered TOC, figure-wrapped code blocks, directory after article, zero JS" css="/assets/reader.css" active="false"/>
    <theme id="handbook" name="Handbook" tagline="Single-column print-ready technical specification sheet with metadata ledger" audience="Technical reviewers, auditors, and print/PDF export workflows" structure="Single-column specification document with &lt;dl&gt; metadata ledger, numbered outline, and print stylesheet" css="/assets/handbook.css" active="false"/>
  </themes>
  <navigation>
    <section name="Start Here">
      <item slug="index" title="Xao Documentation" description="XML Application Objects for PHP 8 and PHC — zero-dependency XML content aggregation and XSLT 3.0 presentation." order="1" active="false"/>
      <item slug="getting-started" title="Getting Started" description="Set up PHC, autoload Xao with zero Composer packages, transform your first DomDoc with XSLT 3.0, and run phc serve." order="2" active="false"/>
    </section>
    <section name="Core Concepts">
      <item slug="architecture" title="Architecture &amp; XML Aggregation" description="How Xao builds a unified XML conglomeration document from databases, Markdown, and domain objects before delegating presentation to XSLT 3.0." order="3" active="false"/>
      <item slug="xslt-themes" title="XSLT 3.0 Themes vs CSS Skins" description="Why XSLT themes are structurally and semantically richer than CSS-only themes — including accessibility-first DOM transformations." order="4" active="false"/>
    </section>
    <section name="Framework Reference">
      <item slug="core-api" title="Core API Reference" description="Reference for Xao\\Runtime, Xao\\XaoRoot, Xao\\DomDoc, Xao\\DomFactory, Xao\\AppDoc, Xao\\Http\\Response, and Xao\\Exceptions." order="5" active="false"/>
      <item slug="database-aggregation" title="Database Aggregation (DbToXml)" description="Convert PDO queries, PDOStatement results, and PHP iterables directly into structured DomDoc XML fragments under PHC." order="6" active="true"/>
      <item slug="markdown-aggregation" title="Markdown Aggregation" description="Convert CommonMark and GFM into CommonMark XML 1.0 natively in PHC via comrak and graft it into Xao DomDoc trees." order="7" active="false"/>
      <item slug="xslt-drivers" title="XSLT 3.0 Drivers" description="Pluggable XsltDriverInterface strategies: PhcNativeXsltDriver (default in-process), SaxonHttpDriver (HTTP/distributed), and ClientSideXslDriver." order="8" active="false"/>
    </section>
    <section name="Operations &amp; Runtime">
      <item slug="phc-runtime" title="PHC Runtime &amp; Compatibility" description="Running Xao on PHC in isolate and worker modes, sandbox defaults, worker-safe autoloading, and P00–P05 compatibility findings." order="9" active="false"/>
      <item slug="security" title="Security Model" description="XXE rejection, realpath containment, strict slug allowlists, CSRF guards, gated debug output, and PHC sandbox enforcement." order="10" active="false"/>
    </section>
    <section name="Examples &amp; Design">
      <item slug="reference-apps" title="Reference Applications" description="Comparing Xao's built-in SQLite PDO catalog reference app with XaoDocs, the flat-file Markdown multi-theme CMS." order="11" active="false"/>
      <item slug="adr-0001-phc-xee-xslt" title="ADR 0001 &amp; Roadmap" description="Architecture Decision Record for pure-Rust xee-xslt in PHC plus the Xao v1.0.0 milestone and phase roadmap (M0–M3, P00–P06)." order="12" active="false"/>
    </section>
  </navigation>
  <page slug="database-aggregation" title="Database Aggregation (DbToXml)" description="Convert PDO queries, PDOStatement results, and PHP iterables directly into structured DomDoc XML fragments under PHC." section="Framework Reference" audience="Backend developers integrating SQLite or SQL databases" status="stable" words="164" reading-minutes="1" source-file="content/database-aggregation.md">
    <tags>
      <tag>pdo</tag>
      <tag>sqlite</tag>
      <tag>dbtoxml</tag>
      <tag>aggregation</tag>
    </tags>
    <neighbors>
      <prev slug="core-api" title="Core API Reference"/>
      <next slug="markdown-aggregation" title="Markdown Aggregation"/>
    </neighbors>
    <body>
      <document xmlns="http://commonmark.org/xml/1.0">
        <heading level="1">
          <text xml:space="preserve">Database Aggregation (</text>
          <code xml:space="preserve">DbToXml</code>
          <text xml:space="preserve">)</text>
        </heading>
        <paragraph>
          <code xml:space="preserve">Xao\DbToXml</code>
          <text xml:space="preserve"> replaces Xao's legacy 2003 PEAR </text>
          <code xml:space="preserve">DB</code>
          <text xml:space="preserve"> </text>
          <code xml:space="preserve">ResultFetcher</code>
          <text xml:space="preserve"> with modern </text>
          <code xml:space="preserve">PDO</code>
          <text xml:space="preserve"> / </text>
          <code xml:space="preserve">PDOStatement</code>
          <text xml:space="preserve"> and PHP iterable serialization, producing clean </text>
          <code xml:space="preserve">DomDoc</code>
          <text xml:space="preserve"> XML trees ready to graft into an application document.</text>
        </paragraph>
        <heading level="2">
          <text xml:space="preserve">API Methods</text>
        </heading>
        <paragraph>
          <text xml:space="preserve">Source: </text>
          <code xml:space="preserve">src/DbToXml.php</code>
          <text xml:space="preserve"> (extends </text>
          <code xml:space="preserve">XaoRoot</code>
          <text xml:space="preserve">)</text>
        </paragraph>
        <list type="bullet" tight="true">
          <item>
            <paragraph>
              <code xml:space="preserve">fromQuery(PDO $pdo, string $sql, array $params = [], string $rootTag = 'result', string $rowTag = 'row'): DomDoc</code>
            </paragraph>
          </item>
          <item>
            <paragraph>
              <code xml:space="preserve">fromStatement(PDOStatement $statement, string $rootTag = 'result', string $rowTag = 'row'): DomDoc</code>
            </paragraph>
          </item>
          <item>
            <paragraph>
              <code xml:space="preserve">fromIterable(iterable $rows, string $rootTag = 'result', string $rowTag = 'row'): DomDoc</code>
            </paragraph>
          </item>
        </list>
        <heading level="2">
          <text xml:space="preserve">Example: Grafting SQLite Rows Into a Page Document</text>
        </heading>
        <code_block info="php" xml:space="preserve">&lt;?php

declare(strict_types=1);

use Xao\DbToXml;
use Xao\DomDoc;

$pdo = new PDO('sqlite:' . __DIR__ . '/data/catalog.sqlite');
$pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db = new DbToXml();
$itemsDoc = $db-&gt;fromQuery(
    $pdo,
    'SELECT slug, title, category, summary FROM items WHERE category = :cat ORDER BY title',
    [':cat' =&gt; 'core'],
    'items',
    'item'
);

$page = new DomDoc('catalog');
$page-&gt;ndConsumeDoc($itemsDoc);
echo $page-&gt;toXml(true);
</code_block>
        <heading level="2">
          <text xml:space="preserve">Error Handling &amp; Parity</text>
        </heading>
        <paragraph>
          <text xml:space="preserve">Invalid SQL queries, connection errors, or invalid XML tag names throw </text>
          <code xml:space="preserve">Xao\Exceptions\DbToXmlException</code>
          <text xml:space="preserve"> (or record structured error state on </text>
          <code xml:space="preserve">XaoRoot</code>
          <text xml:space="preserve">) rather than crashing silently or exposing raw database exceptions to HTTP clients.</text>
        </paragraph>
        <heading level="2">
          <text xml:space="preserve">PHC Sandbox Requirements</text>
        </heading>
        <paragraph>
          <text xml:space="preserve">When running file-backed SQLite databases under </text>
          <code xml:space="preserve">phc serve</code>
          <text xml:space="preserve">, configure </text>
          <code xml:space="preserve">phc.toml</code>
          <text xml:space="preserve">:</text>
        </paragraph>
        <code_block info="toml" xml:space="preserve">[sandbox]
allowed_roots = ["public", "src", "stylesheets", "data"]
allow_file_databases = true
</code_block>
        <paragraph>
          <text xml:space="preserve">Both </text>
          <code xml:space="preserve">allow_file_databases = true</code>
          <text xml:space="preserve"> and listing the database directory in </text>
          <code xml:space="preserve">allowed_roots</code>
          <text xml:space="preserve"> are required by PHC's deny-by-default sandbox.</text>
        </paragraph>
      </document>
    </body>
  </page>
</xao-docs>
