jeramyRR

jeramyRR

Parsing slightly complex XML using Sweet_XML

I have some xml that has some complex types that inherit from other complex types. This looks something like:

<complexTypeTwo xsi:type="ns1:ComplexTypeOne">
    <identifier>Two</identifier>
    <some>thing</some>
    <other>stuff</other>
</complextTypeTwo>

I’m having a very hard time getting the xsi:type attribute value using sweet_xml.
Right now I’m trying to have my xml mapped to a struct using

import SweetXml
xml 
|> parse() 
|> xmap(
...
complex_base_type: ~x[./complexTypeTwo/@type]s,
...
)

That isn’t working, and I’m not finding anything in the docs that’s helping.
I’m hoping someone here has already solved this problem, and can point me in the right direction.

Most Liked

jeramyRR

jeramyRR

I tried that as well, but no joy.

chgeuer

chgeuer

This is certainly what you want.

import SweetXml

"""
<complexTypeTwo xsi:type="ns1:ComplexTypeOne" xmlns:xsi="http://www.w3.org/2001/XMLSchema">
    <identifier xmlns="urn:bar">Two</identifier>
    <some>thing</some>
    <other>stuff</other>
</complexTypeTwo>
"""
|> parse(namespace_conformant: true)
|> xmap([
   complex_base_type: ~x[/complexTypeTwo/@foo:type]s |> add_namespace("foo", "http://www.w3.org/2001/XMLSchema"),
   identifier: ~x[bar:identifier/text()]ls  |> add_namespace("bar", "urn:bar"),
   some: ~x[some/text()]ls,
   other: ~x[other/text()]ls,
])

You cannot ‘just’ say you want xsi:type without saying which XML namespace the xsi prefix corresponds to. The actual source doc from your sample wasn’t namespace compliant in the first place. You see the xmlns:xsi="..." binding at the root elem.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement