需要用可变值替换地图
我有XSLT及以下代码工作正常,但是我需要替换“ MAP {” ABC“:”是“,”,“ DDEF”:“是”,“ RMT”:“ DSFDF”,“ PPAS”:“是”,“,”,“” unite“:”是“是”}”,可变
<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>
如下:
<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)不确定这里缺少什么
,而我的输入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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要解析JSON并将其变成XML数据模型将其定义为“映射”,您应该使用XPATH 3.1函数
parse-json()
https://www.w3.org/tr/xpath-functions-functions-31/#func-parse-jsonXSLT中的NB字符串比较也很敏感;字符串值<代码>是不等于
是
。我建议您使用upper-case()
(或lower-case()
)函数将文本归一化,然后将其与“是” (或<代码>“是”
)。顺便说一句,当您声明变量时,您可以使用
select
属性分配其值;您无需包装XSL:value的
元素。使用XSL:
的value仅具有将数据转换为字符串的效果。如果您确实想将其转换为字符串,则可以使用String()
函数。例如不要这样做:这样做:
或者如果您想将输入转换为字符串,请执行此操作:
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-jsonAlso NB string comparison in XSLT is case sensitive; the string value
Yes
is not equal toyes
. I suggest you use theupper-case()
(orlower-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 anxsl:value-of
element. Usingxsl: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 thestring()
function. e.g. don't do this:do this:
or if you do want to convert the input to a string, do this:
使用Parsejson解决问题
mange to fix the issue by using ParseJson