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>




Grammar:



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




Property:


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.


Example:

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"?>


<document>


<report>


<class>

Jia ban

</class><q1>50</q1><q2>70</q2>


<q3>30</q3><q4>10</q4></report>


<report><class>

Class B

</class><q1>20</q1><q2>30</q2>


<q3>40</q3><q4>50</q4></report>


<report><class>

Class C

</class><q1>70</q1><q2>40</q2>


<q3>20</q3><q4>10</q4></report>


</document>



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://www.w3.org/TR/WD-xsl" >


<xsl:template match= "/" >





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




</HTML>


</xsl:template>


<xsl:template match= "Document" >




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


<TH> Team </TH><TH>

First quarter

</TH><TH>

Second quarter

</TH>




<TH>

Third quarter

</TH><TH>

Four quarters

</TH>



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



</TABLE>


</xsl:template>


<xsl:template match= "the" >


<TR>


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


</TR>


</xsl:template>


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


</xsl:if>


<xsl:value-of/>


</xsl:template>


</xsl:stylesheet>





Description


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