Xsltransform class implementation XSLT Processor

Source: Internet
Author: User
Tags xslt xslt processor

XsltransformClass is An XSLT processor recommended for XSL conversion (XSLT) Version 1.0.LoadMethod to locate and read the style sheet,TransformMethod to convert the specified source document. Any implementedIxpathnavigableThe storage area of the interface can be usedXsltransform.. . NET FrameworkXmldocument,XmldatadocumentAndXpathdocumentOnIxpathnavigableInterfaces, so they can all be used as conversion input source documents.

In. NET FrameworkXsltransformThe object only supports the XSLT 1.0 specification defined using the following namespace:

 
<XSL: stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0">

Style sheets can be usedLoadMethods are loaded from one of the following classes:

    • Xpathnavigator
    • Xmlreader
    • String that represents the URI

The input classes above have differentLoadMethod. Some methods take one of the classes andXmlresolverClass combination as a parameter.XmlresolverLocate from the style sheet<XSL: Import>Or<XSL: Include>The referenced resource. The following methods use strings,XmlreaderOrXpathnavigatorAs input.

[Visual Basic]Overloads public sub load (string)

 
[C #]Public void load (string );

 
[Visual Basic]Overloads public sub load (string, xmlresolver)

 
[C #]Public void load (string, xmlresolver );

 
[Visual Basic]Overloads public sub load (xmlreader, xmlresolver, eviver)

[C #]Public void load (xmlreader, xmlresolver, eviver );

 
[Visual Basic]Overloads public sub load (xpathnavigator, xmlresolver, eviver)

 
[C #]Public void load (xpathnavigator, xmlresolver, eviver );

The majority shown aboveLoadObtain all methods As a parameter.XmlresolverUsed to load style sheets andXSL: ImportAndXSL: IncludeAny style sheet referenced in the element.

MajorityLoadMethod alsoEvidenceAs a parameter.EvidenceParameters are associated with the style sheet. . The security level of the style sheet affects the security level of all resources it references, such as scripts it contains and anyDocument ()Functions, andXsltargumentlistAny extension object used.

If the style sheet uses a URL parameter but does notEvidenceParameterLoadThe evidence of the style sheet is calculated based on the given URL and its site and region.

If notUriOrEvidence, Then the style sheet'sEvidenceSet is fully trusted. Do not load style sheets from untrusted sources or add untrusted extension objectsXslargumentlist.

ForLoadFor more information, see . Security level andEvidenceAndEvidenceFor more information on how to affect script writing, see . Security level andEvidenceAndEvidenceFor more information about how to affect extended objects, see .

Security level andEvidenceAndEvidenceImpactDocument ()For more information about functions, see .

Many input parameters can be provided to the style sheet. Style Sheets can also call functions on extended objects. Both parameters and extension objects are used.XsltargumentlistClass provided to the style sheet. RelatedXsltargumentlistFor more information, see .

Recommended Security usage of the xsltransform class

The security privilege of the style sheet depends onEvidence. The following table summarizes the position of the style sheet and describes the type of evidence to be provided.

Solution Type of evidence provided
the XSLT style sheet does not have external references, or the style sheet comes from the Code library you trust. provides a style sheet from your Program set:
  [Visual Basic]  dim XSLT = new xsltransform () XSLT. load (stylesheet, resolver, me. getType (). assembly. evidence) 

  [C #]  extends ttransform XSLT = new transform (); XSLT. load (stylesheet, resolver, this. getType (). assembly. evidence); 

the XSLT style sheet comes from an external source. The source is known and has a verifiable Uri. Use URI to create evidence.
  [Visual Basic]  dim XSLT as new transform () dim eV as eviver = xmlsecureresolver. createevidenceforurl (stylesheeturi) XSLT. load (stylesheet, resolver, eviver) 

  [C #]  using transform XSLT = new using transform (); evi1_ev = xmlsecureresolver. createevidenceforurl (stylesheeturi); XSLT. load (stylesheet, resolver, eviver); 

XSLT style sheets are from external sources. The source is unknown. Set the evidenceNullReference (in VBNothing). Script blocks will not be processed, XSLTDocument ()Functions are not supported, and privileged extension objects are not allowed.

In addition, you canResolverSet the parameterNullReference (Nothing). This ensures that no processing is performed.XSL: ImportAndXSL: IncludeElement.

XSLT style sheets are from external sources. The source is unknown, but you need script support. Request evidence from the caller.
XML Data Conversion

After the style sheet is loaded,TransformMethod and provide the input source document to start the conversion. Heavy LoadTransformMethods To provide different conversion outputs. Conversion produces the following output formats:

    • Xmlreader
    • Xmlwriter
    • Textwriter
    • Stream
    • File string URL

The last format is string URL, which provides a common solution to convert the input document located in the URL and write the document into the output URL. ThisTransformThe method is to load the XML document from the file, execute XSLT conversion, and write the output to the file. In this way, you do not have to create and load the Input Source Document, and then write the file stream. The following code example showsTransformHow to use a string URL as input and output:

[Visual Basic]Dim transform as transform = new transform () transform. Load ("favorite. XSL") transform. Transform ("mydocument. xml", "transformresult. xml", nothing)

 
[C #]Transform transform into transform = new transform (); transform. Load ("favorite. XSL"); transform. Transform ("mydocument. xml", "transformresult. xml", null );

Section for converting XML documents

The conversion will be applied to the entire document. In other words, if you input a node other than the root node of the document, the conversion process cannot access all nodes of the loaded document. To convert a node fragment, you must createXmldocument, AndXmldocumentPassTransformMethod. The following example converts a node segment.

[Visual Basic]Dim XSLT as new transform () XSLT. load ("print_root.xsl") dim doc as new xmldocument () Doc. load ("library. XML ") 'create a new document containing just the node fragment. dim testnode as xmlnode = Doc. documentelement. firstchilddim tmpdoc as new xmldocument () tmpdoc. loadxml (testnode. outerxml) 'pass the document ining the node fragment 'to the transform method. console. writeline ("passing" + tmpdoc. outerxml + "to print_root.xsl") XSLT. transform (tmpdoc, nothing, console. out, nothing)

[C #]Transform transform XSLT = new transform (); XSLT. load ("print_root.xsl"); xmldocument Doc = new xmldocument (); Doc. load ("library. XML "); // create a new document containing just the node fragment. xmlnode testnode = Doc. documentelement. firstchild; xmldocument tmpdoc = new xmldocument (); tmpdoc. loadxml (testnode. outerxml); // pass the document containing the node fragment // to the transform method. console. writeline ("passing" + tmpdoc. outerxml + "to print_root.xsl"); XSLT. transform (tmpdoc, null, console. out, null );

This example usesLibrary. xmlAndPrint_root.xslFile as the input, and the following content is output to the console.

[Visual Basic, C #]Passing<Book genre = "novel" ISBN = "1-861001-57-5"> <title> Pride and Prejudice </title> </book>To print_root.xsl root node isBook.

Library. xml

 
[Visual Basic, C #]<Library> <book genre = 'noel' ISBN = '1-861001-57-5 '> <title> Pride and Prejudice </title> </book> <book genre = 'noel' ISBN = '1-81920-21-2 '> <title> hook </title> </book> </library>

Print_root.xsl

[Visual Basic, C #]<Stylesheet version = "1.0" xmlns = "http://www.w3.org/1999/XSL/Transform"> <output method = "text"/> <template match = "/"> root node is <value-of select =" local-Name (// * [position () = 1]) "/> </template> </stylesheet>

XSLT migration from. NET Framework 1.0 to. NET Framework 1.1

The following table shows the outdated versions of. NET Framework 1.0.Xsltransform. LoadMethod and new in. NET Framework 1.1Xsltransform. LoadMethod. The new method allows you to specify evidence to restrict the style sheet permissions.

outdated load method in. NET Framework 1.0 . NET Framework 1.1 as an alternative to the new load method
load (xpathnavigator input);

load (xpathnavigator input, xmlresolver resolver );

load (xpathnavigator stylesheet, xmlresolver resolver, evidence eviver);
load (ixpathnavigable stylesheet);

load (ixpathnavigable stylesheet, xmlresolver resolver );

load (ixpathnavigable stylesheet, xmlresolver resolver, evidence eviver);
load (xmlreader stylesheet);

load (xmlreader stylesheet, xmlresolver resolver );

load (xmlreader stylesheet, xmlresolver resolver, evidence eviver);

The following table showsXsltransform. TransformAnd the new method. New Method UsedXmlresolverObject as a parameter.

Outdated. NET Framework 1.0 Transform Method New transform method used as an alternative in. NET Framework 1.1
Xmlreader transform (xpathnavigator input, xsltargumentlist ARGs) Xmlreader transform (xpathnavigator input, extends targumentlist ARGs, xmlresolver resolver)
Xmlreader transform (ixpathnavigable input, xsltargumentlist ARGs) Xmlreader transform (ixpathnavigable input, xsltargumentlist ARGs, xmlresolver resolver)
Void transform (xpathnavigator input, extends targumentlist ARGs, xmlwriter output) Void transform (xpathnavigator input, extends targumentlist ARGs, xmlwriter output, xmlresolver resolver)
Void transform (ixpathnavigable input, xsltargumentlist ARGs, xmlwriter output) Void transform (ixpathnavigable input, extends targumentlist ARGs, xmlwriter output, xmlresolver resolver)
Void transform (xpathnavigator input, extends targumentlist ARGs, textwriter output) Void transform (xpathnavigator input, extends targumentlist ARGs, textwriter output, xmlresolver resolver)
Void transform (ixpathnavigable input, xsltargumentlist ARGs, textwriter output) Void transform (ixpathnavigable input, extends targumentlist ARGs, textwriter output, xmlresolver resolver)
Void transform (xpathnavigator input, xsltargumentlist ARGs, stream output) Void transform (xpathnavigator input, extends targumentlist ARGs, stream output, xmlresolver resolver)
Void transform (ixpathnavigable input, xsltargumentlist ARGs, stream output) Void transform (ixpathnavigable input, xsltargumentlist ARGs, stream output, xmlresolver resolver)
Void transform (string input, string output ); Void transform (string input, string output, xmlresolver resolver );

Xsltransform. xmlresolverThe property is out of date in. NET Framework 1.1. Use should be switchedXmlresolverObject as a new parameterTransformOverload.

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.