13
XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

Embed Size (px)

Citation preview

Page 1: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

XSLT Streaming TerminologyUnderstanding “Climbing”

Roger L. CostelloJanuary 29, 2014

Page 2: XSLT Streaming Terminology Understanding “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>

Page 3: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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!

Page 4: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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

D E

N

F

context node

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.

This is a key concept!

Page 5: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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!

Page 6: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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.

Page 7: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

Why is it called “climbing”?

The term “climbing” comes from the notion that a climbing construct is one that climbs up the XML tree.

Page 8: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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.

Page 9: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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.

Page 10: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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)

Page 11: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

Can’t apply the string() function to an ancestor element

This is not legal (not streamable): apply string() to the grandparent:string(../..)

ElementBook

TextHello World

ElementIntro

ElementChapter

ElementTitle xml:lang="EN"

TextA book about

id="abc"

xmlns="http://www.book.org"

context node

Fetching this would require the XSLT processor to back up. Backing up is not allowed in streaming.

Executing string(../..) would result in the XSLT processor attempting to obtain all of this data.

Page 12: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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.

Page 13: XSLT Streaming Terminology Understanding “Climbing” Roger L. Costello January 29, 2014

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)

ElementBook

TextHello World

ElementIntro

ElementChapter

ElementTitle xml:lang="EN"

TextA book about

id="abc"

xmlns="http://www.book.org"

context node

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.