Podstrony
|
[ Pobierz całość w formacie PDF ]
most common XPath expressions, simply to introduce the concept. The expression @type="unordered" specifies an attribute named type whose value is "unordered". So an expression like LIST/@type specifies the type attribute of a LIST element. But now for something a little different! In XPath, the square-bracket notation ([]) normally associated with indexing is extended to specify selection-criteria. For example, the expression LIST[@type="unordered"] selects all LIST elements whose type value is "unordered". Similar expressions exist for elements, where each element has an associated string-value. (You'll see how the string-value is determined for a complicated element in a little while. For now, we'll stick with super-simple elements that have a single text string.) http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/1_intro.html (4 of 10) [8/22/2001 12:52:20 PM] 1. Intro to XSLT Suppose you model what's going on in your organization with an XML structure that consists of PROJECT elements and ACTIVITY elements that have a text string with the project name, multiple PERSON elements to list the people involved and, optionally, a STATUS element that records the projects status. Here are some more examples that use the extended square-bracket notation: /PROJECT[.="MyProject"] selects a PROJECT named "MyProject". /PROJECT[STATUS] -- selects all projects that have a STATUS child element. /PROJECT[STATUS="Critical"] -- selects all projects that have a STATUS child element with the string-value "Critical". Combining Index Addresses The XPath specification defines quite a few addressing mechanisms, and they can be combined in many different ways. As a result, XPath delivers a lot of expressive power for a relatively simple specification. This section illustrates two more interesting combinations: LIST[@type="ordered"][3] -- selects all LIST elements of type "ordered", and returns the third. LIST[3][@type="ordered"] -- selects the third LIST element, but only if it is of "ordered" type. Note: Many more combinations of address operators are listed in section 2.5 of the XPath Specification. This is arguably the most useful section of the spec for defining an XSLT transform. Wildcards By definition, an unqualified XPath expression selects a set of XML nodes that matches that specified pattern. For example, /HEAD matches all top-level HEAD entries, while /HEAD[1] matches only the first. But XPath expressions can also contain one of several wildcards to broaden the scope of the pattern matching: matches any element node (not attributes or * text) matches all nodes of any kind: element nodes, text nodes, attribute nodes, node()processing instruction nodes, namespace nodes, and comment nodes. @* matches all attribute nodes http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/1_intro.html (5 of 10) [8/22/2001 12:52:20 PM] 1. Intro to XSLT In the project database example, for instance, /*/PERSON[.="Fred"] matches any PROJECT or ACTIVITY element that includes Fred. Extended-Path Addressing So far, all of the patterns we've seen have specified an exact number of levels in the hierarchy. For example, /HEAD specifies any HEAD element at the first level in the hierarchy, while /*/* specifies any element at the second level in the hierarchy. To specify an indeterminate level in the hierarchy, use a double forward slash (//). For example, the XPath expression //PARA selects all paragraph elements in a document, wherever they may be found. The // pattern can also be used within a path. So the expression /HEAD/LIST//PARA indicates all paragraph elements in a subtree that begins from /HEAD/LIST. XPath Data Types and Operators XPath expressions yield either a set of nodes,: a string, a boolean (true/false value), or a number. Expressions can also be created using one of several operations on these values: | Alternative. So PARA|LIST selects all PARA and LIST elements. or, and Returns the or/and of two boolean values. =, != Equal or not equal, for booleans, strings, and numbers. Less than, greater than, less than or equal to, greater than or equal to -- for , = numbers. Add, subtract, multiply, floating-point divide, and modulus (remainder) +, -, *, div, operations mod (e.g. 6 mod 4 = 2) Finally, expressions can be grouped in parentheses, so you don't have to worry about operator precedence. (Which, for those of you who who are good at such things, is roughly the same as that shown in the table.) String-Value of an Element Before going on, it's worthwhile to understand how the string-value of more complex element is determined. We'll do that now. The string-value of an element is the concatenation of all descendent text nodes, no matter how deep. So, for a "mixed-model" XML data element like this: http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/1_intro.html (6 of 10) [8/22/2001 12:52:20 PM] 1. Intro to XSLT This paragraph contains a bold word the string-value of is "This paragraph contains a bold word". In particular, note that is a child of and that the text contained in all children is concatenated to form the string-value. Also, it is worth understanding that the text in the abstract data model defined by XPath is fully normalized. So whether the XML structure contains the entity reference "<" or " section, the element's string-value will contain the " XML with an XSLT stylesheet, occurrences of " CDATA section. Similarly, occurrence of "&" will need to be converted to &. XPath Functions This section ends with an overview of the XPath functions. You can use XPath functions to select a
[ Pobierz całość w formacie PDF ]
zanotowane.pldoc.pisz.plpdf.pisz.plkskarol.keep.pl
|