Skip to content

Commit 4430a5f

Browse files
committed
Treat rdf:version as an element attribute, not just on the root.
1 parent 35f8f0e commit 4430a5f

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

lib/rdf/rdfxml/reader.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ class EvaluationContext
3131
attr_accessor :graph
3232
attr_accessor :li_counter
3333
attr_accessor :direction
34-
attr_accessor :version # RDF Version, mirrored here
34+
attr_accessor :version
3535

36-
def initialize(base, element, graph, version: nil, &cb)
36+
def initialize(base, element, graph, &cb)
3737
# Initialize the evaluation context, [5.1]
3838
self.base = RDF::URI(base)
3939
@uri_mappings = {}
4040
@language = nil
4141
@direction = nil
42-
@version = version
42+
@version = nil
4343
@graph = graph
4444
@li_counter = 0
4545

@@ -74,6 +74,7 @@ def extract_from_ancestors(el, &cb)
7474
# Extract Evaluation Context from an element
7575
def extract_from_element(el, &cb)
7676
self.language = el.language if el.language
77+
self.version = el.version if el.version
7778
# Direction only used in RDF 1.2 or greater
7879
self.direction = el.direction if el.direction && self.version.to_s >= "1.2"
7980
# Direction requires the appropriate ITS version
@@ -112,7 +113,7 @@ def base=(b)
112113
end
113114

114115
def inspect
115-
v = %w(base subject language direction).map {|a| "#{a}='#{self.send(a).nil? ? 'nil' : self.send(a)}'"}
116+
v = %w(base subject language direction version).map {|a| "#{a}='#{self.send(a).nil? ? 'nil' : self.send(a)}'"}
116117
v << "uri_mappings[#{uri_mappings.keys.length}]"
117118
v.join(",")
118119
end
@@ -213,11 +214,7 @@ def each_statement(&block)
213214
if rdf_nodes.size == 0
214215
# If none found, root element may be processed as an RDF Node
215216

216-
# Extract RDF version from root
217-
@version = root.version
218-
add_debug(root, "version: #{@version.inspect}")
219-
220-
ec = EvaluationContext.new(base_uri, root, @graph, version: @version) do |prefix, value|
217+
ec = EvaluationContext.new(base_uri, root, @graph) do |prefix, value|
221218
prefix(prefix, value)
222219
end
223220

@@ -227,13 +224,8 @@ def each_statement(&block)
227224
log_fatal "node must be a proxy not a #{node.class}" unless node.is_a?(@implementation::NodeProxy)
228225
# XXX Skip this element if it's contained within another rdf:RDF element
229226

230-
# Extract RDF version from node
231-
# XXX potentially, one node is processed with version "1.2" and others are parsed without a version.
232-
@version = node.version
233-
add_debug(root, "version: #{@version.inspect}")
234-
235227
# Extract base, lang, direction, version and namespaces from parents to create proper evaluation context
236-
ec = EvaluationContext.new(base_uri, nil, @graph, version: @version)
228+
ec = EvaluationContext.new(base_uri, nil, @graph)
237229
ec.extract_from_ancestors(node) do |prefix, value|
238230
prefix(prefix, value)
239231
end
@@ -419,6 +411,7 @@ def nodeElement(el, ec)
419411
when "parseType" then parseType = attr.value
420412
when "resource" then resourceAttr = attr.value
421413
when "nodeID" then nodeID = attr.value
414+
when "version" then nil # version already extracted
422415
else attrs[attr] = attr.value
423416
end
424417
elsif attr.namespace.href == RDF::ITS.to_s

spec/reader_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,19 @@
296296
<http://example.org/joe> <http://example.org/name> "bar"@en--ltr .
297297
)
298298
},
299+
"Language with version and direction on element directly": {
300+
input: %(<?xml version="1.0" ?>
301+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
302+
xmlns:ex="http://example.org/"
303+
xmlns:its="http://www.w3.org/2005/11/its">
304+
<rdf:Description rdf:about="http://example.org/joe">
305+
<ex:name xml:lang="en" rdf:version="1.2" its:version="2.0" its:dir="ltr">bar</ex:name>
306+
</rdf:Description>
307+
</rdf:RDF>),
308+
expected: %(
309+
<http://example.org/joe> <http://example.org/name> "bar"@en--ltr .
310+
)
311+
},
299312
"Direction with no language": {
300313
input: %(<?xml version="1.0" ?>
301314
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

0 commit comments

Comments
 (0)