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.

First Post!

BrightEyesDavid

BrightEyesDavid

I’m not certain and my XML is rusty (not that it was ever good), but could it be that you need to include the xsi namespace in your XPath?

~x[./complexTypeTwo/@xsi:type]s

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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement