XsltProcessor Class

Transforms nodes with an XSLT 1.0 stylesheet (https://dom.spec.whatwg.org/#interface-xsltprocessor, https://www.w3.org/TR/xslt-10/), as the browsers do.

public sealed class XsltProcessor

Remarks

The stylesheet is compiled when the first transformation runs, and its xsl:include and xsl:import elements load their stylesheets synchronously with the fetch of the document of the current global object (the same-origin requests of destination xslt, checked by its content security policies). As in Chromium, the document() function returns an empty node-set, and the disable-output-escaping attributes are ignored.

The XPath expressions are evaluated by System.Xml.XPath over the DOM: the stylesheet is interpreted, so the transformations work without dynamic code (with native AOT). A recursion deeper than 3000 templates is an error.

The transformations are bounded by the BrowsingContextOptions.Xslt options of the browsing context of the relevant document (XsltOptions.Default without browsing context): a transformation that exceeds them is an error. A transformation run by a script also stops when the script engine interrupts the script (e.g. its timeout).

See also

Constructors

NameDescription
XsltProcessor()Creates a processor associated with the current global document.

XsltProcessor()

XsltProcessor.cs

Creates a processor associated with the current global document.

public XsltProcessor()

Methods

NameDescription
ClearParameters()Removes all the parameters.
GetParameter(string, string)Returns the value of a parameter set by XsltProcessor.SetParameter, or null.
ImportStylesheet(Node)Imports a stylesheet: a document whose document element is an xsl:stylesheet or xsl:transform element (or a literal result element with an xsl:version attribute), or such an element. The stylesheet replaces the previous one; it is compiled by the next transformation.
RemoveParameter(string, string)Removes a parameter set by XsltProcessor.SetParameter.
Reset()Removes the stylesheet and all the parameters.
SetParameter(string, string, object?)Sets the value of a global parameter of the stylesheet (an xsl:param element). A string, a number and a boolean are the XPath values; a node, or a list of nodes, is a node-set; any other value is converted to a string.
TransformToDocument(Node)Transforms a node, and returns the result tree as a new document that has no browsing context: an HTML document for the html output method, an XML document for the xml output method, and an HTML document whose text is in a pre element for the text output method. Its scripts never run.
TransformToFragment(Node, Document)Transforms a node, and returns the nodes of the result tree in a fragment owned by output. With the html output method, or without output method when output is an HTML document, the elements in no namespace are HTML elements. The scripts of the fragment run when it is inserted in a document.

ClearParameters()

XsltProcessor.cs

Removes all the parameters.

public void ClearParameters()

GetParameter(string, string)

XsltProcessor.cs

Returns the value of a parameter set by XsltProcessor.SetParameter, or null.

public object? GetParameter(string namespaceUri, string localName)

ImportStylesheet(Node)

XsltProcessor.cs

Imports a stylesheet: a document whose document element is an xsl:stylesheet or xsl:transform element (or a literal result element with an xsl:version attribute), or such an element. The stylesheet replaces the previous one; it is compiled by the next transformation.

public void ImportStylesheet(Node style)

Parameters

style Node

Exceptions

DomException
The node is not a document or an element (NotSupportedError).

See also

RemoveParameter(string, string)

XsltProcessor.cs

Removes a parameter set by XsltProcessor.SetParameter.

public void RemoveParameter(string namespaceUri, string localName)

Removes the stylesheet and all the parameters.

public void Reset()

SetParameter(string, string, object?)

XsltProcessor.cs

Sets the value of a global parameter of the stylesheet (an xsl:param element). A string, a number and a boolean are the XPath values; a node, or a list of nodes, is a node-set; any other value is converted to a string.

public void SetParameter(string namespaceUri, string localName, object? value)

TransformToDocument(Node)

XsltProcessor.cs

Transforms a node, and returns the result tree as a new document that has no browsing context: an HTML document for the html output method, an XML document for the xml output method, and an HTML document whose text is in a pre element for the text output method. Its scripts never run.

public Document TransformToDocument(Node source)

Parameters

source Node

Returns

Document

Exceptions

DomException
No stylesheet is imported (InvalidStateError), the source is not a node that XPath represents (NotSupportedError), or the stylesheet is not valid or the transformation fails (OperationError).

See also

TransformToFragment(Node, Document)

XsltProcessor.cs

Transforms a node, and returns the nodes of the result tree in a fragment owned by output. With the html output method, or without output method when output is an HTML document, the elements in no namespace are HTML elements. The scripts of the fragment run when it is inserted in a document.

public DocumentFragment TransformToFragment(Node source, Document output)

Parameters

source Node
output Document

Returns

DocumentFragment

Exceptions

DomException
No stylesheet is imported (InvalidStateError), the source is not a node that XPath represents (NotSupportedError), or the stylesheet is not valid or the transformation fails (OperationError).

See also