Deep XSL (4)---template (rpm)

Source: Internet
Author: User
Tags define expression string xsl domain
Template Depth xsl (4)
---template
Translation: Sun Yizhong

1 overview

When a rule applied to a source element is determined, the template for that rule is implemented. A template can contain the elements of a literal result, character data, and instructions that produce a tree portion of the result. Directives are represented by elements in the XSL Name field and can be processed by selecting descendant elements. There are two types of such directives, Xsl:process-children and xsl:process; The xsl:process-children instruction handles the neighboring child elements of the source element, while the xsl:process instruction handles the elements selected by the specified pattern. See the following example:

<xsl:template match= "Chapter/title" >
<fo:rule-graphic/>
<fo:block space-before= "2pt" >
<xsl:text>chapter </xsl:text>
<xsl:number/>
<xsl:text&gt: </xsl:text>
<xsl:process-children/>
</fo:block>
<fo:rule-graphic/>
</xsl:template>


2 text result elements

In a template, element materialization in a style sheet that is not part of an XSL name field will produce nodes of the same type, and the resulting element nodes will have properties that are already specified for the elements in the template tree. The value of an attribute of a literal result element is considered to be a property value template: A string that can be contained in curly braces ({}). The name Domain prefix mapping of the resulting element node is the mapping of the URI that is mapped to the XSL Name field in the stylesheet. Because the XSL processor is only for elements that belong to an XSL name field, there is the problem of how to create a new element that belongs to an XSL name field. A URI is a http://www.w3.org/TR/WD-xsl name field if one or more of the/quote immediately appears to be the referenced name domain. Application name fields will be treated as special.


3 named attribute set

The Xsl:define-attribute-set element defines a collection of named properties. The "name" attribute sets the name of the property set. The content of the Xsl:define-attribute-set element is a xsl:attribute-set element that specifies the attribute. A literal result element or a XSL:ATTRIBUTE-SET element can specify an attribute set name to be xsl: The value of the Use property. The following example produces a set of properties called Title-style and uses it in template rules.

<xsl:define-attribute-set name= "Title-style" >
<xsl:attribute-set font-size= "12pt" font-weight= "bold"/>
</xsl:define-attribute-set>
<xsl:template match= "Chapter/heading" >
<fo:block xsl:use= "Title-style" quadding= "Start" >
<xsl:process-children/>
</fo:block>
</xsl:template>


4 text in the template

The template can also contain pcdata (parsed Character Data). Each data character that is removed from a space in the template produces a data character in the result tree. The data character of the literal can also be wrapped in a xsl:text element. Such packaging processing may change the removal of whitespace without affecting the processing of characters by the XSL processor.


5 Processing of Xsl:process-children

The following example creates a new block for the chapter element and processes its adjacent child elements.

<xsl:template match= "chapter" >
<fo:block>
<xsl:process-children/>
</fo:block>
</xsl:template>

The xsl:process-children instruction handles all child nodes of the current node, including characters. The processing of characters in the source tree is the addition of characters to the result tree. Therefore, the < tag in the source tree represents the < character, which is converted from the built-in template rule to the < character in the result tree, and the,< character is also represented as < when the result tree is materialized as an XML document.


6 Processing of Xsl:process

The xsl:process element handles elements selected by a pattern. The pattern of the xsl:process element is a selection pattern, so it is indirectly positioned to the current node. The following example handles all author subnodes of Author-group:

<xsl:template match= "Author-group" >
<fo:sequence>
<xsl:process select= "Author"/>
</fo:sequence>
</xsl:template>

The xsl:process element handles all elements that match the specified pattern. Character data is not matched by xsl:process elements. A pattern cannot contain a property pattern (Attributepattern) unless it is part of a property qualification (Attributequalifier) . The pattern controls the depth at which the match occurred. The following example processes the First-name elements in all author nodes:

<xsl:template match= "Author-group" >
<fo:sequence>
<xsl:process select= "Author/first-name"/>
</fo:sequence>
</xsl:template>

Use the//operator in the pattern to match any depth. The following example handles all the heading elements in the book element.

<xsl:template match= "book" >
<fo:block>
<xsl:process select= ".//heading"/>
</fo:block>
</xsl:template>


7 Direct processing

When the result is a known rule structure, it is useful to be able to directly determine the template for the selection element. The Xsl:for-each element includes a template that implements each selection element specified by the Select attribute. For example, for the following XML document:

<customers>
<customer>
<name>...</name>
<order>...</order>
<order>...</order>
</customer>
<customer>
<name>...</name>
<order>...</order>
<order>...</order>
</customer>
</customers>

The following XSL generates an HTML document, including a table in which one row is a custom element

<xsl:template match= "/" >
<HTML>
<HEAD>
<TITLE>Customers</TITLE>
</HEAD>
<BODY>
<TABLE>
<TBODY>
<xsl:for-each select= "Customers/customer" >
<TR>
<TH>
<xsl:process select= "Name"/>
</TH>
<xsl:for-each select= "Order" >
<TD>
<xsl:process-children/>
</TD>
</xsl:for-each>
</TR>
</xsl:for-each>
</TBODY>
</TABLE>
</BODY>
</HTML>
</xsl:template>


8 Conditions in the template

There are two directives in the XSL to support conditional processing: xsl:if and Xsl:choose. The xsl:if instruction provides simple if-then condition selection; Xsl:choose supports multiple conditions of choice.


9 calculation of the resulting text

In a template, the xsl:value-of element can be used to compute the resulting text, such as the value of extracting text from the source tree or inserting character constants. It is implemented by the xsl:value-of element through a string expression that is defined as the value of the expr attribute. A string expression can also be used in the property value of a literal result element, as long as the string expression is nested in {}.

10 macros

A macro can produce a result set that can also be referenced as a single object. In the following example, a macro is defined for a encapsulated paragraph, and the "warning!" is added before its contents Statement. The macro is referenced in the rule that matches the warning element.

<xsl:define-macro name= "Warning-para" >
<fo:box>
<fo:block>
<xsl:text>warning! </xsl:text>
<xsl:contents/>
</fo:block>
</fo:box>
</xsl:define-macro>
<xsl:template match= "Warning" >
<xsl:invoke macro= "Warning-para" >
<xsl:process-children/>
</xsl-invoke>
</xsl:template>



-------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.