What is xsl transformation




















This template rule uses and to accomplish that:. This template matches both the first and last ATOM elements in their parent by matching when the position is 1 or when the position is equal to the number of elements in the set using the last function :. This is logical or , so it will also match if both conditions are true. The not function reverses the result of an operation. For example, this template rule matches all ATOM elements that are not the first child of their parents:.

XSLT does not have an exclusive or operator. However, one can be formed by judicious use of not , and , and or. For example, this rule selects those ATOM elements that are either the first or last child, but not both:. Even numbers like 43 or that look like integers are stored as doubles. Non-number values, such as strings and booleans, are converted to numbers automatically as necessary, or at user request through the number function using these rules:.

A string is trimmed of leading and trailing white space, then converted to a number in the fashion you would expect; for example, the string "12" is converted to the number If the string cannot be interpreted as a number, it is converted to NaN. For example, this template only outputs the transuranium elements; that is, those elements with atomic numbers greater than 92 the atomic number of uranium.

This string is then converted into a number. These operations are more commonly used as part of a test. For example, this rule selects those elements whose atomic weight is more than twice their atomic number:.

XPath also provides the less familiar mod operator, which takes the remainder of two numbers. When used in conjunction with position , this operator enables you to perform tasks such as outputting every second ATOM or alternating colors between rows in a table.

For example, these two rules use different colors for alternate rows of a table:. You can change the divisor to 3 to apply different styles to every third element, to 4 to apply different styles to every fourth element, and so forth. For example, this template rule estimates the number of neutrons in an atom by subtracting the atomic number the number of protons from the atomic weight the weighted average over the natural distribution of isotopes of the number of neutrons plus the number of protons and rounding to the nearest integer:.

This rule calculates the average atomic weight of all the atoms in the table by adding all the atomic weights, and then dividing by the number of atoms:. A string is a sequence of Unicode characters. Other data types can be converted to strings using the string function according to these rules:. Node-sets are converted to strings by taking the value of the first node in the set, as calculated by the xsl:value-of element, according to the rules given in Table Again, the value of this element is calculated by the xsl:value-of element according to the rules given in Table Boolean false is converted to the English word false.

Boolean true is converted to the English word true. Besides string , XSLT contains 10 functions that manipulate strings. These are summarized in Table String Returns the concatenation of as many strings as are passed as arguments in the order they were passed. This is especially true if the document does not follow a stable, predictable order like the periodic table, but rather throws elements together willy-nilly like many web pages.

In those cases, you should have general rules that can find an element and apply templates to it regardless of where it appears in the source document. To make this process easier, XSLT defines several default template rules that are implicitly included in all style sheets.

The first default rule matches root and element nodes, and applies templates to all child nodes. The second default rule matches text nodes and attributes, copying their values into the output. Together, these two rules mean that even a blank XSLT style sheet with just one empty xsl:stylesheet element will still produce the raw character data of the input XML document as output. That is, unless another rule overrides this one especially for the root element , all element nodes will be processed.

However, once an explicit rule for any parent of an element is present, this rule will not be activated for the child elements unless the template rule for the parent has an xsl:apply-templates child.

For example, you can stop all processing by matching the root element and neither applying templates nor using xsl:for-each to process the children, like this:. Exceptionally observant readers may have noted that several of the examples seem to have output the contents of some elements without actually taking the value of the element they were outputting!

This rule is as follows:. In other words, it copies the text from the input to the output. Another rule can override this one for specific elements where you want either more or less than the text content of an element.

This rule also copies attribute values but not names. However, they turn from attributes in the input to simple text in the output. Because there's no default rule that ever applies templates to attributes, this rule won't be activated for attributes unless you specifically add a nondefault rule somewhere in the style sheet that does apply templates to attributes of one or more elements.

It looks like this:. You can, of course, replace this with your own rule for handling processing instructions and comments if you want to. Together, the default rules imply that applying an empty style sheet with only an xsl:stylesheet or xsl:transform element but no children such as Listing to an XML document copies all the PCDATA out of the elements in the input to the output. However, this method produces no markup. These are, however, extremely low priority rules.

Consequently, any other matches take precedence over the default rules. You have to make sure that you explicitly match any node whose contents including descendants you want to output. Attribute value templates enable a style sheet to determine the content of an attribute dynamically based on the content of the input document rather than using a literal fixed value in the style sheet. For example, suppose you want to convert the periodic table into empty ATOM elements with this attribute-based form:.

To do this, you must extract the contents of elements in the input document and place those in attribute values in the output document. But this is malformed XML. The correct way to write the preceding template rule is like this:. Attribute value templates can have more complicated patterns than merely an element name. In fact, you can use any XPath expression in an attribute value template.

Attribute values are not limited to a single attribute value template. You can combine an attribute value template with literal data or with other attribute value templates. For example, this template rule matches ATOM elements and replaces them with their name formatted as a link to a file in the format H. More than one attribute value template can be included in an attribute value. For example, this template rule includes the density units as part of the VALUE attribute rather than making them a separate attribute:.

You can place attribute value templates in many attributes in an XSLT style sheet. This is particularly important in xsl:element , xsl:attribute , and xsl:processing-instruction elements discussed in the next section , where attribute value templates allow the designer to defer the decision about exactly what element, attribute, or processing instruction appears in the output until the input document is read.

For example, you might want to change the contents of a FILENAME element into the HREF attribute of an A element, or replace one element type in the input with several different element types in the output depending on the value of an attribute. This is accomplished with xsl:element , xsl:attribute , xsl:processing-instruction , xsl:comment , and xsl:text elements. Elements are usually included in the output document simply by including the literal start- and end-tags in template content.

However, occasionally, you need to use details from the input document to determine which element to place in the output document. This might happen, for example, when making a transformation from a source vocabulary that uses attributes for information to an output vocabulary that uses elements for the same information.

The xsl:element element inserts an element into the output document. The name of the element is given by an attribute value template in the name attribute of xsl:element. The content of the element derives from the content of the xsl:element element, which may include xsl:attribute , xsl:processing-instruction, and xsl:comment instructions all discussed shortly to insert these items.

Using xsl:element , a single rule can do this by converting the value of the STATE attribute to an element name. This is how it is done:. By using more complicated attribute value templates, you can perform most of the calculations that you might need. You can include attributes in the output document simply by typing the literal attributes themselves. However, you frequently have to rely on data that you read from the input document to determine an attribute value and sometimes even to determine the attribute name.

For example, suppose you want a style sheet that selects atom names and formats them as links to files named H. Each different element in the input will have a different value for the HREF attribute.

The xsl:attribute element calculates an attribute name and value and inserts it into the output. Each xsl:attribute element is a child of either an xsl:element element or a literal result element. The attribute calculated by xsl:attribute will be attached to the element calculated by its parent in the output.

The name of the attribute is specified by the name attribute of the xsl:attribute element. The value of the attribute is given by the contents of the xsl:attribute element. For example, this template rule produces the output previously shown:.

All xsl:attribute elements must come before any other content of their parent element. For example, this template is illegal:. You often need to apply the same group of attributes to many different elements of either the same or different classes. For example, you might want to apply a style attribute to each cell in an HTML table. To make this simpler, you can define one or more attributes as members of an attribute set at the top level of the style sheet with xsl:attribute-set , and then include that attribute set in an element with an xsl:use-attribute-sets attribute.

An element can use more than one attribute set by specifying the names of the all the sets in a white-space-separated list in the value of the xsl:use-attribute-sets attribute.

All attributes from all the sets are applied to the element. If more than one attribute set defines the same attribute, the last attribute set mentioned is used. If there is more than one attribute set with the same name, the attributes in the sets are merged. You can also include attribute sets in particular elements by adding a use-attribute-sets element to an xsl:element , xsl:copy , or xsl:attribute-set element, as in the following example:.

The xsl: prefix is unnecessary and in fact prohibited when use-attribute-sets is an attribute of an XSLT element rather than a literal result element.

The xsl:processing-instruction element places a processing instruction in the output document. The target of the processing instruction is specified by a required name attribute. The contents of the xsl:processing-instruction element become the contents of the processing instruction. The contents of the xsl:processing-instruction element can include xsl:value-of elements and xsl:apply-templates elements, provided the result of these instructions is pure text.

The xsl:processing-instruction element cannot contain xsl:element and other instructions that produce elements and attributes in the result.

Furthermore, xsl:processing-instruction cannot include any instructions or literal text that insert a? The xsl:comment element inserts a comment in the output document. It has no attributes. Its contents are the text of the comment, as in the following example:. The contents of the xsl:comment element can include xsl:value-of elements and xsl:apply-templates elements, provided the results of these instructions are pure text.

It cannot contain xsl:element and other instructions that produce elements and attributes in the result. Furthermore, xsl:comment cannot include any instructions or literal text that inserts a double hyphen in the comment. This would result in a malformed comment in the output. The xsl:text element inserts its contents into the output document as literal text. However, xsl:text does have a couple of advantages.

The first is that it preserves white space exactly, even if the node contains nothing but white space. By default, XSLT processors delete all text nodes from the style sheet that contain only white space. Thus, this element is useful when dealing with concrete poetry, computer source code, or other text in which white space is significant.

This can be useful when you need to include JavaScript source code in the output document, as in the following example:. This may produce output that is not well-formed XML. Note, however, that the style sheet and the input document are both still well-formed XML. The xsl:copy element copies the source node into the output tree. Child elements, attributes, and other content are not automatically copied.

However, the contents of the xsl:copy element are a template that can select these things to be copied as well. This is often useful when transforming a document from one markup vocabulary to the same or a closely related markup vocabulary.

For example, this template rule strips the attributes and child elements off an ATOM and replaces it with the value of its contents enclosed in a B element:. One useful template xsl:copy makes possible is the identity transformation; that is, a transformation from a document into itself.

Such a transformation looks like this:. You can adjust the identity transformation a little to produce similar documents. For example, Listing is a style sheet that strips comments from a document, leaving the document otherwise untouched. It simply omits the comment node test from the match and select attribute values in the identity transformation. In other words, it is a shallow copy. To deep copy the entire node including all its attributes and descendants, use xsl:copy-of.

The select attribute of xsl:copy-of chooses the nodes to be copied. Listings and are examples of XSL transformations from a source vocabulary to the same vocabulary. Unlike most of the examples in this chapter, they do not transform to well-formed HTML. The xsl:number element inserts a formatted integer into the output document. The value of the integer is given by the value attribute. This contains a number, which is rounded to the nearest integer, then formatted according to the value of the format attribute.

Reasonable defaults are provided for both these attributes. Each element is matched with its atomic number. The value attribute can contain any data that XPath knows how to convert to a number. However, if the value attribute is omitted, the position of the current node in the source tree is used as the number. For example, consider Listing , which produces a table of atoms that have boiling points less than or equal to the boiling point of nitrogen.

Figure shows the finished table produced by applying this style sheet to the complete periodic table. This shows that the default value calculated by xsl:number is the position of the node among other sibling nodes of the same type ATOM elements in this case.

You can change what xsl:number counts using these three attributes:. For example, the page numbers in the preface and other front matter of books often appear in small Roman numerals such as i, ii, iii, iv, and so on.

And different countries use different conventions to group the digits, separate the integer and fractional parts of a real number, and represent the symbols for the various digits.

These are all adjustable through four attributes of xsl:number :. You can adjust the numbering style used by xsl:number using the format attribute.

This attribute generally has one of the following values:. You can specify decimal numbering with leading zeros by including the number of leading zeros you want in the format attribute. You might find this useful when lining numbers up in columns. The letter-value attribute distinguishes between letters interpreted as numbers and letters interpreted as letters. The keyword traditional specifies a numeric sequence, as in the following example:.

In the United States, we tend to write large numbers with commas grouping every three digits; for example, 4,,, However, in many languages and countries, a period or a space separates the groups instead; for instance, 4. The grouping-separator attribute specifies the grouping separator used between groups of digits.

The grouping-size attribute specifies the number of digits used in a group, as in the following example:. The xsl:sort element sorts the output nodes into a different order than they were generated in. An xsl:sort element appears as a child of an xsl:apply-templates element or xsl:for-each element. By default, sorting is performed in alphabetical order of the keys. If more than one xsl:sort element is present in a given xsl:apply-templates or xsl:for-each element, the elements are sorted first by the first key, then by the second key, and so on.

If any elements still compare equally, they are output in the order they appear in the source document. For example, suppose you have a file full of ATOM elements arranged alphabetically. To sort by atomic number, you can use the style sheet in Listing Figure shows the limits of alphabetical sorting.

Hydrogen, atomic number 1, is the first element. However, the second element is not helium, atomic number 2, but rather neon, atomic number Although 10 sorts after 9 numerically, alphabetically 10 falls before 2. You can, however, adjust the order of the sort by setting the optional data-type attribute to the value number , as in this element:.

You can change the order of the sort from the default ascending order to descending by setting the order attribute to descending , like this:. This sorts the elements from the largest atomic number to the smallest so that hydrogen now appears last in the list.

Alphabetical sorting naturally depends on the alphabet. The lang attribute can set the language of the keys. The value of this attribute should be an ISO language code such as en for English. However, processors are not required to know how to sort in all the different languages that might be encountered in XML. While English sorting is fairly straightforward, many other languages require much more complicated algorithms.

Indeed, a few languages actually have multiple standard ways of sorting based on different criteria. The lang attribute is ignored if data-type is number. These are the same values supported by the xml:lang attribute discussed in Chapter 6. Finally, you can set the case-order attribute to one of the two values, upper-first or lower-first , to specify whether uppercase letters sort before lowercase letters or vice versa.

The default depends on the language. Sometimes you want to include the same content from the source document in the output document multiple times.

However, suppose you want the data to be formatted differently in different locations? For example, suppose you want the output of processing the periodic table to be a series of links to more detailed descriptions of the individual atoms. In this case, the output document would start like this:.

This sort of application is common anytime you automatically generate a table of contents or an index. The NAME of the atom must be formatted differently in the table of contents than in the body of the document. You need two different rules that both apply to the ATOM element at different places in the document. The solution is to give each of the different rules a mode attribute.

Then you can choose which template to apply by setting the mode attribute of the xsl:apply-templates element. Listing demonstrates. The default template rule for nodes preserves modes. That is, for every mode n you declare in your style sheet, the XSLT processor adds one template rule that applies specifically to that mode and looks like this:. Named constants help clean up code.

They can replace commonly used boilerplate text with a simple name and reference. They can also make it easy to adjust boilerplate text that appears in multiple locations by simply changing the constant definition. The xsl:variable element defines a named string for use elsewhere in the style sheet via an attribute value template.

It has a single attribute, name, which provides a name by which the variable can be referred to. The contents of the xsl:variable element provide the replacement text. For example, this xsl:variable element defines a variable with the name copy04 and the value Copyright Elliotte Rusty Harold :. To access the value of this variable, you prefix a dollar sign to the name of the variable. To insert this in an attribute, use an attribute value template.

The contents of the xsl:variable element can contain markup including other XSLT instructions. This means that you can calculate the value of a variable based on other information, including the value of other variables. However, a variable may not refer to itself recursively, either directly or indirectly. For instance, the following example is in error:. A variable present at the top level of a style sheet can be accessed anywhere in the style sheet. That is, it only applies inside that one template rule.

It is local to the template. Local variables override global variables with the same name. Local variables can also override other local variables. In the event of a conflict between two variables with the same name, the closest local variable with the same name is used. Unlike variables in traditional programming languages such as Java, XSLT variables may not be changed. After the value of a variable has been set, it cannot be changed. It can be shadowed by another variable with the same name in a more local scope, but its own value is fixed.

An XSLT variable is more like an algebraic variable than a programming language variable. Variables are limited to basic text and markup. XSLT provides a more powerful macro facility that can wrap standard markup and text around changing data. In other words, you want the output to look like this:. This markup can be repeated inside other template rules.

When the detailed markup grows more complex, and when it appears in several different places in a style sheet, you may elect to turn it into a named template. Named templates resemble variables. However, they enable you to include data from the place where the template is applied, rather than merely inserting fixed text. The xsl:template element can have a name attribute by which it can be explicitly invoked, even when it isn't applied indirectly. For example, this shows a sample named template for the preceding pattern:.

The xsl:call-template element appears in the contents of a template rule. It has a required name argument that names the template it will call. When processed, the xsl:call-template element is replaced by the contents of the xsl:template element it names.

Named templates also have the advantage, like variables, of factoring out common patterns in the style sheet so that you can edit them as one. Verbal Ability. Interview Questions. Company Questions.

Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. The figure below shows a rendering of the Formatting Objects generated by the XML file and the stylesheet. The XSL-FO vocabulary is designed to be displayed on a wide variety of media: screen, paper, or even voice. The full PDF document is also available. They are likely to co-exist since they meet different needs. XSL is intended for complex formatting where the content of the document might be displayed in multiple places; for example the text of a heading might also appear in a dynamically generated table of contents.

CSS is intended for dynamic formatting of online documents for multiple media; its strictly declarative nature limits its capabilities but also makes it efficient and easy to generate and modify in the content-generation workflow.



0コメント

  • 1000 / 1000