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 (https://github.com/kbrw/sweet_xml)
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
I’m not seeing that in your example XML though? How could it be located if it doesn’t exist?
Popular in Questions
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
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Hey guys.
I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
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
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
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
Other popular topics
can someone please explain to me how Enum.reduce works with maps
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New








