Download presentation
Presentation is loading. Please wait.
1
A note on generating text with the xsl:value-of instruction
2
Number of nodes in the node-list chosen by the select attribute of the xsl:value-of instruction Often, there is only one node in the node-list chosen by the select attribute, for example: I found a woman. Her name is:. The text generated is the string value of the chosen node
3
Number of nodes in the node-list chosen by the select attribute of the xsl:value-of instruction Often, there is only one node in the node-list chosen by the select attribute, for example: I found a woman. Her name is:. The text generated is the string value of the chosen node –That is, the concatenation of the strings in the text nodes in the sub-tree of the chosen node
4
Number of nodes in the node-list chosen by the select attribute of the xsl:value-of instruction Often, there is only one node in the node-list chosen by the select attribute, for example: I found a woman. Her name is:. The text generated is the string value of the chosen node –That is, the concatenation of the strings in the text nodes in the sub-tree of the chosen node There is only one such text node, so only its text is output
5
Number of nodes in the node-list chosen by the select attribute of the xsl:value-of instruction Often, there is only one node in the node-list chosen by the select attribute, for example: I found a woman. Her name is:. The text generated is the string value of the chosen node –That is, the concatenation of the strings in the text nodes in the sub-tree of the chosen node There is only one such text node, so only its text is output
6
Sometimes, there may multiple text nodes in the sub-tree of a single chosen node In this example, there is still only one node in the list chosen by the select attribute –but there are two text nodes in the sub-tree of this single chosen node I found some people. Their names are:. So, the text generated is the concatenation of the strings in these two text nodes
7
Sometimes, there may multiple text nodes in the sub-tree of a single chosen node In this example, there is still only one node in the list chosen by the select attribute –but there are two text nodes in the sub-tree of this single chosen node I found some people. Their names are:. So, the text generated is the concatenation of the strings in these two text nodes
8
But, sometimes, there can be more than one node in the list chosen by the select attribute There are two nodes in the list selected here: I found some people. Their names are The selected node-list contains two person nodes
9
But, sometimes, there can be more than one node in the list chosen by the select attribute There are two nodes in the list selected here: I found some people. Their names are The selected node-list contains two person nodes But only one person’s name is output.
10
But, sometimes, there can be more than one node in the list chosen by the select attribute There are two nodes in the list selected here: I found some people. Their names are The selected node-list contains two person nodes But only one person’s name is output. Why?
11
Answer When a xsl:value-of instruction converts a selected node list to a text node for the result tree, it only uses only the string value of the first node in the selected list
12
Current node versus Context node
13
People often confuse these two terms This is understandable because, –in many situations the context node is the same as the current node But they are not always the same So, let's look carefully
14
The current node When the XSLT processor instantiates a template for some node in the document tree, then, –during the execution of the instantiation, –that node is the current node for the entire execution of the instantiation
15
The current node When this stylesheet is applied to this XML document, the person template is instantiated twice, once for each person node that is a descendent of the root node –During the execution of Instantiation 1, the person node whose subtree contains Celia Larkin is the current node –During the execution of Instantiation 2, the person node whose subtree contains Bertie Ahern is the current node
16
In XPath, the function current() returns the current node During the execution of Instantiation 1, the first person node is returned by current() and the xsl:value-of instruction computes the string value of this node, which is the text in its sole descendant text-node During the execution of Instantiation 2, the second person node is returned by current() and the xsl:value-of instruction computes the string value of this node, which is the text in its sole descendant text-node
17
Do. and current() mean the same thing? No.. returns the context node current() returns the current node When an XPath expression is evaluated, –the current node remains fixed –but the context node changes
18
At the start of evaluating an XPath expression, the context node is the same as the current node The two stylesheets above are equivalent has the same value as because, at the start of evaluating an XPath expression, the context node is the same as the current node But the context node can change during the evaluation of a complex XPath expression –this often happens when we use predicates within an XPath expression
19
Context node changing during XPath evaluation In this element, the XPath expression uses. twice –The first time, it refers to the root node –The second time, it refers to a person node which is being considered for inclusion in the node-set that will be returned by the XPath expression
20
Context node changing during XPath evaluation while current node is unchanged In this element, the XPath expression uses current() once and uses. twice –current() refers to the current node, which is a nation node –The first time,. refers to the current node, which is a nation node –The second time,. refers to a person node which is being considered for inclusion in the node-set being counted by the XPath expression
21
for-each loops and current() The function current() has two different meanings in a for-each loop which is iterating through a set of nodes: –in the open-tag for the loop, current() refers to the current node in the enclosing XSL element –between the tags for the loop, current() refers to the member of the selected node-set which is the subject of the current iteration
22
for-each loop and current() Consider the two usages of current() in this template: The name of this white-haired person is
23
for-each loop and current() The usage of current() in the open-tag of the for-each loop refers to the current node in the surrounding template, that is, it refers to the root node The name of this white-haired person is
24
for-each loop and current() The usage of current() inside the for-each loop refers to the member of the selected set of person nodes which is the subject of the current iteration through the loop The name of this white-haired person is
25
current() and nested for-each loops There are five usages of current() in this template In the nation,, have typical hair colour
26
current() and nested for-each loops The 1st usage of current() refers to the current node for the overall template In the nation,, have typical hair colour
27
current() and nested for-each loops The 2nd, 3rd and 4th usages of current() refer to the nation in the current iteration of the outer for-each loop In the nation,, have typical hair colour
28
current() and nested for-each loops The 5th usage of current() refers to the person in the current iteration of the inner for-each loop In the nation,, have typical hair colour
29
current() and sorting in for-each loops This template has two sort elements In the nation,, have typical hair colour
30
current() and sorting in for-each loops In the 1st sort element, current() refers to a nation in the node-set being sorted In the nation,, have typical hair colour
31
current() and sorting in for-each loops In the 2nd sort element, current() refers to a person in the node-set being sorted In the nation,, have typical hair colour
32
Using current() for cross- referenced sorting
33
An XML document with 2 cross-referenced tables This XML document contains two lists –a list of languages –a list of countries Each country has an attribute which uses an ID number to cross-refer to a language
34
Sorting the countries by name
35
Sorting the countries by name of language
36
Sorting by name of language, then by name of country
37
Using current() only when we really must
38
Using. only when we really must
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.