marklogicmarklogic-9marklogic-10

Marklogic tde templates how to use string-join for numeric values


Is it possible to use string-join for numeric values in marklogic template?

let json = xdmp.toJSON({
    "instance": {
        "uri": "/A/Uri/of/some/type.json",
        "types": [
            "1",
            "2",
            "3"
        ]
    }
    });

    let tpl = xdmp.toJSON({
        "template": {
            "context": "/instance",
            "enabled": true,
            "rows": [
                {
                    "schemaName": "namespace",
                    "viewName": "uri2types",
                    "columns": [
                        {
                            "name": "uri",
                            "scalarType": "anyURI",
                            "val": "./uri",
                            "nullable": true,
                            "invalidValues": "ignore"
                        }
                        ,
                        {
                            "name": "type",
                            "scalarType": "string",
                            "val": "fn:string-join(./types, ', ')"
                        }
    
                    ]
                }
            ]
        }
    });

tde.validate([tpl]);
tde.nodeDataExtract([json], [tpl]);

And this code above works correct for string values but when I change types to array of numeric values I got error.

 "types": [
            1,
            2,
            3
        ]

Is it possible to cast numbers to strings before join?


Solution

  • Yes, you can adjust the XPath selecting the types numbers, and turn them into strings using the fn:string() function:

    "val": "fn:string-join(./types/fn:string(.), ', ')"
    

    Your example input has them as strings anyhow, but the above will work if they are strings or numbers in the JSON array.