ropensci / xslt

Extension of xml2 package for xsl transformations
https://docs.ropensci.org/xslt
27 stars 1 forks source link

feature request: support for <xsl:param/> #2

Closed thomaszumbrunn closed 6 years ago

thomaszumbrunn commented 6 years ago

Hi Jeroen

It would be great if one could pass parameter values from 'xml_xslt' to the XSL stylesheet where those values are used instead of the default values specified in the corresponding elements. (In 'xsltproc', this is available via the --stringparam option.)

Cheers /thomas

BTW: Great work!

jeroen commented 6 years ago

Can you perhaps include an example for me to test with?

thomaszumbrunn commented 6 years ago

Thanks for considering.

XML document

<?xml version="1.0" encoding="UTF-8"?>
<root/>

XSL document

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="text" encoding="UTF-8" media-type="text/plain" omit-xml-declaration="yes"/>
  <xsl:param name="author" select="''"/>
  <xsl:param name="reviewer" select="''"/>
  <xsl:template match="root">
    <xsl:text>author: </xsl:text><xsl:value-of select="$author"/><xsl:text>&#10;</xsl:text>
    <xsl:text>reviewer: </xsl:text><xsl:value-of select="$reviewer"/><xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

xsltproc call

xsltproc --stringparam author "foo" --stringparam reviewer "bar" example.xsl example.xml

output of xsltproc call

author: foo
reviewer: bar
jeroen commented 6 years ago

Ref: https://github.com/GNOME/libxslt/blob/master/doc/tutorial/libxslt_tutorial.c

jeroen commented 6 years ago

Can you test the dev version:

doc <- xml2::read_xml("~/Desktop/doc.xml")
style <- xml2::read_xml("~/Desktop/style.xml")
xslt::xml_xslt(doc, style, c(author="jeroen", reviewer="bert"))
thomaszumbrunn commented 6 years ago

Fabulous. Works as expected.

Thanks a lot for this package. (Up to now, I used a very primitive wrapper around 'xsltproc'.)

jeroen commented 6 years ago

On CRAN now.