Write and execute XSLT transformations with Clojure.
Scenario:
xsl:element
.For example, you want to transform this:
<a xmlns="my-ns"/>
To this:
<b xmlns="my-ns"/>
In your XSLT transformation, you want to write something like this:
(xsl/template {:match "a"} [:b])
Instead of this:
(xsl/template {:match "a"}
(xsl/element {:name "b"}))
If you create a literal element with [:b]
, though, clojure.data.xml puts it
into the empty namespace. That means that the XSLT stylesheet Sigel produces
will have this:
<xsl:template match="a">
<!-- note the empty xmlns namespace declaration -->
<b xmlns=""/>
</xsl:template>
When what you actually want is this:
<xsl:template match="a">
<b/>
</xsl:template>
To do that, you must first set up your namespace with
clojure.data.xml/alias-uri
:
(xml/alias-uri 'my-ns "my-ns-uri")
You then need to set the xmlns
attribute of your stylesheet and use the
my-ns
prefix when emitting literal XML elements:
(xsl/stylesheet {:version 3.0 :xmlns "my-ns-uri"}
(xsl/template {:match "a"} [::my-ns/b]))
Write and execute XSLT transformations with Clojure. ## XML namespaces Scenario: - Your source XML document is in an XML namespace. - You want to transform that document with Sigel into the same namespace. - You want to write literal XML elements in your transformation, instead of using `xsl:element`. For example, you want to transform this: ```xml <a xmlns="my-ns"/> ``` To this: ```xml <b xmlns="my-ns"/> ``` In your XSLT transformation, you want to write something like this: ``` (xsl/template {:match "a"} [:b]) ``` Instead of this: ``` (xsl/template {:match "a"} (xsl/element {:name "b"})) ``` If you create a literal element with `[:b]`, though, clojure.data.xml puts it into the empty namespace. That means that the XSLT stylesheet Sigel produces will have this: ```xml <xsl:template match="a"> <!-- note the empty xmlns namespace declaration --> <b xmlns=""/> </xsl:template> ``` When what you actually want is this: ```xml <xsl:template match="a"> <b/> </xsl:template> ``` To do that, you must first set up your namespace with `clojure.data.xml/alias-uri`: ```clojure (xml/alias-uri 'my-ns "my-ns-uri") ``` You then need to set the `xmlns` attribute of your stylesheet and use the `my-ns` prefix when emitting literal XML elements: ```clojure (xsl/stylesheet {:version 3.0 :xmlns "my-ns-uri"} (xsl/template {:match "a"} [::my-ns/b])) ```
A default XSLT compiler.
If you don't pass in your own XsltCompiler instance when compiling a stylesheet, Sigel uses this instance.
A default XSLT compiler. If you don't pass in your own [XsltCompiler](http://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltCompiler.html) instance when compiling a stylesheet, Sigel uses this instance.
(apply-templates transformer source)
(apply-templates transformer source destination)
(compile-edn path)
Compile a stylesheet defined in an EDN file.
To write an EDN stylesheet, instead of calling the functions in the
sigel.xslt.elements
namespace, use the :xsl
namespace prefix like this:
;; a.edn
[:xsl/stylesheet {:version 3.0}
[:xsl/template {:match "a"} [:b]]]
Then compile the stylesheet and transform some XML with it:
(xslt/transform (xslt/compile-edn "resources/examples/a.edn") "<a/>")
;;=> #object[net.sf.saxon.s9api.XdmNode 0xf2a49c4 "<b/>"]
Compile a stylesheet defined in an EDN file. To write an EDN stylesheet, instead of calling the functions in the `sigel.xslt.elements` namespace, use the `:xsl` namespace prefix like this: ``` ;; a.edn [:xsl/stylesheet {:version 3.0} [:xsl/template {:match "a"} [:b]]] ``` Then compile the stylesheet and transform some XML with it: ``` (xslt/transform (xslt/compile-edn "resources/examples/a.edn") "<a/>") ;;=> #object[net.sf.saxon.s9api.XdmNode 0xf2a49c4 "<b/>"] ```
(compile-sexp stylesheet)
(compile-sexp compiler stylesheet)
Compile an XSLT stylesheet written in Clojure.
If you don't pass in an XsltCompiler,
Sigel uses *compiler*
.
You can execute the compiled stylesheet with transform
.
Example:
;; Define an XSLT stylesheet.
(def stylesheet
(xsl/stylesheet {:version 3.0}
(xsl/template {:match "a"} [:b])))
;; Compile the stylesheet.
(xslt/compile-sexp stylesheet)
;;=> #object[net.sf.saxon.s9api.XsltExecutable 0x1098b3aa "net.sf.saxon.s9api.XsltExecutable@1098b3aa"]
Compile an XSLT stylesheet written in Clojure. If you don't pass in an [XsltCompiler](http://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltCompiler.html), Sigel uses [[*compiler*]]. You can execute the compiled stylesheet with [[transform]]. Example: ``` ;; Define an XSLT stylesheet. (def stylesheet (xsl/stylesheet {:version 3.0} (xsl/template {:match "a"} [:b]))) ;; Compile the stylesheet. (xslt/compile-sexp stylesheet) ;;=> #object[net.sf.saxon.s9api.XsltExecutable 0x1098b3aa "net.sf.saxon.s9api.XsltExecutable@1098b3aa"] ```
(compile-source stylesheet)
(compile-source compiler stylesheet)
Compile an XSLT stylesheet from a Source.
Compile an XSLT stylesheet from a [Source](https://docs.oracle.com/javase/8/docs/api/javax/xml/transform/Source.html).
(compile-xslt input)
Compile a stylesheet defined in an XSLT resource.
The input must be something that [[io/input-stream]] can coerce into an input stream.
Compile a stylesheet defined in an XSLT resource. The input must be something that [[io/input-stream]] can coerce into an input stream.
(compiler)
Create a new XsltCompiler.
Create a new [XsltCompiler](http://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltCompiler.html).
(transform executables source)
(transform executables params source)
Execute one or more XSLT transformations on the given source file.
Example:
;; Define an XSLT transformation.
(def stylesheet
(xslt3-identity (xsl/template {:match "a"} [:b])))
;; Compile the stylesheet and use it to transform "<a/>".
(xslt/transform (xslt/compile-sexp stylesheet) "<a/>")
;;=> "<b/>"
To execute multiple transformations in sequence, pass a sequence of compiled stylesheets.
Example:
(def stylesheet-1
(xslt3-identity (xsl/template {:match "a"} [:b])))
(def stylesheet-2
(xslt3-identity (xsl/template {:match "b"} [:c])))
(def compiled-stylesheets
[(xslt/compile-sexp stylesheet-1) (xslt/compile-sexp stylesheet-2)])
(xslt/transform compiled-stylesheets "<a/>")
;;=> "<c/>"
You can pass a map of parameters to the XSLT stylesheet.
Example:
(def xslt
(xsl/stylesheet {:version 3.0 :xmlns:xs "http://www.w3.org/2001/XMLSchema"}
(xsl/param {:name "factor" :as "xs:integer"})
(xsl/template {:match "num"}
(xsl/copy (xsl/value-of {:select "xs:int(.) * $factor"})))))
(xslt/transform (xslt/compile-sexp xslt) {:factor 10} "<num>1</num>")
;;=> "<num>10</num>"
Execute one or more XSLT transformations on the given source file. Example: ``` ;; Define an XSLT transformation. (def stylesheet (xslt3-identity (xsl/template {:match "a"} [:b]))) ;; Compile the stylesheet and use it to transform "<a/>". (xslt/transform (xslt/compile-sexp stylesheet) "<a/>") ;;=> "<b/>" ``` To execute multiple transformations in sequence, pass a sequence of compiled stylesheets. Example: ``` (def stylesheet-1 (xslt3-identity (xsl/template {:match "a"} [:b]))) (def stylesheet-2 (xslt3-identity (xsl/template {:match "b"} [:c]))) (def compiled-stylesheets [(xslt/compile-sexp stylesheet-1) (xslt/compile-sexp stylesheet-2)]) (xslt/transform compiled-stylesheets "<a/>") ;;=> "<c/>" ``` You can pass a map of parameters to the XSLT stylesheet. Example: ``` (def xslt (xsl/stylesheet {:version 3.0 :xmlns:xs "http://www.w3.org/2001/XMLSchema"} (xsl/param {:name "factor" :as "xs:integer"}) (xsl/template {:match "num"} (xsl/copy (xsl/value-of {:select "xs:int(.) * $factor"}))))) (xslt/transform (xslt/compile-sexp xslt) {:factor 10} "<num>1</num>") ;;=> "<num>10</num>" ```
(transform-to-file executables source target)
(transform-to-file executables params source target)
Execute one or more XSLT transformations on the given source file.
See also transform
.
Example:
;; Define an XSLT transformation.
(def stylesheet
(xslt3-identity (xsl/template {:match "a"} [:b])))
;; Compile the stylesheet and use it to transform "<a/>".
;;
;; Save the result of the transformation into "/tmp/b.xml".
(xslt/transform-to-file (xslt/compile-sexp stylesheet)
"<a/>"
(io/file "/tmp/b.xml")
Execute one or more XSLT transformations on the given source file. See also [[transform]]. Example: ``` ;; Define an XSLT transformation. (def stylesheet (xslt3-identity (xsl/template {:match "a"} [:b]))) ;; Compile the stylesheet and use it to transform "<a/>". ;; ;; Save the result of the transformation into "/tmp/b.xml". (xslt/transform-to-file (xslt/compile-sexp stylesheet) "<a/>" (io/file "/tmp/b.xml") ```
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close