Xue XSL (8)

Source: Internet
Author: User
Tags xsl xsl file
Sometimes, we may want to add some statistical information or numbers to the content in XML Document Output, so it is not easy to use the previous knowledge. Today we will introduce two new elements: <XSL: eval> and <XSL: SCRIPT>, so that we can easily deal with this problem.

<XSL: eval>

Meaning:

Calculates the script expression and outputs a text string.

Syntax:

<XSL: eval Language = "language-name">

Attribute:

Language-specifies the name of the scripting language used. The available attributes include "JavaScript", "jscript", "VBScript", and "vbs". The default value is "jscript ".

<XSL: SCRIPT>

Meaning:

Declare global variables or define functions.

Syntax:

<XSL: script language = "language-name">

Attribute:

Same as <XSL: eval>

 

Example:

I wonder if you are still impressed with the examples in the fourth issue of "Learning XML? The XML document does not have a resume number, but the output contains an uppercase sequence number. Today, we will give a more complex example:

If we compile a year-end production statistical table, which requires a subtotal item, the general practice is to calculate it in advance, and now it is not necessary, we can only give a single statistical item, returns the subtotal item when it is displayed. Find out the fifth part of "Learn XML". The XML file does not need to be modified. The modifications to the XSL file are as follows:

......

<XSL: template match = "document">

......

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

<Tr> <TD> subtotal </TD>

<TD> <XSL: eval> total (this, "Q1") </XSL: eval> </TD>

<TD> <XSL: eval> total (this, "Q2") </XSL: eval> </TD>

<TD> <XSL: eval> total (this, "Q3") </XSL: eval> </TD>

<TD> <XSL: eval> total (this, "Q4") </XSL: eval> </TD>

</Tr>

</Table>

<XSL: SCRIPT>

Function total (node, q ){

Temp = 0;

Mark = '/document/report/' + q;

V = node. selectnodes (Mark );

For (t = V. nextnode (); t = V. nextnode ()){

Temp + = Number (T. Text );

}

Return temp; // subtotal Value

}

</XSL: SCRIPT>

</XSL: Template>

Note:

The add part is the add part. Note that the add part is divided into two parts: <XSL: SCRIPT> </XSL: SCRIPT> must be placed after </table>. Remember.

Selectnodes () ── it is a method of xmldomobject. It returns a set of all nodes that meet the conditions in the document. The conditions are written in the same way as the values of <XSL: For-each> and select attributes, you can add filtering and subscript restrictions, such:

Find teams with a first quarter output greater than or equal to 50

/Document/report/q1 [value () $ Ge $50]

The above method also provides a simpler method.

// Q1 [value () $ Ge $50]

// It indicates that all nodes are traversed from the root node to find the nodes that meet the conditions. If there are nodes with the same name but different meanings in the document, this method cannot be used. For example, if you want to count the total annual output, you can search for the node using the following string (we recommend that you use the last one, which will precisely find the data to be summarized)

// * [Value () $ GT $0]

Or

// (Q1 | Q2 | Q3 | Q4)

Or

/Document/report/(Q1 | Q2 | Q3 | Q4)

 

Nextnode () -- return the next node in the node set

Number () -- convert the provided parameter to a value

 

Next, we will introduce XSL function 2, which is used in <XSL: SCRIPT> and <XSL: eval>, as well as the expr attributes of <XSL: If> and <XSL: When>. It is recommended that you be familiar with at least one of JavaScript, JScript, and VBScript. If you can use XSL, the work will be very limited. Due to the length, we will not detail it here.

 

(This is now available and will be added later)

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.