saxon-xslt 0.5.1-java → 0.6.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69ba3f6f9956b33cc7dc283bd45612363ad81f63
4
- data.tar.gz: 2b1391e74ef5bdcdd0588ceffaf9be97df313ff8
3
+ metadata.gz: af91b7c69d754725f26d768b1866057099101db0
4
+ data.tar.gz: 808008c7ca54ec9914f6317664977bb0f9ead2c3
5
5
  SHA512:
6
- metadata.gz: 0903c2754a622c08d40a901534443ea3e9c54a41206a124d1ee8e066d3db5b10ae28fe0320b4a63d720c9781bd531e7c1160f76b7cf493b7f506911fcfabaabb
7
- data.tar.gz: 46ee2706c0df6d6dbe1ab979fb02564533fd3c0546104cad49c3bf5b4efcec937fe1b704ad2c9c6f94cb5061ec51971b30871c764e35878729892e73229498f3
6
+ metadata.gz: eadf0c7d72d149475479e11845ae9618506f33741b15c4878ff9f44c2a1f5b0a2ae3b186db6cebbfc5258dbe7ba1718152dd9c80c0a02bcda98c501665fff3bb
7
+ data.tar.gz: c350adfe866ea020afa4b81b3e7a949d2c18d8942fb40b8d3063ca6c29ecbb75151f8552e968a088e360405ceed653e13819f9b7a947fd91e2639089ba8d57c6
data/README.md CHANGED
@@ -35,8 +35,25 @@ input = Saxon.XML(File.open('/path/to/your.xml'))
35
35
  output = transformer.transform(input)
36
36
  ```
37
37
 
38
+ XSL parameters can be passed to #transform as a flat array of name, value pairs, or as a hash, e.g.
39
+
40
+ ```ruby
41
+ output = transformer.transform(input, ["my-param", "'my-value'",
42
+ "my-other-param", "/take-from@id"])
43
+
44
+ # or
45
+
46
+ output = transformer.transform(input, {"my-param" => "'my-value'",
47
+ "my-other-param" => "/take-from@id"})
48
+ ```
49
+
50
+ For those familiar with the Saxon API, names are passed directly to the QName constructor.
51
+
52
+ Values are evaluated as XPath expressions in context of the document being transformed; this means
53
+ that, to pass a string, you must pass an XPath that resolves to a string, i.e. "'You must wrap strings in quotes'"
54
+
38
55
  ## Saxon version
39
- `saxon-xslt` 0.5 includes Saxon HE 9.5.1.7
56
+ `saxon-xslt` 0.6 includes Saxon HE 9.5.1.7
40
57
 
41
58
  ## Differences between Saxon and Nokogiri
42
59
 
data/lib/saxon/s9api.rb CHANGED
@@ -7,5 +7,6 @@ module Saxon
7
7
  module S9API
8
8
  java_import 'net.sf.saxon.s9api.Processor'
9
9
  java_import 'net.sf.saxon.s9api.XdmDestination'
10
+ java_import 'net.sf.saxon.s9api.QName'
10
11
  end
11
12
  end
data/lib/saxon/xml.rb CHANGED
@@ -14,6 +14,7 @@ module Saxon
14
14
  module XML
15
15
  # Parse an XML File or String into a Document object
16
16
  class Document
17
+ # @api private
17
18
  # @param [Saxon::Processor] processor The processor object which should
18
19
  # be used to build the document
19
20
  # @param [String, File, IO] string_or_io The input XML
@@ -26,6 +27,13 @@ module Saxon
26
27
  new(xdm_document)
27
28
  end
28
29
 
30
+ # @param [String] expr The XPath expression to evaluate
31
+ # @return [net.sf.saxon.s9api.XdmValue] return the value, node, or
32
+ # nodes selected
33
+ def xpath(expr)
34
+ processor.to_java.new_xpath_compiler.evaluate(expr, @xdm_document)
35
+ end
36
+
29
37
  # @api private
30
38
  def initialize(xdm_document)
31
39
  @xdm_document = xdm_document
@@ -1,5 +1,5 @@
1
1
  module Saxon
2
2
  module XSLT
3
- VERSION = "0.5.1"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
data/lib/saxon/xslt.rb CHANGED
@@ -15,6 +15,7 @@ module Saxon
15
15
  module XSLT
16
16
  # a Stylesheet transforms input (XML) into output
17
17
  class Stylesheet
18
+ # @api private
18
19
  # @param processor [Saxon::Processor] the Saxon processor object
19
20
  # @param string_or_io [File, IO, String] the input XSLT
20
21
  # @param opts [Hash] Stylesheet and input options
@@ -24,6 +25,15 @@ module Saxon
24
25
  new(source)
25
26
  end
26
27
 
28
+ # Compile a stylesheet from an existing Saxon::XML instance of an XSLT
29
+ # source
30
+ #
31
+ # @param [Saxon::XML::Document] source the input XSLT as an XML document
32
+ # @return [Saxon::XSLT::Stylesheet] the compiled XSLT stylesheet
33
+ def self.parse_stylesheet_doc(document)
34
+ new(document)
35
+ end
36
+
27
37
  # @param [Saxon::XML::Document] source the input XSLT as an XML document
28
38
  def initialize(source)
29
39
  processor = source.processor
@@ -32,17 +42,64 @@ module Saxon
32
42
  end
33
43
 
34
44
  # Transform an input document
35
- # @param [Saxon::XML::Document] document the XML Document object to
45
+ #
46
+ # To pass global parameters you can pass a hash with parameter names as
47
+ # keys and values as XPath expressions as values: to pass a string value,
48
+ # you need to pass it quoted: `"'string'"`. An unquoted string is an
49
+ # XPath reference into the document being transformed.
50
+ #
51
+ # @param [Saxon::XML::Document] document the XML Document object to
36
52
  # transform
37
- # @return a Saxon::XML::Document object
38
- def transform(document)
53
+ # @param params [Hash,Array] xsl params to set in the xsl document
54
+ # @return [Saxon::XML::Document] the transformed XML Document
55
+ def transform(document, params = {})
39
56
  output = S9API::XdmDestination.new
40
57
  transformer = @xslt.load
41
58
  transformer.setInitialContextNode(document.to_java)
42
59
  transformer.setDestination(output)
60
+ set_params(transformer, document, params)
43
61
  transformer.transform
44
62
  Saxon::XML::Document.new(output.getXdmNode)
45
63
  end
64
+
65
+ # Transform an input document and return the result as a string.
66
+ #
67
+ # See #transform for details of params handling
68
+ # @param [Saxon::XML::Document] document the XML Document object to
69
+ # transform
70
+ # @param params [Hash,Array] xsl params to set in the xsl document
71
+ # @return [String] the transformed XML Document serialised to a string
72
+ def apply_to(document, params = {})
73
+ serialize(transform(document, params))
74
+ end
75
+
76
+ # Serialise a document to a string
77
+ #
78
+ # Not the most useful serialiser in the world. Provided for Nokogiri API
79
+ # compatibility
80
+ #
81
+ # @param [Saxon::XML::Document] document the XML Document object to
82
+ # serialise
83
+ # @return [String] the XML Document serialised to a string
84
+ def serialize(document)
85
+ document.to_s
86
+ end
87
+
88
+ private
89
+
90
+ def set_params(transformer, document, params)
91
+ case params
92
+ when Hash
93
+ params.each do |k,v|
94
+ transformer.setParameter(S9API::QName.new(k), document.xpath(v))
95
+ end
96
+ when Array
97
+ params.each_slice(2) do |k,v|
98
+ raise ArgumentError.new("Odd number of values passed as params: #{params}") if v.nil?
99
+ transformer.setParameter(S9API::QName.new(k), document.xpath(v))
100
+ end
101
+ end
102
+ end
46
103
  end
47
104
  end
48
105
  end
data/spec/fixtures/eg.xsl CHANGED
@@ -1,9 +1,10 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
3
- <xsl:template match="input">
4
- <output/>
5
- </xsl:template>
6
- <xsl:template match="output">
7
- <piped/>
8
- </xsl:template>
3
+ <xsl:param name="testparam">default</xsl:param>
4
+ <xsl:template match="input">
5
+ <output/>
6
+ </xsl:template>
7
+ <xsl:template match="output">
8
+ <piped/>
9
+ </xsl:template>
9
10
  </xsl:stylesheet>
@@ -0,0 +1,21 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
3
+ <xsl:param name="testparam">default</xsl:param>
4
+ <xsl:template match="input">
5
+ <xsl:choose>
6
+ <xsl:when test="$testparam = 'default'">
7
+ <output/>
8
+ </xsl:when>
9
+ <xsl:when test="$testparam = 'input'">
10
+ <output>
11
+ Select works
12
+ </output>
13
+ </xsl:when>
14
+ <xsl:otherwise>
15
+ <output>
16
+ <xsl:value-of select="$testparam" />
17
+ </output>
18
+ </xsl:otherwise>
19
+ </xsl:choose>
20
+ </xsl:template>
21
+ </xsl:stylesheet>
@@ -35,6 +35,12 @@ describe Saxon::XSLT do
35
35
  xslt = Saxon::XSLT::Stylesheet.new(input)
36
36
  expect(xslt).to respond_to(:transform)
37
37
  end
38
+
39
+ it "provides the parse_stylesheet_doc method to compile from a Saxon::XML::Document" do
40
+ input = processor.XML(File.open(fixture_path('eg.xsl')))
41
+ xslt = Saxon::XSLT::Stylesheet.parse_stylesheet_doc(input)
42
+ expect(xslt).to respond_to(:transform)
43
+ end
38
44
  end
39
45
 
40
46
  context "transforming a document" do
@@ -57,6 +63,63 @@ describe Saxon::XSLT do
57
63
  expect(xsl.transform(result).to_s.strip).to eq('<piped/>')
58
64
  end
59
65
  end
66
+
67
+ context "when passing global parameters at transform time" do
68
+ let(:xsl) { processor.XSLT(File.open(fixture_path('params-eg.xsl'))) }
69
+
70
+ context "using hash params" do
71
+ let(:result) { xsl.transform(xml, {"testparam" => "'non-default'"}) }
72
+
73
+ it "contains the parameter value string" do
74
+ expect(result.to_s.strip).to include("non-default")
75
+ end
76
+ end
77
+
78
+ context "using array params" do
79
+ let(:result) { xsl.transform(xml, ["testparam", "'non-default'"]) }
80
+
81
+ it "contains the parameter value string" do
82
+ expect(result.to_s.strip).to include("non-default")
83
+ end
84
+ end
85
+
86
+ context "using malformed array params" do
87
+ it "should raise ArgumentError" do
88
+ expect{xsl.transform(xml, ["testparam", "'non-default'", "'wrongo'"])}.to raise_error(ArgumentError)
89
+ end
90
+ end
91
+
92
+ context "using a selection from the input" do
93
+ let(:result) { xsl.transform(xml, ["testparam", "local-name(/input)"]) }
94
+
95
+ it "should contain the name of the tag" do
96
+ expect(result.to_s.strip).to include('Select works')
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ describe "applying a stylesheet to a document and returning a serialised XML string" do
104
+ let(:xsl) { processor.XSLT(File.open(fixture_path('eg.xsl'))) }
105
+ let(:xml) { processor.XML(File.open(fixture_path('eg.xml'))) }
106
+
107
+ it "returns XML as a string" do
108
+ result = xsl.apply_to(xml)
109
+
110
+ expect(result).to match(/<output\/>/)
111
+ end
112
+
113
+ it "correctly invokes transform" do
114
+ expect(xsl).to receive(:transform).with(xml, {})
115
+
116
+ xsl.apply_to(xml)
117
+ end
118
+
119
+ it "correctly passes params through to transform" do
120
+ expect(xsl).to receive(:transform).with(xml, {"param" => "'value'"})
121
+
122
+ xsl.apply_to(xml, {"param" => "'value'"})
60
123
  end
61
124
  end
62
125
 
@@ -68,4 +131,3 @@ describe Saxon::XSLT do
68
131
  end
69
132
  end
70
133
  end
71
-
Binary file
Binary file
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saxon-xslt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: java
6
6
  authors:
7
7
  - Matt Patterson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-18 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -109,6 +109,7 @@ files:
109
109
  - spec/fixtures/config.xml
110
110
  - spec/fixtures/eg.xml
111
111
  - spec/fixtures/eg.xsl
112
+ - spec/fixtures/params-eg.xsl
112
113
  - spec/fixtures/simple-xsl-import.xsl
113
114
  - spec/saxon/processor_spec.rb
114
115
  - spec/saxon/source_helper_spec.rb
@@ -150,6 +151,7 @@ test_files:
150
151
  - spec/fixtures/config.xml
151
152
  - spec/fixtures/eg.xml
152
153
  - spec/fixtures/eg.xsl
154
+ - spec/fixtures/params-eg.xsl
153
155
  - spec/fixtures/simple-xsl-import.xsl
154
156
  - spec/saxon/processor_spec.rb
155
157
  - spec/saxon/source_helper_spec.rb