60 likes | 130 Views
Designing Streamable XPath Expressions. Roger L. Costello January 5, 2014. Acknowledgement. The example shown in the following slides comes from the XSLT 3.0 specification. Count the <head> elements.
E N D
Designing Streamable XPath Expressions Roger L. CostelloJanuary 5, 2014
Acknowledgement • The example shown in the following slides comes from the XSLT 3.0 specification.
Count the <head> elements • Suppose an XML document contains a bunch of <section> elements. Every <section> element has a child <head> element. You are to create an XPath expression to count the <head> elements that are within the <section> elements. • A <section> element may contain a child <section> element (in addition to its child <head> element). • Here is a sample XML document: <section> <section> <head>…</head> </section> <head>…</head> </section> count(xpath) should return 2. • You are to design the XPath so that it can be used in a streaming program: you are to design a streamable XPath expression!
Not a streamable XPath expression • This XPath is not streamable: count(//section/head) • Let’s see why. • Consider our sample XML document: <section> <section> <head>…</head> </section> <head>…</head> </section> This is the first section element that is fetched. Its head element is down here To fetch the second section element the XSLT processor would have to back up, which is not allowed in streaming. Therefore, this XPath expression is not streamable: //section/head
Streamable XPath expression • A rule of streaming is that you can always access ancestors. • So, to make the XPath expression viable for streaming, find each <head> element that has a parent <section> element: count(/descendant::head[parent::section]) <section> <section> <head>…</head> </section> <head>…</head> </section> This is the first headelement that is fetched. It has a parent sectionelement. Therefore, this XPath expression is streamable: /descendant::head[parent::section]
Issues in streaming • A streaming processor always moves forward, it can never back up. So if your program contains an XPath expression which would require the processor to back up, your program is not streamable. • As a streaming processor descends the input document’s XML tree, it keeps a record of nodes it has visited. Thus, your program can reference ancestors.