Msxsl: Application of script tag in XSLT

Source: Internet
Author: User
Tags xpath functions xslt

XSLT is applied in parsing XML files. In Asp.net programming, you can use the XML control to specify the documentsource attribute to load the XML file and specify the transformsource attribute to load the XSLT file, the XML control automatically displays the converted results on the page. XSLT also supports many XPath functions and some simple logical statements, such as if, choose-when-otherwise, and for-each, in addition, in XSLT, you can also customize methods (called templates in XSLT) by otherCodeFor more information about XSLT, see the w3school XSLT tutorial.

Since the emergence of Microsoft's Moss, XSLT has become more and more widely used. Sometimes, we also encounter the problem of interaction between XSLT and JavaScript and some server-side languages, for example, you need to determine whether to output content on the page based on the value of a variable in JavaScript code. At this time, you need to use the msxsl: script mark in XSLT.

Msxsl: script markup is supported by Microsoft. It can support several different languages, such as C # and VB. net, JScript and so on, the specific use method and introduction can refer to the msdn, http://msdn.microsoft.com/en-us/library/533texsx (vs.71 ). aspx. In addition, here is an articleArticleIt also details how to use msxsl: script tag in XSLT, http://blog.joycode.com/saucer/archive/2004/05/12/2%3.joy.

Basically, we can directly use the common methods and types defined in the msxsl: script tag in XSLT. If it is a server-side language, make sure that the methods or types are public, in addition, the type of the parameter or return value must be the same when it is passed and received (some parameter types are not supported in XSLT, and we may use the nodelist type in most cases ). Of course, when using the server language,ProgramIt is also necessary to reference a set and namespace. Here is an example:

< Msxsl: script Language = "C #" Implements-Prefix = "User" >
< Msxsl: Using Namespace = "System. Web"   />
< Msxsl: Assembly Name = "System. Web"   />
Public bool matchcurrurl (string LOC)
{
Return httpcontext. Current. Request. url. tostring (). tolower (). indexof (loc. tolower ())! =-1;
}
</ Msxsl: script >

Msxsl: Using markup and msxsl: Assembly markup are used to reference namespaces and assemblies respectively. Theoretically they can be written anywhere in the XSLT file. Msxsl: the script mark is identified as the C # language, and the httpcontext global object is used in the matchcurrurl method. This object belongs to the system. web namespace. Therefore, you must add the Assembly reference and namespace before use. Do not expect that XSLT will automatically load the Assembly and namespace references for you, Because msxsl: script markup is not W3C standard and you need to declare it before using it, as shown in the following two lines:

Xmlns: msxsl = "urn: Schemas-Microsoft-com: XSLT"
Xmlns: User = "urn: My-scripts"

If the msxsl: script tag uses a client language, such as JavaScript, do not use client browser objects, such as Windows and document, because XSLT is parsed on the server side, the server does not know the behavior of the client. In msxsl: the javascript code written in the script tag should be mapped to the server method to be correctly parsed. If you want to obtain the page address accessed by the client during the interaction process, the best way is to use httpcontext in C. current. request. URL instead of using document in JavaScript. location or window. location.

Using the msxsl: script tag can save a lot of trouble in the development process. According to the traditional development habits, if we want to insert C # code in XSLT, the general practice is to write the code in the user control, and then output the reference tag of the user control in XSLT. If you directly use the msxsl: script tag, you can write all the C # code in it, saving the trouble of Writing user controls!

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.