Подход к реализации XQuery Паращенко Олег Санкт-Петербургский Государственный Университет Обнинск, 2004
XML Hello Hello, World ! article / ”“!” “World”
… … Простой XPath Похож на путь к файлу /usr/bin/xmllint
Простой XPath Результат — множество узлов /AAA/CCC
// Поиск по всему поддереву //DDD/BBB
* Любое имя /AAA/*/*/BBB
@ Атрибут
Оси навигации child::ename descendant::enam e parent::node() attribute::aname self::node() ancestor following-sibling preceding-sibling … ename
Фильтрация (/AAA/BBB)[1](/AAA/BBB)[last()]
Фильтрация, функции //bin[vim] //bin/*[contains(name(), 'vi')]
Запросы не для XPath Обратные ссылки /... /xxx//... /yyy//... [..?xxx..?yyy...] Создание элементов, переименование
Пример XQuery Here is a query. doc("books.xml")//book[1]/title Here is the result of the above query. { doc("books.xml")//book[1]/title } Here is a query. doc("books.xml")//book[1]/title Here is the result of the above query. TCP/IP Illustrated
FLWOR (“flower”) expressions for—let—where—order by—return for $i in (1, 2, 3) return { $i } 1 2 3
define function toc($book-or-section as element()) as element()* { for $section in $book-or-section/section return { $section/title, toc($section) } } { for $s in doc("xquery-book.xml")/book return toc($s) } Функции
Ресурсы Спецификации А.Валиков “Технология XSLT” “XQuery from the Experts: A Guide to the W3C XML Query Language” by Howard Katz, D. D. Chamberlin
Подход к реализации XQuery Паращенко Олег Санкт-Петербургский Государственный Университет Обнинск, 2004 (продолжение)
XPath vs “вручную” get_elem("elemname[i]") vs /** * Returns child element at given position. Position can be * negative. In this case nodes are counting from end. * Last node position is '-1'. node parent node name name of node, null if not important pos position of node starting from zero, can be negative */ public static Element getChildElement (Element node, String name, int s) { // // Update position to be always positive. // boolean fromEnd = pos < 0; if (fromEnd) { pos = -(pos + 1); } // // Initialize // Node cur_node = fromEnd ? node.getLastChild(): node.getFirstChild(); int cur_pos = -1; // // Walk on children // for (; cur_node != null; cur_node = (fromEnd ? cur_node.getPreviousSibling() : cur_node.getNextSibling ())) { // // Check that current node is of type 'element' // if (cur_node.getNodeType() != Node.ELEMENT_NODE) { continue; } // // Get element, check its name and position // Element cur_elem = (Element)cur_node; if ((name != null) && (! name.equals (cur_elem.getTagName()))) { continue; } cur_pos++; if (cur_pos == pos) { return cur_elem; } // // If node was found, it was returned from inside loop // return null; }
XPath vs “вручную” наглядность меньше кода – меньше ошибок и дополнительных расходов менее 1 минуты vs более 2 часов
Программы с деревьями find текстовые процессоры компиляторы, интерпретаторы встроенный xpath/xquery — “серебрянная пуля”
Единая реализация Библиотеки C vs Java vs.NET vs … Виртуальная машина ● Реализация XQuery на удобном языке программирования ● Байт-код ВМ ● Интерпретатор ВМ
Готовые реализации ● CL-XML ● SXML/SXPath/SXQuery Удобный язык ● Common Lisp или Scheme
SXML Hello Hello, World ! (*TOP* (article (id "hw")) (title "Hello") (para "Hello, " (object "World") "!"))) S-exp: XML:
SXPath ((txpath document) ((sxpath '(// TAF TRange *text*)) document) ((node-join (node-closure (node-typeof? 'TAF)) (select-kids (node-typeof? 'VALID)) (select-kids (node-typeof? (select-kids (node-typeof? 'TRange)) (select-kids (node-typeof? '*text*))) document)
Части системы SXQuery XQuery VM макрораскрытие, компиляция В программе: Интерпретатор ВМ Привязка (bindings) Парсер XML, XPath, XQuery
Этапы развития Сейчас – проверка идеи v.0.1: Python AST v.1.0: Docbook XSLT
Похожие системы Частично – любая реализация XPath/XQuery XSLTC от Oracle
Оптимизация (//ename)[1]
ВМ как исходный код e1 := _X(doc, “AAA//BBB/CCC”) e2 := _X(doc, “AAA//BBB/DDD”) t1 := child(doc, “AAA”) t2 := descendant(t1, “BBB”) e1 := child(t2, “CCC”) t3 := child(doc, “AAA”) t4 := descendant(t3, “BBB”) e2 := child(t4, “DDD”) t1 := child(doc, “AAA”) t2 := descendant(t1, “BBB”) e1 := child(t2, “CCC”) e2 := child(t2, “DDD”)
Другие ВМ Java VM.NET VM LLVM
Подход к реализации XQuery Паращенко Олег Санкт-Петербургский Государственный Университет Обнинск, 2004