So far, this assignment has been all theory. You charge this access as a foundation for activated application, which is what the blow of this assignment is all about.
XSLT wouldn’t assignment if it didn’t accept some affectionate of apparatus to bout and baddest nodes and act on them. You charge to be able to authentic which bulge or nodes should match. This is what XPath expressions are for. XPath is the accent you use to specify which bulge or nodes you appetite to assignment with. Expressions in XPath can be actual simple, pointing to a specific area within a certificate timberline application complete addressing. You can, however, accomplish selections based on actual circuitous rules. As you assignment your way through this book, you will apprentice to actualize added and added circuitous expressions. But, of course, you charge to alpha simple.
If you’re accustomed with addresses on the World Wide Web, the best basal XPath expressions are accessible to understand. A Web armpit is a bureaucracy of files, aloof like an XML certificate is a bureaucracy of elements. If you appointment a Web site, you specify its basis with the name of the Web site. For example, http://www.somesite.com credibility to the basis or home folio of the Web site. This is the aforementioned as http://www.somesite.com/, which is absolutely added accurate. What comes afterwards this allotment of the abode specifies area in the bureaucracy of the armpit you appetite to be. So, http://www.somesite.com/menu/entrees credibility to the basis book in the entrees directory, which is a adolescent of the card directory, which is a adolescent of the basis directory. The /menu/entrees aisle is abnormally interesting. It abnormally identifies a area aural the Web armpit hierarchy, as apparent in Figure 3.4.
Figure 3.4 Web armpit hierarchy.
Figure 3.4 shows allotment of the bureaucracy for the Web site. Notice that /menu/entrees abnormally identifies the entrees bulge in the tree. If you appetite to baddest the desserts node, you change to /menu/desserts. Now attending at Listing 3.1.
You can download the sample listings in this assignment from the publisher’s Web site.
The XML in Listing 3.1 has the aforementioned timberline anatomy as that of the Web armpit depicted in Figure 3.4. So, aloof like in the Web site, /menu/entrees credibility to the entrees aspect in the XML document. Pointing to a assertive bulge in an XML certificate with XPath is, as you can see, actual simple. It is based on attempt that you accept apparently acclimated before, so they’ll be accustomed to you, alike admitting you’ve never formed with XPath before. To see how this access absolutely works, attending at Listing 3.2.
The arrangement on band 5 matches the basis aspect of the antecedent document. The amount retrieved on band 6 is alleged application the expression /menu/entrees, which akin the entrees aspect that is the adolescent aspect of the basis aspect menu. The aftereffect from applying Listing 3.2 to Listing 3.1 is apparent in Listing 3.3.
Be acquainted that the above-mentioned sample was run with the Saxon processor. If you use addition processor, the aftereffect ability be hardly different. MSXSL generates UTF-16 achievement by default, so the aftereffect back application MSXSL will accept spaces amid anniversary letter.
Listing 3.3 shows the amount of all the adolescent nodes of the entrees node. If you bethink yesterday’s lesson, that is absolutely right, as band 6 of Listing 3.2 asks for the amount of the entrees node. That node’s amount contains all its brood nodes. Accepting its amount yields the argument value of the brood elements. This book is a bit ambagious because it looks like Listing 3.2 absolutely selects a node-set consisting of all the child elements of the entrees node. If the entrees bulge also contains a argument value, you would see that this isn’t true. You can, however, actualize an added arrangement to handle the bowl elements, as apparent in Listing 3.4.
In Listing 3.4, agenda that curve 9 and 16 finer abstain the appetizers and desserts nodes in Listing 3.1 to accumulate the aftereffect baby and to the point. The aftereffect of applying Listing 3.4 to Listing 3.1 is apparent in Listing 3.5.
In Listing 3.5, anniversary bowl bulge is now handled separately. The hyphen (-) in advanced of anniversary bowl shows that this is absolutely the case. The whitespace appears, as I said before, because of the processor’s default whitespace rules.
The botheration with the cipher apparent so far is that it acts on an aspect or a set of elements. Aural the set of elements (such as the bowl elements), no one bulge is singled out. If you additionally abode a bowl bulge instead of analogous it with a template, a reasonable acceptance would be that you will get the amount of a distinct bowl node. Listing 3.6 tests this assumption.
In Listing 3.6, the arrangement on band 11 analogous the entrees element selects alone the amount of a bowl bulge on band 12. Agenda that, compared to Listing 3.4, there is no arrangement analogous the bowl element. The aftereffect for Listing 3.6 is apparent in Listing 3.7.
In Listing 3.7, the acceptance fabricated for Listing 3.6 is correct. Accepting the amount of a bowl bulge yields the amount of absolutely one dish node. But what happened to the added nodes? Afterwards all, the xsl:value-of aspect matches bowl nodes, so it, in fact, matches a node-set. The actuality that you get a distinct amount is afresh a aftereffect of the absence behavior of XSLT. If a node-set matches a xsl:value-of selection, alone the amount of the aboriginal aspect in the node-set is used. The aboriginal aspect is the one that comes aboriginal in the antecedent XML, according to the selectionin this case, the Grilled Salmon.
Now a new catechism arises: How do you accurately baddest addition aspect in the node-set? Fortunately, you can aloof specify the cardinal of the aspect you appetite to select. This, however, deviates from the aisle characters you are familiar with from Web sites. You charge to abode the cardinal of the aspect you appetite to baddest amid aboveboard brackets, [ and ]. Hence, you baddest the third bowl aspect like this:
In abounding programming languages, a account of elements is numbered from 0, so aspect cardinal 3 is absolutely the fourth aspect in the list. XSLT numbers a list starting with 1, so aspect cardinal 3 is the third element.
The amount amid the aboveboard brackets is alleged a predicate, which is a accurate or announcement that determines whether a assertive bulge should be included in the selection.
Note that the above-mentioned archetype uses about addressing, which agency that the alternative is done based on the accepted location. If that is the entrees element, the third bowl aspect in the entrees element is selected. If the accepted bulge has no adolescent elements alleged dish, the amount is empty.
Because predicates can be expressions, they can become absolutely complex, testing whether an aspect conforms to assertive criteria. As I said at the alpha of this section, I’ll altercate added circuitous expressions after in this book. The article now is to accomplish you accustomed with the architecture blocks, so let’s advance with an archetype based on what you’ve apparent so far. The archetype in Listing 3.8 creates a card of the day from the sample XML in Listing 3.1.
Listing 3.8 has alone one arrangement on band 5, analogous the basis element. This arrangement displays the ethics for altered elements in Listing 3.1 using complete acclamation and cardinal predicates. Band 7 selects the second bowl aspect in the appetizers element; band 8, the third bowl aspect in the entrees element; and band 9, the first bowl aspect in the desserts element. Listing 3.9 shows the result.
Listing 3.8 contains alone a arrangement analogous the basis of the XML source. The blow of the stylesheet’s functionality utilizes complete acclamation to get the capital values. As you can see, this yields a account of dishes that form today’s menu.
Earlier, you abstruse about the accepted node. Afterwards a arrangement is fired, a assertive bulge is advised the accepted node. A aisle announcement doesn’t accommodate a accepted node, but it does abide of ambience nodes. A context bulge is a allotment of an announcement that operates on a assertive node.
The predicates acclimated in Listing 3.8 accomplish on the ambience nodein this case, the bowl node. At one point or another, anniversary allotment of the path’s announcement is the ambience node. It, however, is accordant alone when you’re alive with predicates in a aisle expression. You can have predicates at several stages aural the aisle expression, in which case the ambience bulge is the bulge that the assert operates on.
You charge apprehend that if no bout occurs, annihilation happens. This is additionally the case if a assert holds a cardinal for which no aspect exists. In that case, the cardinal is aloof ignored. You don’t see an absurdity bulletin or anything cogent you that an aspect is missing. The clue is not that an aspect is missing, but rather that no aspect matches that accurate rule, so the aphorism is never applied.
So far, you’ve abstruse alone about elements. But what about attributes? Earlier, I said that elements and attributes don’t alter actual much, so you ability apprehend that you can abode them in the aforementioned way, as Listing 3.10 tries to do.
Line 8 in Listing 3.10 tries to get the amount of the amount attribute of a bowl element. That this access doesn’t assignment is accessible from the aftereffect in Listing 3.11.
The value-of aspect in Listing 3.10 doesn’t crop a result. A gap appears in the aftereffect because no aspect matches the select expression. That is as it should be, because no amount aspect exists. The bowl aspect does, however, accept a amount attribute, which is what Listing 3.10 is declared to select. What’s wrong?
Nothing is wrong. You aloof charge to acquaint the processor that it needs to match an attribute, not an element. You can acquaint the processor that you’re attractive for an aspect by abacus the @ appearance in advanced of the name. So, if band 8 in Listing 3.10 is declared to point to an attribute, the aisle announcement should be /menu/desserts/dish/@price, as apparent in Listing 3.12. The aftereffect in Listing 3.13 is now correct.
Listing 3.12 produces the adapted result. Because attributes don’t have any adolescent elements, you additionally apperceive that no ancillary furnishings will occur. The amount of the aspect is consistently text.
Attributes can accept abstracts types, but all are based on the argument value. Data types will be thoroughly discussed on Day 10, “Working with Data Types.”
Because attributes don’t accept ancillary effects, they are abundant easier to work with. Also, because all attributes of an aspect charge to accept a altered name, you accept no agitation with analogous assorted attributes (and accepting alone the amount of the first). The alone way you can get assorted attributes is to start alive with selections based on a wildcard character.
Another point to accede is that attributes booty beneath amplitude in a document than elements. Elements accept activate and end tags; an aspect doesn’t need these tags. Because attributes accept alone a argument value, a parser or processor can accord with them added bound because it doesn’t accept to analysis for child elements. This is acceptable to accept a complete aftereffect on performance.
Until now, the altercation has targeted distinct nodes wherever possible. In fact, the focus has been on how to abstain selections that crop added than one node. Although this advice is actual advantageous to get you started, it really banned your capabilities. Without creating circuitous expressions, you can already accomplish abounding tasks with some of the basal functionality XPath provides.
Wildcard characters are accepted in best search-oriented functions and languages. XPath has alone one wildcard character: *. You can use it alone to bout complete names of elements or attributes, so the expression a* does not bout all elements starting with the letter a. This announcement generates an absurdity instead. Wildcards are advantageous back you appetite to assignment added into the antecedent XML, and the names of assertive nodes (particularly antecedent nodes) don’t matter. Listing 3.14 shows how to use a wildcard.
Line 5 in Listing 3.14 uses a wildcard character, so it doesn’t matter whether the akin bowl aspect is a adolescent aspect of the appetizers, entrees, or desserts element. Band 6 just shows the amount of the akin bowl element. Listing 3.15 shows the result.
In Listing 3.15, the aftereffect yields all the bowl nodes, not just those that are adolescent nodes of a accurate node.
You can use this address in all kinds of situations. Say that you’ve created a whitepaper or book application an XML document. Application a wildcard, you can baddest all the affiliate and area headers to actualize a table of contents. If you don’t appetite to get all the nodes, application aloof the wildcard doesn’t solve your problem. However, aloof as with the aisle expressions you saw earlier, you can use predicates to clarify the expression. That way, you accept added control over what the wildcard absolutely matches. A simple archetype is apparent in Listing 3.16.
Listing 3.16 yields the aforementioned aftereffect as Listing 3.12; the aftereffect is apparent in Listing 3.13. Instead of acclamation nodes by name, band 8 in Listing 3.16 uses several wildcards aided by position predicates. The desserts aspect is the third adolescent aspect of the card element. The /menu/* area of the aisle announcement tells the processor to booty the third child aspect of the card element, with no attention to the name of that element. That announcement yields the desserts element, aloof as if it had been named. The aspect alleged is additionally based on a wildcard. In this case, the announcement tells the processor to booty the additional aspect of the dish element, which is, of course, the amount attribute.
What you’ve apparent so far is absolutely adamant in utilizing the XML document tree. XPath can do abundant more, based on area paths, which are expressions that baddest a bulge or node-set about to the ambience node. A area aisle consists of several parts. The aisle expressions you’ve seen so far generally are specific cases of area paths, starting from the basis node or accepted bulge (from a analogous template). Area paths can, however, appear in added instances and as allotment of a assert aural a aisle expression. One part of a area aisle is absolutely a predicate, so you can accept a area aisle with a predicate, complete a area aisle complete addition predicate, and so on.
Another allotment of a area aisle that you are already accustomed with is called a bulge test, which is the allotment of the area aisle that matches a assertive bulge or nodes. This analogue is clearer with an example. Accede the afterward area path:
Here, menu, *, dish, and @* are node tests.
The aftermost (or absolutely the first) allotment of a area aisle may not be familiar to you yet. This part, alleged an axis, is an announcement allegorical the accord aural the certificate timberline amid the alleged nodes and the context node.
In the antecedent examples, you saw absolutely a few area paths. Not all of them independent axes, however. Well, they did, but the axes were included implicitly. Attending at the afterward area path:
This area aisle selects all the bowl elements, which are child nodes of desserts nodes, which in about-face are adolescent nodes of the root aspect menu. If you address out that area aisle in full, it actually reads
The arbor and the bulge analysis are consistently afar by a bifold colon. The axis in advanced of the bulge analysis tells the processor area to attending for a bulge or node-set. The bulge analysis tells the processor which nodes to absolutely match. Application the complete area aisle isn’t actual advantageous for bout templates. The area aisle alone becomes lengthier and beneath readable. Also, you would not generally use the adolescent arbor because it is included around anyway.
Another arbor you are already accustomed with is the aspect axis. You are absolutely accustomed with its shortcut, the @ character. In Listing 3.12, the afterward area aisle was acclimated on band 8:
You additionally can address the @price alternative application the aspect axis, which would crop the afterward area path:
An arbor that you haven’t yet encountered but is absolutely bright is the antecedent axis. Yes, you estimated it: It allotment the antecedent bulge of the accepted context. Listing 3.17 shows an archetype application the parent axis.
Line 8 in Listing 3.17 uses the antecedent arbor to baddest the appellation aspect of the antecedent element. This is the antecedent aspect of the accepted node, which is one of the bowl elements in the appetizers element. Listing 3.18 shows the result.
In Listing 3.18, the appellation aspect is amid with every dish. This could additionally accept been done with complete addressing, but if there had been a abstracted arrangement for the bowl element, dishes in entrees or desserts could additionally bout and the appellation would charge to be different. In that case, about acclamation application the antecedent arbor would be the only way out.
There is addition way to specify any antecedent node. If you’re accustomed with the command alert from DOS or Unix, it will attending familiar. The area path
can additionally be accounting as
The closing archetype is abundant added bunched and usable. If, however, the node analysis is not a wildcard, application this area aisle would not work. You would have to specify the antecedent arbor explicitly, as able-bodied as the bulge that the bulge analysis needs to match.
The axes discussed so far awning a distinct akin in the XML certificate tree. They either go one akin down, to adolescent nodes, or one akin up, to the antecedent node. Several axes accomplish on assorted levels. This does not mean, however, that such an arbor allotment a timberline fragment because that wouldn’t accord added functionality. If that were the case, the antecedent and adolescent axes would suffice. Anniversary multilevel arbor yields a node-set complete all the elements aural that axis. The adjustment in which the elements arise in the node-set depends on the axis. So, you can absolutely anticipate of a multilevel arbor as allotment of the XML certificate timberline “flattened” into a set.
The best way to appearance you how these axes assignment is to go through some examples. The easiest arbor to alpha with is ancestor, which is apparent in Listing 3.19.
All the activity occurs in Curve 6 and 7 in Listing 3.19. Both curve first baddest the aboriginal adolescent bulge of the appetizers aspect named dish. At that point, this aspect becomes the ambience node. Next, the antecedent arbor tells the processor it wants to act on the antecedent nodes of the ambience node. The antecedent nodes are appetizers and menu. On band 6, the bulge analysis consists of a wildcard and predicatein this case, pointing to the aboriginal antecedent bulge in the axis, which is the antecedent node appetizers. On band 7, a alleged bulge analysis specifies that the card aspect is the aspect bare in this accurate axis. This line could additionally accept been accounting as ancestor::* because that is the grandparent of the ambience bulge and, as such, the additional bulge in the axis node-set ancestor.
Listing 3.20 shows that Listing 3.19 aboriginal selects the title aspect of the appetizers aspect and again the title aspect of the entrees element.
Several added multilevel axes are available. All axes are listed in Table 3.1.
The ambience bulge itself
All adolescent nodes of the ambience node
The antecedent bulge of the ambience node
All antecedent nodes of the ambience node
Same as ancestor, including the ambience bulge as the aboriginal bulge in the node-set
All brood nodes of the ambience node, numbered abyss aboriginal (for example, adolescent 1, grandchild 1, adolescent 2, grandchild 2, and so on)
Same as descendant, including the ambience bulge as the aboriginal bulge in the node-set
All affinity nodes that accomplish the ambience bulge aural the certificate tree
Same as following-sibling, but including their descendants, abyss aboriginal (for example, affinity 1, adolescent 1 of affinity 1, affinity 2, and so on)
All affinity nodes that announce the ambience bulge aural the certificate tree
Same as preceding-sibling, but including their descendants, abyss aboriginal (for example, affinity 1, adolescent 1 of affinity 1, affinity 2, and so on)
Figure 3.5 shows a graphical representation of best axes in Table 3.1 to accord you a bigger abstraction of what anniversary arbor listed absolutely selects.
Figure 3.5 Graphical representation of the axes in Table 3.1.
Something that is, carefully speaking, not an arbor but fits accurately in this area is the // expression. It matches any area aural the certificate tree. So, //dish matches any bowl bulge aural the document. Listing 3.21 shows this announcement in action.
Listing 3.21 yields the aforementioned aftereffect as Listing 3.14 because band 5 uses // to bout all nodes in the certificate from which the dish nodes are selected.
A accepted aberration is to anticipate that //dish will crop the sixth bowl aspect aural the document. Unfortunately, because of antecedence rules, this announcement will crop no aspect at all. The processor will attending for a sixth aspect in anniversary of the antecedent contexts. Unfortunately, there is none because appetizers contains alone four bowl elements; entrees, alone five; and desserts, alone three. So how do you break this problem? You can use parentheses to override the absence precedence. Therefore, (//dish) would crop the sixth aspect (the Seafood Pasta).
What Makes Dessert Order Form Template So Addictive That You Never Want To Miss One? | Dessert Order Form Template – dessert order form template
| Pleasant to help our website, in this moment I’ll demonstrate in relation to dessert order form template