Querying and Storing XML Week 2 XPath & XQuery January 22-25, 2013

  • View
    216

  • Download
    1

Embed Size (px)

Text of Querying and Storing XML Week 2 XPath & XQuery January 22-25, 2013

  • Querying and Storing XMLWeek 2XPath & XQueryJanuary 22-25, 2013

    January 22-25, 2013QSX

    XPathA language of path expressionsLoosely related to file pathsroot (/)sequential composition (p/q)wildcards (*)axis steps (child, parent, descendant, etc.)also: filters, text nodes, label testsplus positional & string functionsUsed for navigationcomponent of XSLT, XQuery, etc.

    January 22-25, 2013QSX

    Context node (starting point)

    January 22-25, 2013QSX

    Child

    January 22-25, 2013QSX

    Descendant

    January 22-25, 2013QSX

    Descendant-or-self

    January 22-25, 2013QSX

    Parent

    January 22-25, 2013QSX

    Ancestor

    January 22-25, 2013QSX

    Ancestor-or-self

    January 22-25, 2013QSX

    Following-sibling

    January 22-25, 2013QSX

    Preceding-sibling

    January 22-25, 2013QSX

    Self

    January 22-25, 2013QSX

    Following

    January 22-25, 2013QSX

    Preceding

    January 22-25, 2013QSX

    Partitionselfancestorfollowingprecedingdescendant

    January 22-25, 2013QSX

    Syntaxaxis ::=

    test ::= * | text() | node() | a | b | @a | ...p ::= ax::tst[q] | p/pq ::= p | q and q | q or q | not(q) | ...ap ::= /pchild | descendant | descendant-or-self | parent | ancestor | ancestor-or-self | preceding-sibling | following-sibling | self | preceding | following

    January 22-25, 2013QSX

    Abbreviations/a = child::a//a = descendant-or-self::*/child::a. = self::*.. = parent::*Starting path with / means start from document rootthis is a special node above the root element

    January 22-25, 2013QSX

    Sequential compositionrbabaabbccdedddeabababbc/child::*/child::*doc

    January 22-25, 2013QSX

    Node label testsrbabaabbccdedddeabababbc/r/a//bdoc

    January 22-25, 2013QSX

    Node label testsrbabaabbccdedddeabababbc/r/a//*doc

    January 22-25, 2013QSX

    Sequential compositionrbabaabbccdedddeabababbc//a/following-sibling::cdoc

    January 22-25, 2013QSX

    Sequential compositionrbabaabbccdedddeabababbc//a/following-sibling::b//*doc

    January 22-25, 2013QSX

    Filtersrbabaabbccdedddeabababbc//a[b]doc

    January 22-25, 2013QSX

    Filtersrbabaabbccdedddeabababbc//a[b]/cdoc

    January 22-25, 2013QSX

    Filtersrbabaabbccdedddeabababbc//a[following-sibling::c]doc

    January 22-25, 2013QSX

    Positional testsrbabaabbccdedddeabababbc//*[position()=2] (or just //a[2])doc

    January 22-25, 2013QSX

    Positional testsrbabaabbccdedddeabababbc//a/*[first()]doc

    January 22-25, 2013QSX

    Positional testsrbabaabbccdedddeabababbc//a/*[last()]doc

    January 22-25, 2013QSX

    Quiz

    rbabaabbccdedddeabababbcdoc1. Write XPath to select red nodes.2. Without using child.3. Or filters.

    January 22-25, 2013QSX

    Attributes & Textrabbcatt = fooabbcatt = barabcdefgh1234//a/@att1234

    January 22-25, 2013QSX

    Attributes & Textrabbcatt = fooabbcatt = barabcdefgh1234//a/b/text()1234

    January 22-25, 2013QSX

    Equalityrabbcatt = fooabbcatt = barabcdefgh1234//a[@att=bar]//*/text()1234

    January 22-25, 2013QSX

    Equalityrabbcatt = fooabbcatt = barabcdefgh1234//a[@att=bar]1234

    January 22-25, 2013QSX

    Equalityrabbcatt = fooabbcatt = barabcdefgh1234//a[@att=bar]//*Child, Descendant do not select attributes!1234

    January 22-25, 2013QSX

    Equalityrabbcatt = fooabbcatt = barabcdefgh1234//a[@att=bar]//text()1234

    January 22-25, 2013QSX

    Equality quizrabbcatt = fooabbcatt = barabcdefgh1234/r[a/b/text() = a/b/c/text()]1234r is selected!Equality of sequences evaluates to true if there is any common value in the two sequences

    January 22-25, 2013QSX

    Tree patternsA graphical notation for (downward) XPath queries/filtersabcdDescendantSelected nodea[b]//c/d

    January 22-25, 2013QSX

    Tree pattern matchingA function h: P -> T such that:Child edges map to edgesDescendant edges map to pathsabcdaabcdedddeabab

    January 22-25, 2013QSX

    Semantics of XPathRepresent tree as T = (V,E,,