在R中使用XSLT转换XML

时间:2022-01-20 21:50:15

I have task currently being performed by SQL Server Reporting Services where it exports a dataset to an XML format using an XSLT that converts the typical schema to a bespoke one. In order to replace this report, I need to be able to similarly transform XML when the user wants to download it.

我的任务当前由SQL Server Reporting Services执行,它使用XSLT将数据集导出为XML格式,XSLT将典型架构转换为定制架构。为了替换此报告,我需要能够在用户想要下载时类似地转换XML。

The end layout produced by XSLT is horrible - it involves padding and all sorts of whacky concatenations and I'd rather not reinvent the wheel by doing the whole transformation in the first conversion from data to XML.

XSLT产生的最终布局非常糟糕 - 它涉及填充和各种破坏性的连接,我宁愿不通过在从数据到XML的第一次转换中完成整个转换来重新发明*。

My google-fu has failed me: how do I convert XML via an XSLT using R?

我的google-fu失败了我:如何使用R通过XSLT转换XML?

MWE

Here is a script that uses iris data and converts it to XML (I use my package from CRAN purely to keep the code level down on the example). I then have an XSLT (on gist) that when I run the XML through in Visual Studio converts the XML but I don't know how to translate that activity into R.

这是一个使用虹膜数据并将其转换为XML的脚本(我使用CRAN中的软件包纯粹是为了保持代码级别在示例中)。然后我有一个XSLT(在要点上)当我在Visual Studio中运行XML时转换XML但我不知道如何将该活动转换为R.

library(optiRum)
library(XML)
irisdata<-convertToXML(iris)
saveXML(irisdata,"iris.xml")

1 个解决方案

#1


6  

I think you're looking for Sxslt package, just using example data

我想你正在寻找Sxslt包,只是使用示例数据

library("Sxslt")
library("XML")
files <- sapply(c("sqrt.xml", "sqrt.xsl"), function(f) system.file("examples", f, package = "Sxslt"))

Where files[1] is an xml file and files[2] is an xsl file

其中files [1]是xml文件,files [2]是xsl文件

xmlParse(files[[1]])

<?xml version="1.0"?>
<test>
  <sqrt>9</sqrt>
  <date/>
  <code>mean(rnorm(10000))</code>
</test>

Then use Sxslt to apply stylesheet

然后使用Sxslt应用样式表

xsltApplyStyleSheet(files[1], files[2])

Gives

$doc
<?xml version="1.0" standalone="yes"?>
<HTML>
  <body>
 sqrt: 3
 pow: 81
 date: Thu Jan 15 06:46:54 2015<i class="output">-0.0140224652198879</i>
  substring: an(rnor</body>
</HTML>


$stylesheet
An object of class "XSLStyleSheet"
Slot "ref":
<pointer: 0x108ef2a60>


$status
OK 
 0 

attr(,"class")
[1] "XMLInternalXSLTDocument"

Extra guidance on installing Sxslt

  • You will need to install libxslt-dev on your linux machine first
  • 您需要先在Linux机器上安装libxslt-dev

  • The latest build no longer works with R 3.1.2, nor will it install from source further to Bug #1 on it's repo. The kind raiser of the bug took a fork and fixed the package so you can use devtools::install_github("cboettig/Sxslt") to install it
  • 最新的版本不再适用于R 3.1.2,它也不会从源代码安装到它的repo上的Bug#1。 bug的类型提升者使用fork并修复了包,因此您可以使用devtools :: install_github(“cboettig / Sxslt”)来安装它

#1


6  

I think you're looking for Sxslt package, just using example data

我想你正在寻找Sxslt包,只是使用示例数据

library("Sxslt")
library("XML")
files <- sapply(c("sqrt.xml", "sqrt.xsl"), function(f) system.file("examples", f, package = "Sxslt"))

Where files[1] is an xml file and files[2] is an xsl file

其中files [1]是xml文件,files [2]是xsl文件

xmlParse(files[[1]])

<?xml version="1.0"?>
<test>
  <sqrt>9</sqrt>
  <date/>
  <code>mean(rnorm(10000))</code>
</test>

Then use Sxslt to apply stylesheet

然后使用Sxslt应用样式表

xsltApplyStyleSheet(files[1], files[2])

Gives

$doc
<?xml version="1.0" standalone="yes"?>
<HTML>
  <body>
 sqrt: 3
 pow: 81
 date: Thu Jan 15 06:46:54 2015<i class="output">-0.0140224652198879</i>
  substring: an(rnor</body>
</HTML>


$stylesheet
An object of class "XSLStyleSheet"
Slot "ref":
<pointer: 0x108ef2a60>


$status
OK 
 0 

attr(,"class")
[1] "XMLInternalXSLTDocument"

Extra guidance on installing Sxslt

  • You will need to install libxslt-dev on your linux machine first
  • 您需要先在Linux机器上安装libxslt-dev

  • The latest build no longer works with R 3.1.2, nor will it install from source further to Bug #1 on it's repo. The kind raiser of the bug took a fork and fixed the package so you can use devtools::install_github("cboettig/Sxslt") to install it
  • 最新的版本不再适用于R 3.1.2,它也不会从源代码安装到它的repo上的Bug#1。 bug的类型提升者使用fork并修复了包,因此您可以使用devtools :: install_github(“cboettig / Sxslt”)来安装它