Mukulch15

Mukulch15

Get elements with no textual values in XML in elixir

I am trying to parse the following XML doc using SweetXML library.

<A>
    <B>23</B>
    <B>34</B>
    <B></B>
</A>

However when I try to parse the document using: xpath(~x"/A/B/text()"l)
I get the response ['23', '34']. Is there anyway I can get the list with nil value for the element having no text ? The response that I expect is: ['23', '34', nil].

Most Liked

OvermindDL1

OvermindDL1

For note, Meeseeks follows the same pattern for XPath queries:

iex(14)> doc = Meeseeks.parse(xmlString, :xml)
#Meeseeks.Document<{...}>

iex(15)> Meeseeks.all(doc, xpath("/A/B/text()"))
[#Meeseeks.Result<{ 23 }>, #Meeseeks.Result<{ 34 }>]

iex(16)> Meeseeks.all(doc, xpath("/A/B")) |> Enum.map(&Meeseeks.one(&1, xpath("text()")))
[#Meeseeks.Result<{ 23 }>, #Meeseeks.Result<{ 34 }>, nil]

iex(17)> Meeseeks.all(doc, xpath("/A/B")) |> Enum.map(&Meeseeks.text(&1))                
["23", "34", ""]
Mukulch15

Mukulch15

So I was able to find two methods of achieving the required result.

  1. stream_tags(xmldoc, :B) |> Stream.map( fn {:B, doc} -> doc |> SweetXml.xpath(~x"./text()") end) |> Enum.to_list

  2. xpath(xml, ~x"//A/B"l, number: ~x"text()") |> Enum.map(fn %{number: number} -> number end)
    Thanks everyone for your responses.

tangui

tangui

The following XPath expression will return the string representation of your B elements:

/A/B/string()

or

/A/B/number()

if you’re expecting a number (NaN will be returned in the “empty” ,B/> case). You can test it here: https://www.freeformatter.com/xpath-tester.html#

Haven’t tested with SweetXML though.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement