File: xml_xslt.R

package info (click to toggle)
r-cran-xslt 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: xml: 1,499; cpp: 83; sh: 14; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#' XSLT 1.0 Transformations
#'
#' Transform an XML document by applying an XSL stylesheet. Usually returns the 
#' transformed [xml_document][xml2::xml_new_document], unless the stylesheet has
#' `<xsl:output method="text">` in which case we return a text string.
#'
#' This implementation supports XSLT 1.0 features plus most of the EXSLT set of
#' processor-portable extensions functions. Unfortunately XSLT 2.0 or 3.0 features
#' are only available in proprietary libraries and currently unsupported. However 
#' XSLT 2.0 is not widely adopted anyway because it is unavailable in most browsers.
#'
#' @export
#' @rdname xslt
#' @name xslt
#' @useDynLib xslt
#' @importFrom xml2 read_xml
#' @importFrom Rcpp sourceCpp
#' @param doc xml document as returned by [xml2::read_xml]
#' @param stylesheet another xml document containing the XSL stylesheet
#' @param params named list or vector with additional XSLT parameters
#' @examples doc <- read_xml(system.file("examples/cd_catalog.xml", package = "xslt"))
#' style <- read_xml(system.file("examples/cd_catalog.xsl", package = "xslt"))
#' html <- xml_xslt(doc, style)
#' cat(as.character(html))
xml_xslt <- function(doc, stylesheet, params){
  UseMethod("xml_xslt")
}

#' @export
xml_xslt.xml_document <- function(doc, stylesheet, params = NULL){
  as_xml2 <- utils::getFromNamespace("xml_document", "xml2")
  stopifnot(inherits(stylesheet, "xml_document"))
  paramstr <- c(rbind(names(params), vapply(params, deparse, character(1))))
  out <- doc_xslt_apply(doc$doc, stylesheet$doc, paramstr)
  if(is.character(out))
    return(out)
  as_xml2(out)
}

#' @export
#' @rdname xslt
xslt_version <- function(){
  list(
    xml2 = as.package_version(libxml2_version()),
    xslt = as.package_version(libxslt_version())
  )
}