Mukulch15

Mukulch15

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] .

Showing Posts 1 to 8

NobbZ

NobbZ

As I understand your xpath you are explicitly asking for the text, but since there is non, you won’t get it in the result.

Mukulch15

Mukulch15 OP

Thanks for the response. But is there any way around this ? It will be really helpful even if I can get the count of all B elements. That way I can compare with the length of the list and figure out how many such elements are there.

hassan

hassan

Why not just get the elements and parse them yourself?

axelson

axelson

Scenic Core Team

Looking through the SweetXML docs I don’t see a way to get empty nodes so you’re probably best off asking the maintainer directly.

Mukulch15

Mukulch15 OP

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: Free Online XPath Tester / Evaluator - FreeFormatter.com

Haven’t tested with SweetXML though.

Mukulch15

Mukulch15 OP

Thanks a lot. I found some solutions to the question and posted them here. They seem to be working.

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", ""]
— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews