Main XSLT Elements

Source: Internet
Author: User
Tags xsl xsl file xslt
1. the XSL style sheet consists of one or more rules called templates.

Each template contains the rules applied when a specified node is matched.

2. <XSL: Template> element

<XSL: Template> elements are used to build templates.

The match attribute is used to associate XML elements and templates. The match attribute can also be used to define a template for the entire document. The value of the match attribute is an XPATH expression (for example, match = "/" defines the entire document ).

If multiple processing methods are defined for the same element, use mode to differentiate them.

<XSL: template match = "/">

<Table border = "1">

<XSL: Apply-templates select = "employees/employee" mode = "table"/>

</Table>

<XSL: Apply-templates select = "employees/employee" mode = "list"/>

</XSL: Template>

<XSL: template match = "employee" mode = "table">

<Tr>

<TD> <XSL: value-of select = "name"/> </TD>

<TD> <XSL: value-of select = "Age"/> </TD>

<TD> <XSL: value-of select = "monthly_pay"/> </TD>

</Tr>

</XSL: Template>

<XSL: template match = "employee" mode = "list">

<Ul>

<Li> <XSL: value-of select = "name"/> </LI>

<Li> <XSL: value-of select = "Age"/> </LI>

<Li> <XSL: value-of select = "monthly_pay"/> </LI>

</Ul>

</XSL: Template>

Note: according to the actual test, if a node element does not define its template, or its template is empty, <XSL: template match = "element name"/>, the child node under it will not be processed (even if a template is defined for the child node ).

3. The <XSL: value-of> element is used to extract the value of a selected node.

<XSL: value-of> element is used to extract the value of a selected node and add the value to the converted output stream:

<TD> <XSL: value-of select = "catalog/CD/Title"/> </TD>

<TD> <XSL: value-of select = "catalog/CD/artist"/> </TD>

4. The <XSL: For-each> element allows you to cycle in XSLT.

<XSL: For-each> the element can be used to select each XML Element in the specified node set.

<XSL: For-each select = "catalog/CD">

<Tr>

<TD> <XSL: value-of select = "title"/> </TD>

<TD> <XSL: value-of select = "artist"/> </TD>

</Tr>

</XSL: For-each>

5. The <XSL: Sort> element is used to sort the results.

To sort the results, simply add the <XSL: Sort> element to the <XSL: For-each> element in the XSL file:

<XSL: For-each select = "catalog/CD">

<XSL: Sort select = "artist"/>

<Tr>

<TD> <XSL: value-of select = "title"/> </TD>

<TD> <XSL: value-of select = "artist"/> </TD>

</Tr>

</XSL: For-each>

6. <XSL: If> element

<XSL: If> elements are used for conditional testing of XML file content.

Syntax:

<XSL: If test = "expression">

...

... If the condition is true, the output...

...

</XSL: If>

To add a conditional test, add the <XSL: If> element in the <XSL: For-each> element in the XSL file:

<XSL: For-each select = "catalog/CD">

<XSL: If test = "Price & gt; 10">

<Tr>

<TD> <XSL: value-of select = "title"/> </TD>

<TD> <XSL: value-of select = "artist"/> </TD>

</Tr>

</XSL: If>

</XSL: For-each>

7. <XSL: Choose> element

The XSLT <XSL: Choose> element is used to combine <XSL: When> and <XSL: otherwise> to express multiple conditional tests.

Syntax:

<XSL: Choose>
<XSL: When test = "expression">
... Output...
</XSL: When>
<XSL: otherwise>
... Output ....
</XSL: otherwise>
</XSL: Choose>

8. <XSL: Apply-templates> element

The <XSL: Apply-templates> element can apply a template to the current element or all child nodes of the current element and add a select attribute to the <XSL: Apply-templates> element, this element only processes child elements that match the property value. Otherwise, all child elements and the current element are processed.

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<XSL: stylesheetversion = "1.0"

Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

<XSL: template match = "/">

<HTML>

<Body>

<H2> my CD collection </H2>

<XSL: Apply-templates/>

</Body>

</Html>

</XSL: Template>

<XSL: template match = "cd">

<P>

<XSL: Apply-templates select = "title"/>

<XSL: Apply-templates select = "artist"/>

</P>

</XSL: Template>

<XSL: template match = "title">

Title: <span style = "color: # ff0000">

<XSL: value-of select = "."/> </span>

<Br/>

</XSL: Template>

<XSL: template match = "artist">

Artist: <span style = "color: #00ff00">

<XSL: value-of select = "."/> </span>

<Br/>

</XSL: Template>

</XSL: stylesheet>

Note: If the direct subnode of the current node does not have a matching template, the text of this element is output by default.

If no matching template exists for the direct subnode of the current node, the processor selects the template of its grandson node for processing.

9. XSLT <XSL: attribute> Elements

Adds an attribute to an output element (an element created by a non-XSL: element.

Note: The <XSL: attribute> element replaces existing attributes with the same name.

<XSL: attribute name = "attributename" namespace = "Uri">
<! -- Content: Template -->
</XSL: attribute>

The name attribute is required, and the namespace is optional.

<XSL: Attribute-set name = "font">

<XSL: attribute name = "fname"> Arial </XSL: attribute>

<XSL: attribute name = "size"> 14px </XSL: attribute>

<XSL: attribute name = "color"> Red </XSL: attribute>

</XSL: Attribute-set>

Example:

<Test>

<XSL: attribute name = "addattribute"> testattribute </XSL: attribute>

</Test>

10. The <XSL: Attribute-set> element can create a named attribute set. This attribute set can be applied as a whole to the output document.

<XSL: Attribute-Set

Name = "name" use-Attribute-sets = "name-list">

<! -- Content: XSL: attribute * -->

</XSL: Attribute-set>

The name attribute is required, while the use-Attribute-sets attribute is optional. Other attribute set definitions are referenced. Multiple Attribute Sets are separated by spaces.

11. The <XSL: Call-template> element can use a specified template.

<XSL: Call-Template Name = "templatename">

<! -- Content: XSL: With-Param * -->

</XSL: Call-template>

If you want to call the template directly, you do not need to match nodes or attributes. That is to say, it ignores the matching pattern of the called template (the Template Name Is templatename ).

Unlike the apply-template element, it calls the specified template (based on matching conditions) for all the child elements of the current element ).

Or: The apply-template method calls the template from the perspective of element matching, while the call-template method calls the template according to the Template Name.

Comment: Unlike apply-template, it cyclically calls the corresponding template for each select-compliant element. Call-template is more like a function call because it can pass parameters, you can use <XSL: With-param> to pass the elements that need to be processed by the called template.

12. The <XSL: With-param> element defines the parameter values passed to the template.

Note: the value of the name attribute of the <XSL: With-param> element must match the name in the <XSL: param> element. Otherwise, <XSL: with-param> element ,.

<XSL: With-Param name = "name" select = "expression">

<! -- Content: Template -->

</XSL: With-param>

13. The <XSL: param> element is used to declare local or global parameters.

<XSL: Param

Name = "name"

Select = "expression">

<! -- Content: Template -->

</XSL: param>

Here, name is required, while select is XPath, which is the default value of the parameter.

<? XML version = "1.0" encoding = "ISO-8859-1"?>
<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: variable name = "XX">
<HTML>
<Body>
<XSL: Call-Template Name = "show_title">
<XSL: With-Param name = "title"/>
</XSL: Call-template>
</Body>
</Html>
</XSL: Variable>
<XSL: Template Name = "show_title" match = "/">
<XSL: Param name = "title"/>
<XSL: For-each select = "catalog/CD">
<P> title: <XSL: value-of select = "$ title"/> </P>
</XSL: For-each>
</XSL: Template>
</XSL: stylesheet>

Both <XSL: value-of and <XSL: For-each> use the $ variable name to reference the parameter value.

Note: the value specified on the <XSL: param> element is the default value for binding. If the called template or style sheet contains <XSL: param>, the passed parameter replaces the default value.

The <XSL: param> element must be declared as the direct sub-level of the <XSL: Template> element. If it is not declared as a direct child, the value of the <XSL: param> element cannot be accessed and an error occurs.

14. <XSL: Element> element

<XSL: Element> an element is used to create an element node in the output document.

<XSL: Element
Name = "name"
Namespace = "Uri"
Use-Attribute-sets = "namelist">
<! -- Content: Template -->
</XSL: Element>

Name is required, namespace, use-Attribute-sets are optional, and name and namespace can be form-based, and the result is calculated by the processor at runtime.

Note: For an element created by XSL: element, you must use-Attribute-sets to add attributes for it, rather than directly using XSL: attribute to add attributes for it.

15. <XSL: Message> element

Definition and usage

<XSL: Message> you can write a message to the output. This element is mainly used to report errors.

This element can contain almost any other XSL elements (<XSL: Text>, <XSL: value-of>, and so on ).

The terminate attribute allows you to choose whether to terminate the conversion when an error occurs.

<XSL: Message terminate = "Yes | no">

<! -- Content: Template -->

</XSL: Message>

Output Message: The Output Message is displayed in red on the console instead of in the output document.

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.