RDDL(Resource Directory Description Language)是一種基於 XML 的語言,用於描述命名空間 URI 下可用的資源(如 Schema、樣式表、文檔、代碼庫)。它讓機器能自動發現與某個 XML 命名空間相關的所有資源。
<rddl:resource xmlns:rddl="https://www.rddl.org/"
xlink:href="https://example.com/schema.xsd"
xlink:role="https://www.w3.org/2001/XMLSchema"
xlink:title="Example Schema" />
由於 RDDL 文檔通常較小,推薦使用 SimpleXML + XPath 快速提取資源信息。
<?php
function parseRDDL($url) {
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace('rddl', 'https://www.rddl.org/');
$xml->registerXPathNamespace('xlink', 'https://www.w3.org/1999/xlink');
$resources = [];
foreach ($xml->xpath('//rddl:resource') as $res) {
$href = (string)$res['href'];
$role = (string)$res['role'];
$title = (string)$res['title'];
$resources[] = compact('href', 'role', 'title');
}
return $resources;
}
// 使用示例
$resources = parseRDDL('https://example.com/ns/myns.rddl');
foreach ($resources as $r) {
echo "[$r[role]] $r[title] -> $r[href]\n";
}
?>
| Role URI | 含義 |
|---|---|
| https://www.w3.org/2001/XMLSchema | XML Schema 定義 |
| https://www.w3.org/1999/XSL/Transform | XSLT 樣式表 |
| https://www.rddl.org/#CSS | CSS 樣式表 |
| https://www.rddl.org/#Java | Java 類庫/代碼 |
| https://www.rddl.org/#Python | Python 模塊 |
libxml_use_internal_errors(true))。掌握 RDDL 解析,能讓你的 PHP 應用在 XML 生態中實現更智能的資源自動發現與集成。