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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









