Follow me to learn XSL (iv)

Source: Internet
Author: User
Tags filter expression version xsl
Test mode

One of the advantages of XML technology is the selectivity of data output, that is, to select the required data output. The selection pattern statement:<xsl:for-each>, <xsl:value-of>, and <xsl:apply-template> that we talked about earlier simply select the nodes that arrive through the "/" symbol layer. If we do not need all of the XML data output, and only need to meet a certain condition of some of the data, "radish greens, to do everything", then conditional judgment <xsl:if> and multiple conditions of judgment <xsl:choose> and <xsl:when> To cater to this need, if you are familiar with the design, you will find them familiar.

If in XSL, first, introduce the syntax structure of the XSL element <xsl:if>:



<xsl:if expr= "script-expression" language= "language-name" test= "pattern" >


expr── a script language expression that evaluates to True or false, and if the result is true, and the content is displayed in the output (this property can be omitted).

The script language type of the expression in the Language──expr property is the same as the value of the language property of the HTML tag script, which defaults to the "JScript" test── source data test condition.


This is in the case of a report named Report.xml, which reads as follows:

<?xml version= "1.0" encoding= "GB2312"?>

<?xml:stylesheet type= "text/xsl" href= "report.xsl"?>




Jia ban




Class B




Class C




We use the XSL template in conjunction with the <xsl:if&gt we learned today, and write an XSL document for it, requiring a quarterly yield of less than or equal to 20 in red, the file name is report.xsl and the contents are as follows:

<?xml version= "1.0" encoding= "GB2312"?>

<xsl:stylesheet xmlns:xsl= "Http://" >

<xsl:template match= "/" >

<body><xsl:apply-templates select= "document;" /></body>



<xsl:template match= "Document" >

<table border= "1" cellspacing= "0" >

<TH> Team </TH><TH>

First quarter


Second quarter



Third quarter


Four quarters


<xsl:apply-templates select= "The/>"



<xsl:template match= "the" >


<td><xsl:value-of select= "Class"/></td>

<td><xsl:apply-templates select= "Q1"/></td>

<td><xsl:apply-templates select= "Q2"/></td>

<td><xsl:apply-templates select= "Q3"/></td>

<td><xsl:apply-templates select= "Q4"/></td>



<xsl:template match= "Q1|q2|q3|q4" >

<!--here to test output, such as less than or equal to 20, add a style property color with red (red)-->

<xsl:if test= ". [Value () $le $] >

<xsl:attribute name= "Style" >color:red</xsl:attribute>






q1|q2|q3|q4── tags q1, q2, Q3, Q3 Use this template to determine the output

$le $── is "less than equal" in the relational operator, other relationships are less than ($LT $), greater than ($GT $), greater than or equal to ($ge $), equal to ($eq $), not equal to ($ne $), etc.

. That refers to the current tag

[]── represents a filter, only the tags that meet the filter criteria can be selected

Value () ──xsl function, other common XSL functions have text (), end (), index (), and so on.

In the next phase, we will learn about the other three elements of XSL that can be tested multiple times for the same data, producing the corresponding output according to different conditions.

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: 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.