xmlxpathfreemarker

Why Freemarker Template is not parsing the xml with namespace


my earlier question answered here

How to separate XML tags in freemarker in body function

But my xml is coming with namespace, But when i am trying to add the namespace in template its not parsing my xml data. Below is my modified template and sample xml and expected output.

<#ftl ns_prefixes={"D": "http://www.test.org/"}>
<#assign DEALS = body[".//D:MESSAGE/.//D:DEALS[following-
sibling::D:DOCUMENT_SETS]/.//D:DEAL"]>
{
loans:[
 <#list DEALS as d>
    {
         ${d["name(./*)"]} : ${d[".//D:AddressLineText"]}
    }
 </#list>       
    ]
}

Sample Xml

<MESSAGE xmlns="http://www.test.org/">
 <DEALS>
  <DEAL>
    <AddressLineText>Addr1</AddressLineText>
   </DEAL>
   <DEAL>
    <AddressLineText>Addr2</AddressLineText>
   </DEAL>
  </DEALS>
 <DOCUMENT_SETS>
  <DEALS>
    <DEAL>
        <LateChargeAmount>110</LateChargeAmount>
    </DEAL>
   </DEALS>
  </DOCUMENT_SETS>
 </MESSAGE>

Expected output

  {
  "loans": [
      {
          AddressLineText : Addr1
       },
        {
     AddressLineText : Addr2
        }
       ]
    }

Solution

  • if your root element actually looks like this:

    <MESSAGE xmlns="http://www.test.org/">
    

    or it is part of this namespace: "http://www.test.org/"

    I suppose this xpath

    .//D:MESSAGE/.//D:DEALS[following-sibling::D:DOCUMENT_SETS]/.//D:DEAL
    

    needs to be

    .//D:MESSAGE/D:DEALS[following-sibling::D:DOCUMENT_SETS]/D:DEAL