需要用可变值替换地图

发布于 2025-02-11 20:09:52 字数 1235 浏览 6 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2025-02-18 20:09:52

要解析JSON并将其变成XML数据模型将其定义为“映射”,您应该使用XPATH 3.1函数parse-json() https://www.w3.org/tr/xpath-functions-functions-31/#func-parse-json

<xsl:variable name="map" select="parse-json(/root/input/collective_barg)"/>

XSLT中的NB字符串比较也很敏感;字符串值<代码>是不等于。我建议您使用upper-case()(或lower-case())函数将文本归一化,然后将其与“是” (或<代码>“是”)。

顺便说一句,当您声明变量时,您可以使用select属性分配其值;您无需包装XSL:value的元素。使用XSL:的value仅具有将数据转换为字符串的效果。如果您确实想将其转换为字符串,则可以使用String()函数。例如不要这样做:

<xsl:variable name="var">
        <xsl:value-of select="blah"/>         
        </xsl:variable>  

这样做:

<xsl:variable name="var" select="blah"/>  

或者如果您想将输入转换为字符串,请执行此操作:

<xsl:variable name="var" select="string(blah)"/>  

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)"/>  
ヤ经典坏疍 2025-02-18 20:09:52

使用Parsejson解决问题

    <xsl:variable name="var_mapValue" >
          <xsl:value-of select="Parse-json(/root/input/collective_barg)"> 
  </xsl:value-of>
        
    </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>  

mange to fix the issue by using ParseJson

    <xsl:variable name="var_mapValue" >
          <xsl:value-of select="Parse-json(/root/input/collective_barg)"> 
  </xsl:value-of>
        
    </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>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文