Skip to content

Commit b04912d

Browse files
committed
Support for rdf:XMLLiterals in XML decoder
Thanks @doughsay for the hint on kbrw/sweet_xml#45 in kbrw/sweet_xml#58
1 parent 8d20e22 commit b04912d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/sparql/query/result/formats/xml_decoder.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ defmodule SPARQL.Query.Result.XML.Decoder do
8080
node |> xpath(~x"./text()"s) |> RDF.Literal.new(language: language)
8181
(datatype = xpath(node, ~x"./@datatype"s)) ==
8282
"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral" ->
83-
require IEx ; IEx.pry
84-
node |> xpath(~x"./text()"s) |> RDF.Literal.new(datatype: datatype)
83+
node |> xpath(~x"*") |> node_to_xml() |> RDF.Literal.new(datatype: datatype)
8584
(datatype = xpath(node, ~x"./@datatype"s)) != "" ->
8685
node |> xpath(~x"./text()"s) |> RDF.Literal.new(datatype: datatype)
8786
true ->
@@ -95,4 +94,13 @@ defmodule SPARQL.Query.Result.XML.Decoder do
9594
defp decode_value(value),
9695
do: raise "Invalid query result: #{inspect node}"
9796

97+
# TODO: This is quite hacky! Is there a better solution? - https://github.com/kbrw/sweet_xml/issues/58
98+
# TODO: Remove this when https://github.com/kbrw/sweet_xml/pull/45 gets merged
99+
defp node_to_xml(node) do
100+
[node]
101+
|> :xmerl.export(:xmerl_xml)
102+
|> List.flatten()
103+
|> List.to_string()
104+
|> String.replace_leading(~s[<?xml version="1.0"?>], "")
105+
end
98106
end

test/sparql/query/result/xml_decoder_test.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ defmodule SPARQL.Query.Result.XML.DecoderTest do
175175
]}}
176176
end
177177

178-
@tag skip: "TODO"
179178
test "SELECT result with rdf:XMLLiteral" do
180179
assert Query.Result.XML.decode("""
181180
<sparql xmlns="http://www.w3.org/2005/sparql-results#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/sw/DataAccess/rf1/result2.xsd">
@@ -193,7 +192,7 @@ defmodule SPARQL.Query.Result.XML.DecoderTest do
193192
</results>
194193
</sparql>
195194
""") == {:ok, %Query.ResultSet{
196-
variables: ~w[x hpage name mbox age blurb friend],
195+
variables: ~w[blurb],
197196
results: [
198197
%Query.Result{bindings: %{
199198
"blurb" => RDF.Literal.new(

0 commit comments

Comments
 (0)