The parameter targumentlist of style sheet parameters and extended objects (use program encoding to add scripts! = Add under CDATA)

Source: Internet
Author: User
Tags cdata xsl xslt

XsltargumentlistClass contains XSLT parameters and XSLT extension objects. InputTransformThese parameters and extension objects can be called from the style sheet.

Compared with embedded scripts, passing objects has the following advantages:

    • Improved class encapsulation and reuse.
    • Make the style sheet smaller and easier to maintain. (However, you cannot dynamically modify the functions or services. You 'd better add a script to the style sheet)
    • Support calling belongs to other namespaces (not those that are supportedSystemNamespace defined in the namespace set.
    • SupportedXpathnavigatorPass the result tree fragment to the style sheet.
XSLT style sheet Parameters

XSLT parameters are passedAddparamAdd methodXsltargumentlist. In this case, the qualified name and namespace URI are associated with the parameter object.

The parameter object must be of the W3C type. The following table shows the W3C type, the equivalent. Net class (type), and whether the W3C type is XPath type or XSLT type.

& Lt; TD width = "33%" & gt; string & lt;/TD & gt; & Lt; TD width = "45%" & gt; system. String & lt;/TD & gt; & Lt; TD width = "45%" & gt; system. boolean & lt;/TD & gt; & Lt; TD width = "45%" & gt; system. Double & lt;/TD & gt; & Lt; TD width = "22%" & gt; XSLT & lt;/TD & gt;
W3C type equivalent. Net class (type) XPath type or XSLT type
XPath
Boolean XPath
Number XPath
result tree fragment system. xml. XPath. xpathnavigator
node set system. xml. XPath. xpathnodeiterator XPath

If the parameter object does not belong to the preceding class, it is forcibly specified as a double or string (whichever applies ). The int16, uint16, int32, uint32, int64, uint64, single, and decimal types are forcibly specified as double. All other types are usedTostringMethod must be specified as string.

Use the required XSLT parameters to perform the following operations:

    1. CreateXsltargumentlistAnd useAddparamAdd object.
    2. Call parameters from the style sheet.
    3. SetXsltargumentlistPassTransformMethod.

Example

The following example usesAddparamMethod To save the calculated discount date. The discount date is calculated as the 20 days from the order date.

 [Visual Basic] Imports systemimports system. ioimports system. xmlimports system. XML. xpathimports system. XML. using public class sample private const filename as string = "order. XML "Private const stylesheet as string =" discount. XSL "public shared sub main () 'create the same transform and load the stylesheet. dim XSLT as transform = new transform XSLT. load (stylesheet) 'Load the XML data file. dim doc as xpathdocument = new xpathdocument (filename) 'create an extension targumentlist. dim parameter Arg as parameter targumentlist = new parameter targumentlist 'calculate the discount date. dim today as datetime = datetime. now dim D as datetime = today. adddays (20) invalid Arg. addparam ("discount", "", D. tostring () 'create an xmltextwriter to handle the output. dim writer as xmltextwriter = new xmltextwriter ("orderout. XML ", nothing) 'transform the file. XSLT. transform (Doc, xslarg, writer, nothing) writer. close () end subend class

[C #] Using system; using system. io; using system. XML; using system. XML. XPath; using system. XML. XSL; public class sample {private const string filename = "order. XML "; private const string stylesheet =" discount. XSL "; public static void main () {// create the custom transform and load the stylesheet. transform transform XSLT = new transform (); XSLT. load (stylesheet); // load the XML data file. xpathdocument Doc = new xpathdocument (filename); // create an extension targumentlist. repeated targumentlist parameters Arg = new values targumentlist (); // calculate the discount date. datetime today = datetime. now; datetime d = today. adddays (20); invalid Arg. addparam ("discount", "", D. tostring (); // create an xmltextwriter to handle the output. xmltextwriter writer = new xmltextwriter ("orderout. XML ", null); // transform the file. XSLT. transform (Doc, xslarg, writer, null); writer. close ();}}

Input

Order. xml

 
[Visual Basic, C #]<! -- Represents a customer order --> <order> <book ISBN = '10-861003-324 '> <title> The Handmaid's tale </title> <price> 19.95 </price> </book> <cd isbn = '2-3631-4 '> <title> Americana </title> <price> 16.95 </price> </Cd> </order>

Discount. XSL

[Visual Basic, C #]<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"> <XSL: Param name = "discount"/> <XSL: template match = "/"> <order> <XSL: variable name = "Sub-total" select = "sum (// price)"/> <total> <XSL: value-of select = "$ Sub-total"/> </total> 15% discount if paid by: <XSL: value-of select = "$ discount"/> </order> </XSL: Template> </XSL: stylesheet>

Output

 
[Visual Basic, C #]<Order> <total> 36.9 </total> 15% discount if paid by: 5:01:15 </order>

XSLT extension object

XSLT extends objects throughAddextensionobjectAdd methodXsltargumentlist. In this case, the qualified name and namespace URI are associated with the extended object.

When you add an object,AddextensionobjectThe caller must be fully trusted in the security policy. If the caller is not fully trusted, The add operation fails.

Although the object has been successfully added, it cannot guarantee that the execution will succeed. CallTransformTheLoadAnd assign the permission set to the entire conversion process. If an extension object tries to start an operation and the operation requires permissions not in the permission set, an exception is thrown.

The data types returned from an extended object are one of the four basic XPath data types: Numbers, strings, Boolean values, or node sets.

Use the XSLT extension object you need to perform the following operations:

    1. CreateXsltargumentlistAnd useAddextensionobjectAdd extension object.
    2. Call the extension object from the style sheet.
    3. SetXsltargumentlistPassTransformMethod.

Example

The radius of the known circle. The following example calculates the circle perimeter.

 [Visual Basic] Imports systemimports system. ioimports system. xmlimports system. XML. xpathimports system. XML. using public class sample private const filename as string = "number. XML "Private const stylesheet as string =" circle. XSL "public shared sub main () dim test as sample = new sample end sub public sub new () 'create the same transform and load the stylesheet. dim XSLT as transform = new transform XSLT. load (stylesheet) 'Load the XML data file. dim doc as xpathdocument = new xpathdocument (filename) 'create an extension targumentlist. dim parameter Arg as parameter targumentlist = new parameter targumentlist 'add an object to calculate the circumference of the circle. dim OBJ as calculate = new calculate w.arg. addextensionobject ("urn: myobj", OBJ) 'create an xmltextwriter to output to the console. dim writer as xmltextwriter = new xmltextwriter (console. out) 'transform the file. XSLT. transform (Doc, xslarg, writer, nothing) writer. close () end sub 'calculates the circumference of a circle given the radius. public class calculate private CIRC as double = 0 public function circumference (radius as double) as double CIRC = math. pI * 2 * radius return CIRC end function end classend class

 [C #] Using system; using system. io; using system. XML; using system. XML. XPath; using system. XML. XSL; public class sample {private const string filename = "number. XML "; private const string stylesheet =" circle. XSL "; public static void main () {sample test = new sample ();} public sample () {// create the same transform and load the stylesheet. transform transform XSLT = new transform (); XSLT. load (stylesheet); // load the XML data file. xpathdocument Doc = new xpathdocument (filename); // create an extension targumentlist. using targumentlist using Arg = new using targumentlist (); // Add an object to calculate the circumference of the circle. calculate OBJ = new calculate (); returns Arg. addextensionobject ("urn: myobj", OBJ); // create an xmltextwriter to output to the console. xmltextwriter writer = new xmltextwriter (console. out); // transform the file. XSLT. transform (Doc, xslarg, writer, null); writer. close ();} // calculates the circumference of a circle given the radius. public class calculate {private double CIRC = 0; Public double circumference (double radius) {CIRC = math. pI * 2 * radius; return CIRC ;}}}

Input

Number. xml

 
[Visual Basic, C #]<? XML version = '1. 0'?> <DATA> <circle> <radius> 12 </radius> </circle> <radius> 37.5 </radius> </circle> </data>

Circle. XSL

[Visual Basic, C #]<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" xmlns: myobj = "urn: myobj"> <XSL: template match = "data"> <circles> <XSL: For-each select = "circle"> <circle> <XSL: copy-of select = "Node () "/> <circumference> <XSL: value-of select =" myobj: circumference (RADIUS) "/> </circumference> </circle> </XSL: for-each> </circles> </XSL: Template> </XSL: stylesheet>

Output

 
[Visual Basic, C #]

<Circles xmlns: myobj = "urn: myobj">

<Circle>

<Radius> 12 </radius>

<Circumference> 75.398223686155 </circumference>

</Circle>

<Circle>

& Lt; radius & gt; 37.5 & lt;/radius & gt;

<Circumference> 235.61944901923448 </circumference>

</Circle>

</Circles>

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.