Search code examples
spring-bootfreemarker

How to get the values of several xml tags and pass them to the url as a string using Freemarker


<?xml version="1.0" encoding="UTF-8"?>
        <tns:Map xmlns:tns="urn://xxx" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            <tns:type>2</tns:type>
            <tns:code>CAT</tns:code>
            <tns:code>DOG</tns:code>
        </tns:Map>

it needs to be like this - http://test.com/test/CAT,DOG

i tried like this(and other options):

<#ftl ns_prefixes={"ns1":"urn://xxx", "ns2":"urn://xxx"}>
    <#assign type = body["ns1:Request"]["ns1:Content"]["ns2:Map"]["ns2:type"]>
    <#if type?number == 1> 
     http://test.com/somePath
    <#elseif type?number == 2>
     <#list body["ns1:Request"]["ns1:Content"]["ns2:Map"] as codeList>
     <#assign codes = codeList["ns2:code"]> 
     http://test.com/test/${codes} 
     </#list>
    </#if>

but it throws an syntax error.

And with this code I can get only the first element, but how to display all? using http://test.com/test/${codes?join(", ")} doesn't help:

<#ftl ns_prefixes={"ns1":"urn://xxx", "ns2":"urn://xxx"}>
    <#assign type = body["ns1:Request"]["ns1:Content"]["ns2:Map"]["ns2:type"]>
    <#assign codes = body["ns1:Request"]["ns1:Content"]["ns2:Map"]["ns2:code"]?sequence>
    <#if type?number == 1> 
     http://test.com/somePath
    <#elseif type?number == 2> 
     http://test.com/test/${codes[0]}
    </#if>

how to write list tns:code to variable?


Solution

  • solution:

    <#ftl ns_prefixes={"ns1":"urn://xxx", "ns2":"urn://xxx"}>
        <#assign type = body["ns1:Request"]["ns1:Content"]["ns2:Map"]["ns2:type"]>
        <#if type?number == 1> 
         http://test.com/somePath
        <#elseif type?number == 2>
         <#assign codes = body["ns1:Request"]["ns1:Content"]["ns2:Map"]["ns2:code"]>
         http://test.com/test/${codes?join(",")}
        </#if>