lukeses

lukeses

SweetXML / Xpath - parsing a parent of node

Hi,

I have a problem with parsing an XML. I’m trying to get a parent of the node for number of nodes.

I use for that purpose SweetXml library (GitHub - kbrw/sweet_xml · GitHub)

A script shows, what I’m trying to achieve:

import SweetXml

xml =
"""
<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Con>
    <ID>123456782</ID>
    <StatusData>
      <StatusCode>A</StatusCode>
    </StatusData>
    <StatusData>
      <StatusCode>A</StatusCode>
    </StatusData>
  </Con>
</Response>
"""

xpath(xml, ~x"//Response/Con/StatusData"l,
  status_code: ~x"./StatusCode/text()"s,
  id: ~x"./../ID/text()"s
) |> IO.inspect(label: "RESULT")


# Terminal response:
# RESULT: [%{id: "", status_code: "A"}, %{id: "", status_code: "A"}]

# Expected Terminal response:
# RESULT: [%{id: "123456782", status_code: "A"}, %{id: "123456782", status_code: "A"}]```

Below, I’m presenting sth similar, which works:

xpath(xml, ~x"//StatusData/ancestor::Con/ID/text()"sl) |> IO.inspect(label: "RESULT_2")

# Terminal response:
# RESULT_2: ["123456782", "123456782"]

Do you have any idea, what should be instead of the line: id: ~x"./../ID/text()"s?

BTW, I know I may do this step by step: getting ID, then playing with Enum.map, but it looks like my case should be supported by SweetXML/Xpath.

First Post!

dimitarvp

dimitarvp

I’m not seeing that in your example XML though? How could it be located if it doesn’t exist?

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement