在 R 中使用 XSLT 转换 XML [英] Convert XML using XSLT in R

查看:21
本文介绍了在 R 中使用 XSLT 转换 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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.

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

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

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

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.

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

推荐答案

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

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

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

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

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

xmlParse(files[[1]])

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

然后使用Sxslt应用样式表

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

给予

$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"

关于安装 Sxslt 的额外指导

  • 你需要先在你的 linux 机器上安装 libxslt-dev
  • 最新版本不再适用于 R 3.1.2,也不会从源代码安装到 Bug #1 在它的 repo 上.好心的 bug 提出者拿了一个 fork 并修复了这个包,这样你就可以使用 devtools::install_github("cboettig/Sxslt") 来安装它.
  • Extra guidance on installing Sxslt

    • You will need to install libxslt-dev on your linux machine first
    • 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.
    • 那个版本不再是了,但你可以使用:

      That version is no longer up, but you can use:

      install.packages("remotes")
      remotes::install_github("omegahat/Sxslt")
      

      这篇关于在 R 中使用 XSLT 转换 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆