310 likes | 348 Views
Learn the basics of XML querying with XPath syntax and examples. Understand location paths, predicates, expressions, and core function libraries. Enhance your skills in extracting nodes and filtering node-sets.
E N D
Summary of Introduction to XML • HTML vs. XML • Types of Data • Basics of XML • XML Syntax, XML Elements, XML Attributes • XML Document Type Definition - Types, Elements, Attributes, Example • XML Schema - XSD vs. DTD, Simple elements, Complex elements, Attributes, Facets, Indicators
XML & Databases • Extracting XML Documents from Relational Databases • XML Querying: • XPath • XQuery
XML QUERYING • XML PATH LANGUAGE - XPath • XML QUERY LANGUAGE - XQuery
XML PATH LANGUAGE - XPATH • XPath is a language for addressing parts of an XML Document. • XPath uses paths to define XML elements • XPath defines a library of standard functions • XPath is not written using XML Syntax • XPath is a W3C Standard !! • XPath provides common syntax and semantics for XSLT and XPointer
XPath – XML Data model • root nodes • element nodes • text nodes • attribute nodes • namespace nodes • processing instruction nodes • comment nodes
XPath Basics - Path • In general -> sandeep/xml/xmlpath.ppt • In XPath -> /catalog/cd/price
XPath Syntax with examples (overview) • Locating nodes: /catalog/cd/price (absolute path), //cd • Unknown elements: Use wildcard (*) /catalog/*, /*/*/price , // * • Several paths: //title | // artist • Selecting attributes: Use prefix (@) //@country , /catalog/cd[@country = ‘UK’] , //cd[@*] • Specifying Predicates: /catalog/cd[1] , //cd[price], //cd[price = 10]/price
XPath – Location Path • Most important grammatical construct in the XPath language, that results in a node-set. • EBNF format: Location ::= RelativeLocationPath | AbsoluteLocationPath • AbsoluteLocationPath: /step/step/... • RelativeLocationPath: step/step/...
XPath – Location Path • EBNF format: Location ::= RelativeLocationPath | AbsoluteLocationPath AbsoluteLocationPath::=‘/’RelativeLocationPath? | AbbreviatedAbsoluteLocationPath RelativeLocationPath::=Step|RelativeLocationPath ‘/’ Step | AbbreviatedRelativeLocationPath
XPath – Location Path • Location Step has three parts: • An Axis – Tree Relationship • A Node Test – Node type, expanded-name • Zero or more Predicates – Expressions to refine set of nodes. • Syntax: axisname :: nodetest [predicate] • Example: child :: price [price = 10] • Context: context node, context position, context size, variable bindings, function library, namespace declarations
XPath – Location Path - Examples • child::cd , child::*, child::text() • attribute::name,attribute::* • descentant::catalog, descendant-or-self::cd • child::catalog/self::cd, child::cd[position() = last()] • /descendant::cd, /descendant::catalog/child::cd • child::para[position()=5][attribute::type=“warning”]………what if .. [att..][pos..] • child::*[self::cd or self::catalog]
XPath – Predicates • A Predicate filters a node-set to a new node-set. • The condition is placed in [ ]
XPath – Abbreviated Syntax • Examples: cd, *, text(), @src, @*, cd[1], cd[last()], */cd, //cd, cd[@type and @country], .., ., cd[5][@type=“classic”], ../@src, .//cd cd[@type=“classic”][5]
XPath - Expressions Types: • Numerical -> + ,- ,* ,div , mod • Equality -> =, != • Relational -> <,>,<=,>= • Boolean -> or, and • XPath – Functions Node – set functions, Numeric fuctions, String functions, Boolean functions
XPath – Core Function Library • Node – Set: count() ; setnumber=count(node-set) id() ; node-set=id(value) last() ; listnumber=last() local-name() ; namestring=local-name(node) name() ; nodestring=name(node) namespace-uri() ; nodeuri=namespace-uri(node) position() ; processednumber=position() • Boolean: boolean() ; bool = boolean(value) false() ; number(false()) lang() ; bool = lang(language) not() ; not(false()) true() ; number(true())
XPath – Core Function Library – String • concat() ; string=concat(val1, val2, ..) • contains() ; bool=contains(val,substr) • normalize-space() ; string=normalize-space(string) • starts-with() ; bool=starts-with(string,substr) • string() ; string(value) • string-length() ; number=string-length(string) • substring() ; string=substring(string,start,length) • substring-after() ; string=substring-after(string,substr) • substring-before() ; string= substring-before(string,substr) • translate() ; string=translate(value,string1,string2)
XML QUERY LANGUAGE • XML Query Language is used to extract data by querying the XML Documents • XML is built on XPath which is used in writing queries • XML Query and XML Path have same Data Model, Function Library, Data types, Operators • XML Query is not yet a W3C Standard !
XML Query Language – Extracting Nodes • Using Functions doc(example.xml) • Using Functions and Paths doc(example.xml)/catalog/cd or doc(..)//cd • Using Expressions doc(example.xml)/catalog/cd[price < 10]
XML Query – FLOWR Expression • For <variable bindings to individual nodes> • Let <variable bindings to a collection of nodes> • Order by <query specifications> • Where <conditions> • Return <query result specifications>
REFERENCES • Fundamentals of Database Systems, Fourth Edition, Elmasir and Navathe • www.w3schools.com • www.w3.org