130 likes | 237 Views
XSLT Streaming Terminology Understanding “Climbing”. Roger L. Costello January 29, 2014. Question: Where is the XSLT processor positioned when at a context node?.
E N D
XSLT Streaming TerminologyUnderstanding “Climbing” Roger L. Costello January 29, 2014
Question: Where is the XSLT processor positioned when at a context node? • Suppose that an XSLT processor is at this context node N (that is, the current streaming position is the <N> element): <N x= " … " xmlns= " … " > … </N>Exactly where is the XSLT processor positioned? The answer to this question is important because it tells us what the XSLT processor has visited when it is at a “context node”. • Is the XSLT processor positioned here (after the name of the start tag: <N x= " … " xmlns= " … "> … </N> • Is the XSLT processor positioned here (after the attribute):<N x= " … " xmlns= " … "> … </N> • Or is the XSLT processor positioned here (after the ns declaration):<N x= " … " xmlns= " … "> … </N>
Answer: the XSLT processor is at the “>” • The XSLT processor is positioned at the start tag’s end angle bracket: <N x= " … " xmlns= " … "> … </N> • So what? So the XSLT processor has visited the element, its attributes, and its namespace declarations. Obviously it has not visited its content. This is a key concept!
XSLT processor remembers ancestors and self as it streams the input • Suppose that an XSLT processor is at a context node N (that is, the current streaming position is the <N> element). As the XSLT processor streamed through the input, it remembers all the ancestor nodes it visited, finally arriving at context node N. A B C The XSLT processor remembers all the ancestors of N: E, B, and A, including their attributes and namespaces. The preceding slide explained that the XSLT processor has also visited the attributes and namespace declarations of N. D E context node N F This is a key concept!
What does the XSLT processor remember? • The preceding slide said:The XSLT processor remembers all the ancestors of N: E, B, and A, including their attributes and namespaces. • Question: Exactly what does the XSLT processor remember? • Answer: It remembers the name of the elements, the name of the attributes, the values of the attributes, and the namespaces that are visible at each node. This is a key concept!
Climbing • A construct is climbing if, when executed, it yields the context node (self) or ancestors of the context node. Also a climbing construct can return attributes of the context node or attributes of ancestor nodes. Finally, a climbing construct can return namespaces that are visible on the context node or on any ancestor.
Why is it called “climbing”? • The term “climbing” comes from the notion that a climbing construct is one that climbs up the XML tree.
Climbing examples • Let's suppose that an XSLT program is stream-processing this XML document: <Book xmlns="http://www.book.org"><Intro>A book about ...</Intro> <Chapter id="abc"> <Title xml:lang="EN">Hello World</Title> </Chapter> </Book> • Suppose that the context node is the <Title> element (that is, the current streaming position is the <Title> element). • This XPath expression: @*returns all the attributes of the context node;@* is a climbing construct. • This XPath expression: ancestor::*returns all the ancestors of the context node;ancestor::* is a climbing construct.
Climbing examples (cont.) • This XPath expression: ../@*returns all the attributes of the parent of the context node;../@* is a climbing construct. • This XPath expression: ./namespace::*returns all the namespaces visible on the context node;./namespace::* is a climbing construct.
Operating on the results of a climbing construct • What can be done with the nodes returned by a climbing construct? • We must be careful because the XSLT processor remembers only a limited amount of information about the ancestor nodes. • We can apply the name function to the parent: name(..) • We can obtain the name of each ancestor: for $i in ancestor::* return name($i) • We can obtain the value of each attribute of the context node:for $i in @::* return data($i)
Can’t apply the string() function to an ancestor element • This is not legal (not streamable): apply string() to the grandparent:string(../..) Executing string(../..) would result in the XSLT processor attempting to obtain all of this data. Element Book xmlns="http://www.book.org" Element Chapter Element Intro id="abc" Element Title Text A book about … context node xml:lang="EN" Text Hello World Fetching this would require the XSLT processor to back up. Backing up is not allowed in streaming.
Lesson Learned • So you write an XPath expression to climb up the XML tree. • That XPath expression is a “climbing construct.” • Good so far. • Now you operate on those nodes that your climbing construct fetched. • Be sure that the operation doesn’t result in consuming the contents of the children of those nodes. Key concept: you are allowed to ascend but not allowed to descend afterwards.
Can apply the string() function to an ancestor element’s attribute • It is legal to apply the string() function to an ancestor element’s attributes. For example, this is legal: string(../@id) Element Book xmlns="http://www.book.org" Executing string(../@id) would result in the XSLT processor attempting to obtain all of this data. That’s okay because it is data that the XSLT processor remembers on its journey to the context node. Element Chapter Element Intro id="abc" Element Title Text A book about … xml:lang="EN" context node Text Hello World