Skip to content

Commit

Permalink
Support for rdf:XMLLiterals in XML decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotto committed Mar 14, 2018
1 parent 8d20e22 commit b04912d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/sparql/query/result/formats/xml_decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ defmodule SPARQL.Query.Result.XML.Decoder do
node |> xpath(~x"./text()"s) |> RDF.Literal.new(language: language)
(datatype = xpath(node, ~x"./@datatype"s)) ==
"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral" ->
require IEx ; IEx.pry
node |> xpath(~x"./text()"s) |> RDF.Literal.new(datatype: datatype)
node |> xpath(~x"*") |> node_to_xml() |> RDF.Literal.new(datatype: datatype)
(datatype = xpath(node, ~x"./@datatype"s)) != "" ->
node |> xpath(~x"./text()"s) |> RDF.Literal.new(datatype: datatype)
true ->
Expand All @@ -95,4 +94,13 @@ defmodule SPARQL.Query.Result.XML.Decoder do
defp decode_value(value),
do: raise "Invalid query result: #{inspect node}"

# TODO: This is quite hacky! Is there a better solution? - https://github.com/kbrw/sweet_xml/issues/58
# TODO: Remove this when https://github.com/kbrw/sweet_xml/pull/45 gets merged
defp node_to_xml(node) do
[node]
|> :xmerl.export(:xmerl_xml)
|> List.flatten()
|> List.to_string()
|> String.replace_leading(~s[<?xml version="1.0"?>], "")
end
end
3 changes: 1 addition & 2 deletions test/sparql/query/result/xml_decoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ defmodule SPARQL.Query.Result.XML.DecoderTest do
]}}
end

@tag skip: "TODO"
test "SELECT result with rdf:XMLLiteral" do
assert Query.Result.XML.decode("""
<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">
Expand All @@ -193,7 +192,7 @@ defmodule SPARQL.Query.Result.XML.DecoderTest do
</results>
</sparql>
""") == {:ok, %Query.ResultSet{
variables: ~w[x hpage name mbox age blurb friend],
variables: ~w[blurb],
results: [
%Query.Result{bindings: %{
"blurb" => RDF.Literal.new(
Expand Down

0 comments on commit b04912d

Please sign in to comment.