Home > front end >  Convert child nodes into a single string using XQuery in ML 10
Convert child nodes into a single string using XQuery in ML 10

Time:01-21

We have an application whose backend ML response format is JSON and we have a element with both text and nodes as below. <Title>Header <bold>abc</bold></Title>

Now, while converting the above node to JSON, we get the output as "Title": {"bold": "abc","_value": "Header "}

But to apply formatting in the UI ,we need the complete(text node) data as _value "Title": {"_value": "Header <bold>abc</bold>"}

I tried using xdmp:quote() to convert node to string ,but I need to retain the <Title> as node.Could anyone please help me with this?

CodePudding user response:

You could normalize the XML content, "flattening" out the inline elements with xdmp:quote() and turning them into escaped strings that are part of the title value, so that the JSON conversion process is just turning that title element text() into a JSON property String.

A simple XSLT that achieves that with your sample XML:

import module namespace json = "http://marklogic.com/xdmp/json"
    at "/MarkLogic/json/json.xqy";

let $doc := <Title>Header <bold>abc</bold></Title>

let $xslt := 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
  xmlns:xdmp="http://marklogic.com/xdmp" extension-element-prefixes="xdmp">
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <!--quote any inline elements to make them escaped strings that are part of the parent element value, rather than child elements-->
  <xsl:template match="bold|italics|underline">
    <xsl:sequence select="xdmp:quote(.)"/>
  </xsl:template>
  
</xsl:stylesheet>
let $normalized-doc := xdmp:xslt-eval($xslt, $doc)

let $custom :=
  let $config := json:config("custom") => map:with("whitespace", "ignore" )
  return $config
return json:transform-to-json($normalized-doc, $custom)

and produces the following output:

{
"Title": "Header <bold>abc</bold>"
}
  • Related