xsltApplyStyleSheet: Process XML document with XSLT stylesheet

Description Usage Arguments Value Author(s) References See Also Examples

Description

From within R, this applies the rules in the specified stylesheet to the given XML document given either as the name of a file/URI, string containing the XML, or an internal DOM constructued using xmlTree and creates a new document as a result of the transformation.

Usage

1
2
3
4
5
xsltApplyStyleSheet(fileName, sheet,
                     isURL = length(grep("^(http|ftp)", fileName)) > 0,
                     xinclude = TRUE, ..., .params = character(0),
		     .profile = FALSE, .merge = TRUE,
		     .errorFun = xmlErrorCumulator("XSLErrorList"))

Arguments

fileName

the name of the file containing the XML document to process or alternatively a string containing the XML content directly.

sheet

the XSLTStylesheet object created via xsltParseStyleSheet that contains the rules with which to process the XML document.

isURL

a logical value indicating whether the fileName argument refers to a URI. This can be used to explicitly override the default detection mechanism.

...

any number of name=value pairs which are taken as XSLT parameters to use in the processing.

.params

an alternative mechanism to the ... for specifying the parameters. This is a named character vector giving the name = value pairs of parameters. These values are merged with any in the ... arguments. Elements in ... with the same name as in .params are used in preference to those in .params. Therefore, only those elements of .params whose names are not in ... are added to ... to make up the list of properties. These two arguments allow the caller to specify a persistent set across calls via the .params and to override individual values within that set for specific calls to xsltApplyStyleSheet.

xinclude

a logical value indicating whether to enable XInclude support during the processing of the XML document.

.profile

a logical value, a string or a function indicating that profiling of the XSL operations should be done and the results returned as a data frame (via readXSLProfileData) or written to the file given by this name. If a function is provided, it will be invoked with one argument which is an XMLInternalDocument-class object. See readXSLProfileData for an example of how to parse this material. For most situations, it is easiest to use this and work with the subsequent results. The measurements are in milliseconds.

.merge

either a string or a logical value that controls whether the function searches for an XSL style sheet within the XML document itself and uses that along with any style sheet specified via the sheet argument. If this is FALSE, no attempt to find a style sheet in the XML document is used. If this is TRUE, the first and hopefully only <xsl:stylesheet> node is used as the top-level style sheet. If it doesn't exist, an error occurs. If the value of .merge is a string, then we use this to find an <xsl:stylesheet> with a format attribute that matches this value, i.e. the XPath expression "//xsl:stylesheet[@format='html']".

If a style sheet is used from the input XML document, any style sheet specified via the sheet argument is imported into that top-level sheet, i.e. the equivalent of <xsl:import href="xsl"/>.

If more contol is needed over how the style sheets are merged, see mergeXSL in the package.

.errorFun

an R function (or NULL) that is invoked as each error is encountered during the XSL transformation process. This can raise an error in R or allow the process to continue, collecting more errors should they occur so that a user can fix many together.. This is the same as the error parameter in xmlTreeParse and related functions.

Value

An object of class XMLInternalXSLTDocument which has three slots:

doc

a reference to the native (C-level) XML document.

stylesheet

a reference to the native (C-level) XSLT stylesheet used to process the original document. This is needed to serialize the newly created document.

status

an integer value which is either an NA or the state of the XSL transformation context when the transformation concluded. The possible values are OK = 0, ERROR = 1, STOPPED = 2. The name is on the vector if it is not NA. This allows us to determine whether an error occurred, with 0 being the value if all was well. If status is NA, we cannot tell. This only happens when we are profiling the XSL processing.

Author(s)

Duncan Temple Lang <[email protected]>

References

http://www.omegahat.org/Sxslt, http://www.omegahat.org/SXalan, http://www.w3.org/Style/XSL http://xmlsoft.org/XSLT htt

See Also

saveXML xsltParseStyleSheet

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 files = sapply(c("sqrt.xml", "sqrt.xsl"), function(f) system.file("examples", f, package = "Sxslt"))
 doc <- xsltApplyStyleSheet(files[1], files[2])

 sheet <- xsltParseStyleSheet(system.file("examples", "params.xsl", package="Sxslt"))
 doc <- xsltApplyStyleSheet("<?xml version='1.0' ?><doc></doc>\n", sheet, isURL= FALSE, myParam="'foo'")


   # Using the style sheet within the input XML document.
 f = system.file("examples", "embeddedXSL.xml", package = "Sxslt")
 z = xsltApplyStyleSheet(f , .merge = "html")


## Not run: 
 segments = xsltParseStyleSheet(path.expand("~/Projects/org/omegahat/XML/Literate/segment.xsl"))
 doc <- xsltApplyStyleSheet(path.expand("~/Projects/org/omegahat/XML/Literate/examples/functionParts.xml"),
                             segments, isURL = FALSE)

 saveXML(doc)

## End(Not run)

sckott/sxslt documentation built on May 26, 2017, 3:49 a.m.