Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL.

Similar presentations


Presentation on theme: "XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL."— Presentation transcript:

1 XSLT

2 XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL specifies the styling of an XML document by using: –XSLT to describe how the document is transformed into another XML document –The transformed XML document uses the formatting vocabulary

3 XSLT X’formations A transformation in the XSLT language is expressed as a well-formed XML document A transformation may include both: –Elements that are defined by XSLT elements XSLT-defined elements belong to XSLT Namespace –Elements that are not defined by XSLT

4 XSLT X’formations A style sheet contains a set of template rules A template rule has two parts: –Pattern which is matched against nodes in the source tree (uses XPath here) –Template which can be instantiated to form part of the result tree Style sheets can be applied to wide class of documents that have similar source tree structures

5 XSLT X’formation Step 1 XSLT transformations describe rules for transforming source tree into a result tree Transformation is achieved by associating patterns with templates Result tree is constructed by finding template rule for root node, instantiating its template

6 XSLT X’formation Step 2 Root Node template: –Can contain elements that specify literal result element structure –Can also contain elements from the XSLT namespace that are instructions for creating result tree fragments

7 XSLT X’formation Step 2 Instructions can select and process descendant source elements Processing a descendant element creates a result tree fragment by finding the applicable template rule and instantiating its template Elements are only processed when they have been selected by the execution of an instruction

8 XSLT X’formation Step 3 A template is instantiated to create part of the result tree A template is always instantiated with respect to a current node and a current node list –Only a few instructions change the current node list or the current node When a template is instantiated, each instruction is executed and replaced by the result tree fragment that it creates The result tree is separate from the source tree

9 XSLT X’formation Step 3 The structure of the result tree can be completely different from the structure of the source tree

10 XSLT X’formation Step 3 In constructing the result tree, elements from the source tree can be filtered and reordered, and arbitrary structure can be added

11 XSLT Example Where the result tree structure is independent of source tree structure, a style sheet can often consist of only a single template

12 XSLT Example – Input Data 10 9 7 4 3 4

13 XSLT Example – Style Sheet http://www.w3.org/1999/XSL/Transform Sample

14 XSLT Example – Output Sample North South

15 XSLT Extensibility XSLT provides two "hooks" for extending the language: –Extending the set of instruction elements used in templates –Extending the set of functions used in XPath expressions

16 XSLT Structure

17 XSLT Name Space XSLT namespace has the URI http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/XSL/Transform Prefix of xsl: is used for referring to elements in the XSLT namespace Any extension of additional elements, attributes must be in a separate namespace

18 XSLT Style Sheet Element Stylesheet is represented by an xsl:stylesheet element in an XML document Must have a version attribute, indicating the version of XSLT An element occurring as child of an xsl:stylesheet element is called a top-level element

19 Literal Result Element Simplified syntax for style sheets consisting of only single template for root node Such style sheets may consist of just a literal result element

20 XSLT Example – Style Sheet <xsl:stylesheet version="1.0“ xmlns:xsl=http://www.w3.org/1999/XSL/Transformhttp://www.w3.org/1999/XSL/Transform xmlns="http://www.w3.org/TR/xhtml1/strict"> Expense Report Summary Total Amount:

21 XSLT Example – Simplified <html xsl:version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transformhttp://www.w3.org/1999/XSL/Transform xmlns="http://www.w3.org/TR/xhtml1/strict"> Expense Report Summary Total Amount:

22 Literal Result Element Literal result element that is the document element of a style sheet must have an xsl:version attribute Literal result element used as a style sheet cannot contain top-level elements

23 Forward Compatible Processing Forwards-compatible mode enabled –If xsl:stylesheet element version attribute is not equal to 1.0 –If xsl:stylesheet is a literal result element that has xsl:version attribute whose value is not equal to 1.0 –If xsl:stylesheet is a literal result element that does not have an xsl:version attribute and that is the document element of a stylesheet using the simplified syntax

24 Forward Compatible Processing If an element is processed in forwards- compatible mode: –If it is top-level element & no XSLT 1.0 support, then element ignored –If it is element in a template & no XSLT 1.0 support, then: if element not instantiated, no error signaled if element instantiated, the XSLT must perform fallback –if the element has attribute that XSLT 1.0 does not support, attribute must be ignored

25 Forward Compatible Processing What if style sheet depends crucially on a top- level element introduced by a version of XSL after 1.0? Stylesheet can use xsl:message element with terminate="yes" to ensure XSLT processors implementing earlier versions of XSL will not silently ignore the top-level element

26 Forward Compatible Processing <xsl:stylesheet version="1.5“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Sorry, this stylesheet requires XSLT 1.1....

27 Expressions XSLT uses the expression language defined by XPath Expressions are used in XSLT for –selecting nodes for processing –specifying conditions for different ways of processing a node –generating text to be inserted in the result tree An expression must match the XPath production Expr

28 Expressions Occur as value of certain attributes on: –XSLT-defined elements –Within curly braces in attribute value templates

29 XSLT Processing Model A list of source nodes is processed to create a result tree fragment The result tree is constructed by processing a list containing just the root node A list of source nodes is processed by appending the result tree structure created by processing each of the members of the list in order

30 XSLT Processing Model A node is processed by finding all the template rules with patterns that match the node, and choosing the best amongst them The chosen rule's template is then instantiated with the node as the current node and with the list of source nodes as the current node list A template typically contains instructions that select an additional list of source nodes for processing

31 XSLT Processing Model The process of matching, instantiation and selection is continued recursively until no new source nodes are selected for processing

32 Patterns Template rules identify the nodes to which they apply by using a pattern Specifies a set of conditions on a node A node that satisfies the conditions matches the pattern Patterns are also used for: –Numbering –Declaring keys

33 Pattern Syntax Syntax for patterns is a subset of the syntax for expressions Location paths that meet certain restrictions can be used as patterns An expression that is also a pattern always evaluates to an object of type node-set.

34 Pattern Syntax A Pattern is a set of location path patterns separated by | Location path pattern is a location path whose steps all use only the child or attribute axes Patterns may use the // operator as well as the / operator Location path patterns can also start with an id or key function call with a literal argument

35 Pattern Syntax Predicates in a pattern can use arbitrary expressions just like predicates in a location path A pattern with one or more | separated alternatives matches if any one of the alternative matches

36 Pattern Syntax A pattern that consists of a sequence of Step Patterns separated by / or // is matched from right to left –Such a pattern only matches if the rightmost StepPattern matches and a suitable element matches the rest of the pattern

37 Pattern Syntax When [] is present: –Then the first PredicateExpr in a Pattern is evaluated with the node being matched as the context node and the siblings of the context node that match the NodeTest as the context node list –If the node being matched is an attribute node, then the context node list is all the attributes that have the same parent as the attribute being matched and that match the NameTest

38 Pattern Syntax - Example appendix//ulist/item[position()=1] This matches if and only if: Current node is an item element Evaluating the PredicateExpr position()=1 with the node as context node and the siblings of the node that are item elements as the context node list yields true Current Node has a parent that matches appendix//ulist ; this will be true if the parent is a ulist element that has an appendix ancestor element

39 Patterns - Exercise What do the following patterns stand for? Para, *, chapter|appendix appendix//para text(), / node(), id("W11") *[position()=1 and self::para] para[last()=1], para[last()=1] items/item[position()>1] item[position() mod 2 = 1] @*

40 Defining Template Rules Template rule is specified with the xsl:template element match attribute is a Pattern that identifies the source node or nodes to which the rule applies match attribute is required unless the xsl:template element has a name attribute (Named Templates) Content of the xsl:template element is the template that is instantiated when the template rule is applied.

41 Template Rules - Example

42 Named Templates Templates can be invoked by name An xsl:template element with a name attribute specifies a named template If xsl:template element has name attribute, it may, but need not, also have match attribute An xsl:call-template invokes a template by name –Has required name attribute that identifies the template to be invoked

43 Applying Template Rules Processes all of the children of the current node select attribute used to process nodes selected by expression instead of processing all children –select expression must evaluate to a node-set –selected set of nodes is processed in document order unless sorting specified

44 Applying Template Rules Eg.

45 Applying Template Rules Eg. Employee belongs to group

46 Applying Template Rules Eg. Anything funny in this?

47 XSLT Exercise 1 Exercise on –Defining Template Rules –Applying Template Rules

48 Built In Template Rules To allow recursive processing to continue in the absence of a successful pattern match by an explicit template rule in the style sheet

49 Result Tree This section describes instructions that directly create nodes in the result tree

50 Literal Result Elements In a template, an element that does not belong to the XSLT namespace and that is not an extension element is instantiated to create an element node with the same name The content of the element is a template, which is instantiated to give the content of the created element node The created element node will have the attribute nodes that were present on the element node in the stylesheet tree

51 Literal Result Elements Attribute value of a literal result element is interpreted as an attribute value template Attribute value can contain expressions contained in curly braces ( {} )

52 XSLT Exercise 2 Exercise on –Literal Result Element

53 Name Space Alias Namespace URI in style sheet tree that is used to specify a namespace URI in the result tree is called a literal namespace URI xsl:namespace-alias element to declare that one namespace URI is an alias for another namespace URI stylesheet-prefix attribute specifies the namespace URI that will appear in the style sheet result-prefix attribute specifies the corresponding namespace URI that will appear in the result tree Default namespace (as declared by xmlns ) may be specified by using #default

54 Name Space Alias When literal result elements are being used to create element, attribute, or namespace nodes that use the XSLT namespace URI, the style sheet must use an alias <xsl:stylesheet version="1.0“ xmlns:xsl=http://www.w3.org/1999/XSL/Transformhttp://www.w3.org/1999/XSL/Transform xmlns:fo=http://www.w3.org/1999/XSL/Formathttp://www.w3.org/1999/XSL/Format xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias">

55 XSLT Exercise 3 Exercise on –Name Space Alias

56 Creating Elements xsl:element allows an element to be created with a computed name Name of element to be created is specified by a required name attribute, optional namespace attribute Content of xsl:element is a template for the attributes and children of the created element name, namespace attribute is interpreted as an attribute value template

57 Creating Attributes xsl:attribute element used to add attributes to result elements Attribute name is specified by a required name, optional namespace attribute Instantiating an xsl:attribute adds an attribute node to the containing result element node Content of the xsl:attribute element is a template for the value of the created attribute name, namespace attribute is interpreted as an attribute value template

58 Creating Attributes The following are all errors –Adding an attribute to an element after children have been added to it –Adding an attribute to a node that is not an element –Creating nodes other than text nodes during the instantiation of the content of the xsl:attribute element

59 XSLT Exercise 4 Exercise on –Creating elements using –Creating attributes using

60 Named Attribute Sets xsl:attribute-set defines a named set of attributes name attribute specifies the name of the attribute set xsl:attribute-set consists of zero or more xsl:attribute elements that specify the attributes in the set Attribute sets are used by specifying use-attribute-sets attribute on xsl:element, xsl:copy, xsl:attribute-set elements

61 Named Attribute Sets Value of the use-attribute-sets attribute is a whitespace-separated list of names of attribute sets Specifying use-attribute-sets attribute is equivalent to adding xsl:attribute for each of the attributes in each of the named attribute sets to the beginning of the content of the element with the use-attribute-sets attribute Attribute sets can also be used by specifying an xsl:use-attribute-sets attribute on a literal result element

62 Named Attribute Sets Attributes of attribute sets can be overridden by attributes on the literal result element Each time attribute set is used, template within each xsl:attribute element in an xsl:attribute- set element is instantiated

63 Named Attribute Sets 12pt bold

64 XSLT Exercise 5 Exercise on –Creating attribute sets using

65 Creating Text Template can also contain text nodes Each text node in a template remaining after whitespace stripping will create a text node with the same string-value in the result tree Adjacent text nodes in the result tree are automatically merged Literal data characters may also be wrapped in an xsl:text element

66 Creating PIs xsl:processing-instruction is instantiated to create a processing instruction node Instruction content of xsl:processing-instruction is template for the string-value of the processing instruction node Instruction requires name attribute to specify name of processing instruction Value of name attribute is interpreted as attribute value template

67 Creating PIs href="book.css“type="text/css“ would result in

68 Creating Comments xsl:comment element is instantiated to create a comment node in the result tree Content of xsl:comment is template for the string-value of the comment node This file is automatically generated. Do not edit! would create

69 XSLT Exercise 6 Exercise on –Creating text using –Creating comments using –Creating PI using

70 Copying xsl:copy provides an easy way of copying the current node Instantiating xsl:copy creates a copy of the current node Attributes, children of node are not automatically copied Content of xsl:copy is template for attributes, children of created node Content is instantiated only for nodes of types that can have attributes or children –Root nodes, Element nodes

71 Copying xsl:copy element may have use-attribute-sets attribute –is used only when copying element nodes When current node = root node, xsl:copy will not create root node, but will use content template

72 XSLT Exercise 7 Exercise on –Copying using Could do this exercise with exercise 21 at end

73 Computed Generated Text xsl:value-of is instantiated to create a text node in the result tree The required select attribute is an expression –Expression is evaluated, resulting object is converted to a string –The string specifies the string-value of the created text node

74 XSLT Exercise 8 Exercise on –Inserting data using

75 Attribute Value Templates Attribute value can be interpreted as an attribute value template In such an attribute value, expression can be used by surrounding it with curly braces ( {} ) Attribute value template is instantiated by replacing the expression by result of evaluating the expression and converting the resulting object to a string Curly braces are not recognized in an attribute value unless the attribute is specifically stated to be an attribute value template

76 Attribute Value Templates Source: headquarters.jpg Style Sheet: /images Result:

77 XSLT Exercise 9 Exercise on –Identifying Attribute Value Templates

78 Repetition When result has known regular structure, it is useful to be able to specify directly the template for selected nodes xsl:for-each instruction contains a template, which is instantiated for each node selected by the expression specified by the select attribute select attribute is required, expression must evaluate to a node-set

79 Repetition Template is instantiated with selected node as the current node, list of all selected nodes as the current node list The nodes are processed in document order, unless a sorting specification is present

80 Repetition...... …… Customers

81 XSLT Exercise 10 Exercise on –Producing repeatable, regular result using

82 Numbering xsl:number element is used to insert a formatted number into the result tree value attribute contains an expression which specifies number to be inserted level attribute specifies levels of source tree to consider: single (default), multiple, any count attribute is a pattern specifying nodes to be counted at those levels from attribute is a pattern that specifies where counting starts

83 Numbering format attribute is used for formatting sequence of numbers produced –format token 1 generates the sequence 1 2... 10 11 12..., –format token 01 generates the sequence 01 02... 09 10 11 12... 99 100 101 –format token A generates the sequence A B C... Z AA AB –format token a generates the sequence a b c... z aa ab ac.... –format token i generates the sequence i ii iii iv v vi vii viii ix x –format token I generates the sequence I II III IV V VI VII VIII IX X

84 Numbering When level="single“: Goes up to first node in ancestor-or-self axis that matches the count pattern Constructs a list of length one containing one plus the number of preceding siblings of that ancestor that match the count pattern If the from attribute is specified, then the only ancestors that are searched are those that are descendants of the nearest ancestor that matches the from pattern

85 Numbering When level="multiple“: Constructs a list of all ancestors of the current node in document order followed by the element itself Selects from the list those nodes that match the count pattern Maps each node in the list to one plus the number of preceding siblings of that node that match the count pattern If from attribute specified, only ancestors searched are those that are descendants of the nearest ancestor that matches the from pattern

86 Numbering When level="any“ Constructs a list of length one containing the number of nodes that match the count pattern It involves a set containing the current node and all nodes at any level of the document that are before the current node in document order If from attribute is specified, only nodes after the first node before the current node that match the from pattern are considered

87 Numbering Following would number the items in an ordered list:.

88 Numbering Following two rules would number title elements. –Chapters are numbered 1, 2, 3 –Sections in chapters are numbered 1.1, 1.2, 1.3; <xsl:number level="multiple" count="chapter|section|subsection“ format="1.1 "/>

89 Numbering Following example numbers notes sequentially within a chapter:

90 XSLT Exercise 11 Exercise on –Numbering repeatable, regular result using

91 Conditional Processing xsl:if 2 instructions supporting conditional processing in template: –xsl:if: provides simple if-then conditionality –xsl:choose: supports selection of one choice when there are several possibilities xsl:if element has a test attribute, which specifies an expression Content of xsl:if is a template test expression is evaluated, resulting object is converted to boolean If the result is true, then the content template is instantiated; otherwise, nothing is created

92 Conditional Processing xsl:if In the following, the names in a group of names are formatted as a comma separated list:,

93 XSLT Exercise 12 Exercise on –Conditional Processing using

94 Condnl. Processing xsl:choose xsl:choose element selects one among a number of possible alternatives Consists of a sequence of xsl:when elements followed by an optional xsl:otherwise element Each xsl:when element has a single attribute, test, which specifies an expression The content of the xsl:when, xsl:otherwise elements is a template When xsl:choose element is processed, each of xsl:when elements is tested

95 Condnl. Processing xsl:choose The content of the first, and only the first, xsl:when element whose test is true is instantiated If no xsl:when is true, the content of the xsl:otherwise element is instantiated

96 Condnl. Processing xsl:choose Following example enumerates items in an ordered list depending on depth to which ordered lists are nested.

97 XSLT Exercise 13 Exercise on –Conditional Processing using,

98 Sorting Sorting is specified by adding xsl:sort xsl:sort added as children of –xsl:apply-templates –xsl:for-each When xsl:sort is instantiated by xsl:apply- templates or xsl:for-each, the current node list consists of the complete list of nodes being processed in sorted order

99 Sorting xsl:sort has a select attribute whose value is an expression select expression is evaluated with current node, complete list of nodes being processed in unsorted order as the current node list Resulting object is converted to string, used as the sort key for that node Default value of the select attribute is. –Causes string-value of the current node to be used as the sort key

100 Sorting order attribute specifies order of strings to be sorted (ascending [default], descending order) data-type attribute specifies the data type of the strings (text [default], number)

101 Sorting James Clark ……. The list of employees sorted by name could be generated using:

102 XSLT Exercise 14 Exercise on –Sorting using

103 Variables, Parameters Variable is a name that may be bound to a value Two elements used to bind variables: –xsl:variable –xsl:param Value specified on xsl:param variable is default value When the template within which the xsl:param element occurs is invoked, parameters may be passed that are used in place of the default values xsl:variable, xsl:param have required name attribute specifying name of variable

104 Result Tree Fragments Is an additional data-type into the expression language Variable may be bound to a result tree fragment Represent a fragment of the result tree A result tree fragment is treated equivalently to a node- set that contains just a single root node Not permitted to use the /, //, and [] operators on result tree fragments

105 Variable, Parameter Values Value of the variable specified three alternative ways Variable-binding element has select attribute –Value of select attribute must be an expression –Value of variable is the object that results from evaluating expression Content of the variable-binding element specifies value –Content is a template –Value is result tree fragment equivalent to a node-set containing Single root node Children (sequence of nodes produced by instantiating template) Error if a member of the sequence of nodes created by instantiating the template is attribute or namespace node Variable-binding element has empty content, does not have a select attribute –Value of the variable is an empty string

106 Variable, Parameter Values 2... VERSUS... Anything wrong with the first case?

107 xsl:copy – Var., Para. Values xsl:copy-of can be used to insert a result tree fragment into the result tree xsl:copy-of does not convert result tree fragment to string as xsl:value-of does Required select attribute contains an expression When result of evaluating expression is: –a result tree fragment, the complete fragment is copied into the result tree –a node-set, all the nodes in the set are copied in document order into the result tree

108 Variables, Parameters xsl:variable, xsl:param allowed as top-level elements A top-level variable-binding element declares a global variable that is visible everywhere 12pt

109 Var., Para. within templates xsl:variable, xsl:param are also allowed in templates xsl:variable is allowed anywhere within a template that an instruction is allowed xsl:param is allowed as a child at the beginning of an xsl:template element

110 Passing para’s. to templates Parameters are passed to templates using xsl:with- param Required name attribute specifies name of the parameter xsl:with-param allowed within xsl:call-template, xsl:apply-templates Value of parameter is specified in same way as xsl:variable and xsl:param

111 Passing para’s. to templates 1. a.

112 XSLT Exercise 15 Exercise on –Parameterization using,

113 XSLT Function Library There are additional XSLT-specific additions to the core XPath function library –node-set document (object, node-set?) Refer to XSLT Function Manual for details

114 Keys Keys provide a way to work with documents containing implicit cross-reference structure Limitations of XML (ID, IDREF) approach: –ID attributes must be declared as such in the DTD –Cannot be separate independent sets of unique IDs –ID of element can only be specified in an attribute cannot be specified by content of element, child element –ID is constrained to be an XML name (no spaces) –An element can have at most one ID –At most one element can have a particular ID

115 Keys Key is a triple containing: –Node which has the key –Name of the key –Value of the key Style sheet declares a set of keys for each document using the xsl:key element Key member with node x, name y,value z, means that node x has key with name y and value z Key is a kind of generalized ID, which is not subject to the same limitations as an XML ID

116 Keys Key value for an element may be specified in: –Attribute –Child element –Content XPath expression used to specify where to find the value for a particular named key Multiple keys possible in a document with same node, same key name, but different key values Multiple keys possible in a document with the same key name, same key value, but different nodes

117 Keys xsl:key element is used to declare keys name attribute specifies name of the key xsl:key gives information about keys of any node that matches the pattern specified in the match attribute use attribute is an expression specifying the values of the key

118 Keys A node x has a key with name y,value z if and only if there is an xsl:key element such that: x matches pattern specified in the match attribute of the xsl:key element name attribute of the xsl:key element is equal to y when expression specified in the use attribute of xsl:key element is evaluated with x as the current node, with node list containing just x as the current node list resulting in an object u, then either z is equal to the result of converting u to a string or u is a node- set and z is equal to the string-value of one or more of the nodes in u

119 Keys Function: node-set key(string, object) –First argument specifies the name of the key –Returns a node-set containing the nodes in the same document as the context node that have a value for the named key equal to second argument

120 Keys A function library uses a prototype element to define functions: A function element to refer to function names is: key

121 Keys Style sheet could generate hyperlinks between the references and definitions as follows Function:...

122 XSLT Exercise 16 Exercise on –Cross Reference between reference and definition using, generate-id()

123 Messages xsl:message instruction sends a message in a way that is dependent on the XSLT processor Content of xsl:message instruction is a template If terminate attribute = yes: –XSLT processor will terminate processing after sending message

124 Message Localization Messages for a language L are stored in an XML file resources/L.xml: A problem was detected. <xsl:variable name="messages" select="document(concat('resources/', $lang,'.xml'))/messages"/> problem

125 XSLT Exercise 17 Exercise on –Messages using: –,,, –document() function

126 Extensions Two kinds of extensions –Extension elements –Extension functions Element extension mechanism allows namespaces to be designated as extension namespaces Elements from extension namespace occuring in a template are treated as instructions rather than literal result element Namespace determines the semantics of the instruction.

127 Extensions Extension namespace designated by using extension- element-prefixes attribute on: –xsl:stylesheet –xsl:extension-element-prefixes attribute on literal result element –extension element Value of both these attributes is a white space- separated list of namespace prefixes If XSLT processor does not have an implementation of a particular extension element encountered, processor must perform fallback

128 Fallback XSLT processor performs fallback for instruction element, if: –Instruction element has one or more xsl:fallback children, content of each is instantiated in sequence –Otherwise, an error must be signaled Content of xsl:fallback element is a template Following used to explicitly control style sheet behavior for unavailable elements, functions –boolean element-available(string) –boolean function-available(string)

129 Output XSLT processor outputs result tree as specified by xsl:output xsl:output element is only allowed as a top-level element method attribute used to identify method to be used for outputting the result tree method attribute values could be: xml, html, text If document element = html –output method is html –Else output method = xml

130 Combining Style sheets 2 mechanisms to combine style sheets: –Inclusion mechanism: allows style sheets to be combined without changing the semantics of the style sheets being combined –Import mechanism: allows style sheets to override each other

131 Style sheet Inclusion Style sheet may include another one using xsl:include –href attribute = URI reference identifying the style sheet to be included xsl:include element is only allowed as a top-level element

132 Style sheet Inclusion Inclusion works at the XML tree level Resource located by href attribute value is parsed as an XML document Children of the including xsl:stylesheet element replace xsl:include element in the including document

133 Style sheet Import Style sheet may import another one using xsl:import –href attribute = URI reference identifying the style sheet to be included Importing same as including except that definitions, template rules in the importing style sheet take precedence over template rules and definitions in the imported style sheet

134 Style sheet Import xsl:import element is only allowed as top- level element xsl:import element children must precede all other element children of an xsl:stylesheet element, including any xsl:include element children

135 Style sheet Import When xsl:include used to include style sheet, any xsl:import elements in the included document are moved up in the including document to after any existing xsl:import elements in the including document

136 XSLT Exercise 18 Exercise on –,

137 Embedding Style sheets 2 forms of embedding are possible: –XSLT stylesheet may be textually embedded in a non-XML resource –xsl:stylesheet element may occur in an XML document other than as the document element

138 Embedding Style sheets...

139 XSLT Exercise 19 Exercise on –Embedding Style sheets

140 White Space Stripping After source, style sheet trees are constructed, but before XSLT processing, some text nodes are stripped A text node is preserved if any of the following apply: –The element name of the parent of the text node is in the set of whitespace-preserving element names –The text node contains at least one non-whitespace character –An ancestor element of the text node has an xml:space attribute with a value of preserve, and no closer ancestor element has xml:space with a value of default

141 White Space Stripping For style sheets, the set of whitespace- preserving element names consists of –xsl:text For source documents, the set of whitespace- preserving element names is specified by (top- level): –xsl:strip-space –xsl:preserve-space Both have attributes whose values are a whitespace- separated list of Elements

142 Overriding Template Rules A template rule used to override a template rule in an imported style sheet can use xsl:apply-imports to invoke the overridden template rule xsl:apply-imports processes the current node using only template rules that were imported into the style sheet element containing the current template rule

143 Overriding Template Rules …… Combined Effect:...

144 Conflict Resolution If source node matches more than one template rule then: –All matching template rules with lower priority than matching template rule(s) with the highest priority are eliminated Priority given by priority attribute on template rule Else, default priority is also used

145 Conflict Resolution Default priority computed as follows: Priority = 0: –If pattern has form of QName preceded by child/ attribute axis OR –If pattern has form of processing-instruction ( literal ) preceded by child/ attribute axis Priority = -0.25: –If pattern has form NCName :* preceded by child/ attribute axis Priority = -0.5: –If pattern only has NodeTest preceded by child/ attribute axis Otherwise, the priority is 0.5

146 Modes Allow an element to be processed multiple times, each time producing a different result xsl:template, xsl:apply-templates have an optional mode attribute If xsl:template does not have a match attribute, it must not have a mode attribute If xsl:apply-templates has mode attribute, it applies only to those template rules ( xsl:template) with mode attribute with the same value If xsl:apply-templates does not have mode attribute, it applies only to those template rules ( xsl:template) that do not have a mode attribute

147 XSLT Exercise 20 Exercise on –Average of Numbers

148 XSLT Exercise 21 Exercise on –xsl:copy –xsl:copy-of –xsl:sort –msxsl:node-set()


Download ppt "XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL."

Similar presentations


Ads by Google