Xslt how does it work




















So we'd then create a template, like this:. This, obviously, is much easier to read. Instead you could use it to just transform XML in to a different structure. It also adds:. Styling requires a source XML documents, containing the information that the style sheet will display and the style sheet itself which describes how to display a document of a given type.

This XML file doesn't contain any presentation information, which is contained in the stylesheet. Separating the document's content and the document's styling information allows displaying the same document on different media like screen, paper, cell phone , and it also enables users to view the document according to their preferences and abilities, just by modifying the style sheet.

Here are two templates from the stylesheet used to format the XML file. The full stylesheet which includes extra information on pagination and margins is available. Broadly speaking, there are two kinds of template rules:. XSLT defines a built-in template rule for each of the seven types of node. This ensures that any call to xsl:apply-templates will never fail to find a matching template rule for the current node, even if your stylesheet contains no explicit template rules at all an empty stylesheet.

We'll define exactly what the built-in template rules are in the Built-In Template Rules section later in this chapter.

The template rules that you define are explicitly present in your stylesheet. They are xsl:template elements that have a match attribute. Example 1 shows a template rule that matches any foo element. The value of the match attribute is an XSLT pattern. Unlike an XPath expression, a pattern is not concerned with selecting a set of nodes from a given context. Instead, it has a more passive role. We'll look more closely at how patterns are interpreted in the upcoming section Patterns.

Whenever xsl:apply-templates is called, the XSLT processor examines the patterns of all the stylesheet's template rules. For each node being iterated over, it first finds all the template rules with patterns that match the node and then instantiates the best-matching template rule among them.

The template rule we saw in Example 1 contained an xsl:apply-templates element. There are two things worth noting about this. First, its presence illustrates the recursive nature of XSLT processing.

This process continues until there are no more nodes to process. At that point, the entire result tree has been constructed. The second thing worth noting is that the select attribute on xsl:apply-templates is optional:.

This instruction applies templates to the child nodes of the current node. In other words, it populates the current node list with the set of nodes returned by the node expression, and it iterates over them in document order, invoking the best-matching template rule for each node.

XSLT patterns appear most commonly inside the xsl:template element's match attribute. They also appear in certain attributes of the xsl:key and xsl:number elements. In other words, every pattern is also a syntactically valid expression, but not every expression is a valid pattern. A pattern consists of one or more location path patterns separated by. A location path pattern is a location path that exclusively uses either the child or attribute axis in each of its steps. Finally, a location path pattern can also start with an id or key function call with a literal string argument.

A node matches a pattern if the node is a member of the result of evaluating the pattern as an expression with respect to some possible context node. When a pattern consists of more than one location path pattern separated by , the location path patterns are treated as alternatives.

A node matches the pattern if it matches any of the alternatives. The upshot of this is that two or more template rules that have the same content can be syntactically combined into one xsl:template element, simply by putting their match values together and separating them with. For example, this:. When a given node matches the patterns of more than one template rule, the XSLT processor decides which template rule to instantiate according to its rules for conflict resolution.

For example, it is quite common to have a stylesheet that includes two template rules like these:. The first template rule matches foo elements. The second matches any element. That means that a foo element will match both template rules. But the XSLT processor has to pick only one of them. Assuming that the stylesheet containing these rules isn't imported into another stylesheet that overrides them, the XSLT processor will pick the template rule with the foo pattern.

Generally speaking, the more specific a pattern is, the higher priority it has. We'll describe exactly how priority is determined in the next section, Priority. However, before patterns are ever examined for their relative priority, the XSLT processor first eliminates all matching template rules that have lower import precedence.

Basically, template rules in an imported stylesheet have lower import precedence than template rules in the importing stylesheet. It is an error if there is more than one template rule left. If that happens, the XSLT processor can either signal the error or recover by invoking the matching template rule that occurs last in the stylesheet. Most processors will at least give a warning if this happens. A template rule can explicitly specify its priority using the optional priority attribute on the xsl:template element.

The value of the priority attribute may be any decimal number—positive or negative.



0コメント

  • 1000 / 1000