Newxan
Hi, I’ve got a database XML dump from an old wordpress site that I want to import into a project.
I’musing sweet_xml and it’s working fine for the simple structures.
However when I encounter lists of nested fields such as wordpress’s metadata fields I can’t get sweet_xml to pick up the values. I’ve reproduced my problem in a minimal way:
import.ex
defmodule Mix.Tasks.Import do
use Mix.Task
import SweetXml
@target_file "./data.xml"
def run(_args) do
Mix.Task.run("app.start")
import_dump()
end
defp import_dump do
tree = File.stream!(@target_file)
rows =
SweetXml.xpath(
tree,
~x"/rss/channel/item"l,
slug: ~x"./wp:post_name/text()"s,
company_logo: ~x"./wp:postmeta[wp:meta_key = '_thumbnail_id']/wp:meta_value/text()"s,
edited_at: ~x"./wp:postmeta[wp:meta_key = '_edit_last']/wp:meta_value/text()"s
)
IO.inspect(rows)
end
end
data.xml
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.2/">
<channel>
<item>
<wp:post_id>7068</wp:post_id>
<wp:post_name>
<![CDATA[a-post-name]]>
</wp:post_name>
<wp:postmeta>
<wp:meta_key>
<![CDATA[_edit_last]]>
</wp:meta_key>
<wp:meta_value>
<![CDATA[1]]>
</wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key>
<![CDATA[_thumbnail_id]]>
</wp:meta_key>
<wp:meta_value>
<![CDATA[11111]]>
</wp:meta_value>
</wp:postmeta>
</item>
</channel>
</rss>
The output But the output I’m getting no matter what I try is:
[%{company_logo: "", edited_at: "", slug: "\na-post-name\n "}]
As you can see the company_logo and edited_at fields are always empty.
I’m not really sure where to go from here, as far as I can tell the xpath syntax itself is correct. But I am unsure about how you’re supposed to use the sweet_xml sigil modifiers when there is nested data such as this.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
srowley
There is a hint in your output - the
CDATAelements are wrapped in newline characters and have trailing spaces, i.e. the value for slug in your example is"\napost-name\n ".That means that your Xpath query is not finding anything for the slug or company logo nodes because, for example, it is looking for the key
_edit_lastwhen that key is\n_edit_last\n.Your query would work like this:
I don’t know enough about XML to understand why the newlines/spaces are there; if there is some rule associated with that I suppose you could also just apply it and keep using
=instead ofcontains, akin to (I didn’t test this one):rjk
I can confirm that the suggestion from @srowley works, this code is tested in an iex repl:
i also added a transform_by function from sweet_xml that cleans up those newlines and extra whitespaces.
Also not sure why those are added in the first place as I can’t see them in the XML itself, seems like the underlying xmerl library (that sweet_xml uses) or sweet_xml itself does something weird to those cdata nodes?
But at least with this code you could go on with the task at hand
Newxan
Using ‘contains’ solves my problem. Thanks for the help!
srowley
As a postscript, it occurred to me later that all that is happening here is that the whitespace within the markup is being preserved - looking at the sample within the offending tags there is a newline, the CDATA node, some spaces, and then another newline. The :xmerl documentation confirms this and provides an example very similar to this one.
Newxan
Doh! That makes a lot of sense
I always use a auto-formatter when viewing code. In this case using
https://docs.python.org/2/library/xml.dom.minidom.htmlto make it look readable.But doing that broke the export from wordpress. I can confirm that the original has no newlines whenever there is CDATA…
Thanks for the follow up, its been bugging me ever since not knowing why this occurred.