You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case you're not familiar with my [dataweave-scripts](https://github.com/alexandramartinez/dataweave-scripts) GitHub repo, it's the place where I keep some of the scripts I've created to help the community with transformation questions or simply some scripts that have been handy to me.
18
12
19
13
In this post, I want to introduce you to two transformations I added because of a use case I came up with last week. Basically to help clean an XML or HTML to publish a script in a WordPress article.
@@ -24,11 +18,11 @@ This problem started because I had written a blog post in a WordPress-based blog
24
18
25
19

26
20
27
-
The fix was simple. Instead of having the regular < and > characters pasted in the code snippet, I had to use < and > respectively.
21
+
The fix was simple. Instead of having the regular `<` and `>` characters pasted in the code snippet, I had to use `<` and `>` respectively.
28
22
29
23
*(Thanks so much Julian Duque for providing the fix! I had no idea about this issue in WordPress* 🤗*)*
30
24
31
-
For example, instead of writing <plugin>, I had to replace it with <plugin>
25
+
For example, instead of writing `<plugin>`, I had to replace it with `<plugin>`
32
26
33
27
I thought to myself: If I need to keep doing this for future blog posts, maybe I can create a DataWeave transformation to fix this for me so I can just easily copy and paste the new clean snippet.
34
28
@@ -44,26 +38,32 @@ output text/plain
44
38
---
45
39
write(payload,"application/xml")
46
40
replace "<?xml version='1.0' encoding='UTF-8'?>\n" with ""
47
-
replace "<" with "<"
48
-
replace ">" with ">"
41
+
replace "<" with "<"
42
+
replace ">" with ">"
49
43
```
50
44
45
+
> [!PLAYGROUND]
46
+
> [Open in the Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=alexandramartinez%2Fdataweave-scripts&path=functions%2Fclean-xml)
47
+
51
48
However, I quickly ran into issues when I tried to clean an HTML code snippet using this same transformation. This is how I came up with the second approach.
52
49
53
50
## Second approach: plain text input
54
51
55
-
This time I decided to use a plain text input instead of an XML input format. This way, both XML and HTML code snippets could be used as the input and I wouldn't need to use the write() function in the first place.
52
+
This time I decided to use a plain text input instead of an XML input format. This way, both XML and HTML code snippets could be used as the input and I wouldn't need to use the `write()` function in the first place.
56
53
57
54
```dataweave
58
55
%dw 2.0
59
56
output text/plain
60
57
---
61
58
payload
62
-
replace "<" with "<"
63
-
replace ">" with ">"
59
+
replace "<" with "<"
60
+
replace ">" with ">"
64
61
```
65
62
66
-
Plus, I got rid of one replace() because I no longer needed to remove the XML header.
63
+
> [!PLAYGROUND]
64
+
> [Open in the Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=alexandramartinez%2Fdataweave-scripts&path=functions%2Fclean-html)
65
+
66
+
Plus, I got rid of one `replace()` because I no longer needed to remove the XML header.
67
67
68
68
It's a short post, but I hope it's insightful for you all 🤗 I'm sure I'll keep using this example in the Playground to modify my WordPress posts in the future.
0 commit comments