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.

1 Like

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

I tried that as well, but no joy.

1 Like

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.