SharePoint content customization-advanced XSLT usage-function calls with return values

Source: Internet
Author: User
Tags xslt

Generally, XSLT is used for simple data presentation. However, complicated logic processing becomes impossible. In fact, XSLT itself supports custom functions in JavaScript, C #, and other languages. Unfortunately, SharePoint relentlessly blocks this feature. So for more implementation functions, we have to explore some special uses of xsl: template-function calls with return values, so that it has the flexibility of other programming languages.

 

Xsl: template is not a function. In most cases, it is only a modular tool for content output. However, with the help of the xsl: variable tag, we can obtain the xsl: template output content, perform related operations on it, so as to implement function call simulation with return values, on this basis to complete more complex logic processing, make up for the lack of support for XSLT udfs in SharePoint. At the same time, problems caused by the inability to dynamically assign values to XSLT variables can also be solved. For more information, see the following example.

 

 

1. Simple processing of strings.

Purpose: extract the title information from the Lookup field type. (Note: The value format of the Lookup field type is "ItemId; # ItemTitle", for example, "2; # Hello, world", so here we need to extract "Hello, world)

 

1. Define the template as a "function ".

<Xsl: template name = "ExtractUrl"> <xsl: param name = "input"/> <! -Intercept the string after "; #" --> <xsl: value-of select = "substring-after ($ input, '; #')"/> </xsl: template>

 

2. Call the "function" and process the value of this Lookup1 field, and return the URL value to the variable ExtractedUrl.

<xsl:variable name="ExtractedUrl">    <xsl:call-template name="ExtractUrl">        <xsl:with-param name="input" select="@Lookup1"/>    </xsl:call-template></xsl:variable>

 

3. Execution result

ExtractedUrl variable value:

Hello, world

 

 

2. Implement recursive/cyclic calls

Purpose: format the Contacts value ("NA, NB, NC") as an XML string using "," as the separator.

 

1. Call the "function" recursively to format the string in XML format.

<Xsl: template name = "XmlWrapper"> <xsl: param name = "input"/> <xsl: choose> <xsl: when test = "contains ($ input ,', ') "> <! -Output the string before the first "," (item is a custom tag and can be replaced with any other tag) --> <item> <xsl: value-of select = "substring-before ($ input, ',')"/> </item> <! -Recursively process the remaining string --> <xsl: call-template name = "XmlWrapper"> <xsl: with-param name = "input" select = "substring-after ($ input, ',') "/> </xsl: call-template> </xsl: when> <xsl: otherwise> <! -Directly output content without "," --> <item> <xsl: value-of select = "$ input"/> </item> </xsl: otherwise> </xsl: choose> </xsl: template>

 

2. Call the "function"

<Xsl: variable name = "ContactsXml"> <! -Output the content to the contacts tag --> <contacts> <xsl: call-template name = "XmlWrapper"> <xsl: with-param name = "input" select = "@ Contacts"/> </xsl: call-template> </contacts> </xsl: variable>

 

3. Execution result

ContactsXml variable value:

<contacts>    <item>NA</item>    <item>NB</item>    <item>NC</item></contacts>

 

 

3. format the string information into XML data.

Purpose: Use "," as the separator to bind the "NA, NB, NC" string to the drop-down menu control.

  1. In combination with the preceding content, define the "function" to convert the XML string into queryable XML data, and bind the data to the drop-down menu control cyclically.
<Xsl: template name = "GenerateDDL"> <xsl: param name = "input"/> <! -Format the original string as an XML string --> <xsl: variable name = "ContactsXml"> <contacts> <xsl: call-template name = "XmlWrapper"> <xsl: with-param name = "input" select = "@ Contacts"/> </xsl: call-template> </contacts> </xsl: variable> <select id = "DDL-{generate-id ()}"> <! -Msxsl: the node-set () function converts an XML string into queryable XML data --> <xsl: for-each select = "msxsl: node-set ($ ContactsXml) /contacts/item "> <option> <xsl: value-of select = ". "/> </option> </xsl: for-each> </select> </xsl: template>

 

2. Call the "function ".

<xsl:call-template name="GenerateDDL">    <xsl:with-param name="input" select="@Contacts"/></xsl:call-template>

 

3. output results

<select id="DDL-ID0EAAA">    <option>NA</option>    <option>NB</option>    <option>NC</option></select>

 

 

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.