I have xslt and below code works fine, however I need to replace "map{"abc": "yes","dDEF": "Yes","RMT": "dsfdf","PPAS": "Yes","UNITE": "Yes"}" with variable
<xsl:variable name="var_res">
<xsl:value-of select='map:for-each(map{"abc": "yes","dDEF": "Yes","RMT": "dsfdf","PPAS": "Yes","UNITE": "Yes"}, function($k, $v){
{
if($v!="Yes" and $v!="No")
then $k else ""})' />
</xsl:variable>
as below :
<xsl:variable name="var_mapValue" >
<xsl:value-of select="/root/input/collective_barg"></xsl:value-of>
</xsl:variable>
<xsl:variable name="var_res">
<xsl:value-of select='map:for-each(map(var_mapValue)}, function($k, $v){
{
if($v!="Yes" and $v!="No")
then $k else ""})' />
</xsl:variable>
map(var_mapValue) doesn't work not sure what is missing here
and my input xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<input>
<collective_barg>
{"abc": "yes","dDEF": "Yes","RMT": "dsfdf","PPAS": "Yes","UNITE":
"Yes"}
</collective_barg>
</input>
</root>
CodePudding user response:
To parse the JSON and turn it into what the XML Data Model defines as a "map", you should use the XPath 3.1 function parse-json()
https://www.w3.org/TR/xpath-functions-31/#func-parse-json
<xsl:variable name="map" select="parse-json(/root/input/collective_barg)"/>
Also NB string comparison in XSLT is case sensitive; the string value Yes
is not equal to yes
. I suggest you use the upper-case()
(or lower-case()
) function to normalize the text and then compare it to "YES"
(or "yes"
).
By the way, when you are declaring a variable, you can just assign its value using the select
attribute; you don't need to enclose an xsl:value-of
element. Using xsl:value-of
just has the effect of converting the data to a string. If you really do want to convert it to a string, you can use the string()
function. e.g. don't do this:
<xsl:variable name="var">
<xsl:value-of select="blah"/>
</xsl:variable>
do this:
<xsl:variable name="var" select="blah"/>
or if you do want to convert the input to a string, do this:
<xsl:variable name="var" select="string(blah)"/>
CodePudding user response:
var_mapValue is a variable.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:template match="/root">
<xsl:variable name="var_mapValue">
<xsl:value-of select="/input/collective_barg"/>
</xsl:variable>
<xsl:variable name="var_res">
<xsl:value-of select='map:for-each($var_mapValue, function($k, $v){
if($v!="Yes" and $v!="No")
then $k else ""})'/>
</xsl:variable>
<test>
<xsl:value-of select="$var_res"/>
</test>
</xsl:template>
</xsl:stylesheet>