9

I have a variable in xQuery of type xs:string with the value of an encoded HTML snippet (the content of a twitter tweet). It looks like this:

Headlines-Today • AP sources: <b>Obama</b> pick for Justice post withdraws : News - Rest Of World - <a href="http://shar.es/mqMAG">http://shar.es/mqMAG</a>

When I try to write this out in an HTML block, I need the string to be unescaped so that the HTML snippet will be interpreted by the browser. Instead the string is getting written out as is and the browser is rendering it as just text (so you see <a href="blah.... ). Here's how I'm writing out this string:

{$entry/atom:content/text()}

How can I have the escaped characters unencoded so it writes < rather tha &lt; ?

I've tried to do a replacelike this but it always replaces the &lt; with &lt; !

fn:replace($s, "&lt;", "<")

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
mbrevoort
  • 5,075
  • 6
  • 38
  • 48

4 Answers4

3

In MarkLogic you can use the below query:

let $d := '<a>&lt;c&gt;asdf&lt;/c&gt;</a>' 

return xdmp:unquote ($d)
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
kadalamittai
  • 2,076
  • 1
  • 16
  • 19
3

in eXist, use util:parse():

util:parse(concat("<top>","&lt;c&gt;asdf&lt;/c&gt;",</top>")‌​)
Iain Ward
  • 9,850
  • 5
  • 34
  • 41
Chris Wallace
  • 495
  • 3
  • 11
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Yaroslav Nov 16 '12 at 07:48
  • 1
    As indeed it has :-( - The eXist library util has a function parse which will parse a string provided it is well-formed XML. Uniless it is know that the string has a single root, it is best to add a top element: util:parse(concat("","<c>asdf</c>",")) – Chris Wallace Feb 17 '13 at 16:09
  • Better add your comment to the answer, edit and add what is needed – Yaroslav Feb 17 '13 at 16:49
  • at least for eXist-db v4 or earlier this is true. In eXist-db 5 the function has been replaced by standard Query 3 functions, see below answer https://stackoverflow.com/questions/2611480/how-to-unencode-escaped-xml-with-xquery/70844757#answer-70844757 – Benjamin W. Bohl Jan 25 '22 at 07:25
2

Depends on which XQuery processor you are using... The easiest way is to be using a processor that has an extension that handles this for you. For instance, with Saxon and the following XML:

<a>&lt;c&gt;asdf&lt;/c&gt;</a>

You can write an XQuery that uses the saxon:parse() function to do what you want:

declare namespace saxon = "http://saxon.sf.net/";

<a>{
  saxon:parse(doc('test.xml')/a)
}</a>

The result from that is:

<a>
  <c>asdf</c>
</a>

I think most(?) XQuery processors will have an extension to do this for you. Hope that helps.

Sofia
  • 771
  • 1
  • 8
  • 22
ksclarke
  • 489
  • 5
  • 16
0

since XQuery 3 you can use the parse-xml() or parse-xml-fragment() functions. See also: