Extending XSLT using C # use custom functions

Source: Internet
Author: User
Tags xsl xslt

Use custom functions in xslt

1. the xml file remains unchanged.

2. the xslt file should declare a namespace with two types of references. For select and non-select statements, add {}. Note that ""

3. Declare a class at will

4 The transform function should use an AddExtensionObject of argumentlist and specify the Namespace above (it will be associated)

------------------- 1 -----------------

<? Xml version = "1.0" encoding = "UTF-8"?>
<Employees>
<Employee>
<FirstName> Pierre-Emmanuel </FirstName>
<LastName> Dautreppe </LastName>
</Employee>
</Employees>

------------------- 2 ----------------------

<? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheetversion = "1.0"
Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform"
Xmlns: myUtils = "pda: MyUtils">
<Xsl: template match = "/Employees">
<Employees>
<Xsl: for-each select = "Employee">
<Employee>
<Xsl: value-of select = "myUtils: FormatName (FirstName, LastName)"/>
</Employee>
<Br> </br>
<A href = "{myUtils: GetLink ()} default. aspx"> abc.com </a>
</Xsl: for-each>
</Employees>
</Xsl: template>
</Xsl: stylesheet>

------------ 3 -----------------------------

Public class MyXslExtension
{
Public string FormatName (string firstName, string name)
{
Return name + "," + firstName;
}
Public string GetLink ()
{
Return "http://www.anypromo.com /";
}
}

---------- 4 ---------------------------------

Using targumentlist arguments = new using targumentlist ();

Arguments. AddExtensionObject ("pda: MyUtils", new MyXslExtension ());

StringWriter output = new StringWriter ();
Extends compiledtransform transform = new extends compiledtransform ();
Transform. Load (Server. MapPath ("a. xslt "));
Transform. Transform (Server. MapPath ("a. xml"), arguments, output );
Response. Write (output. ToString ());

Related Article

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.