【问题标题】:Assigning a variable with Dynamic data in Xquery在 Xquery 中使用动态数据分配变量
【发布时间】:2014-06-30 14:29:30
【问题描述】:

我有一个要求,我必须根据 constant.xml 中存在的数据验证传入数据。

下面说的是我的常量文件:

<Constant>
 <data>
  <Nation>India</Nation>
  <EndPointURL>customers/{$custID}/Resource</EndPointURL>
 </data>
 <data>
  <Nation>China</Nation>
  <EndPointURL>customers/{$custID}/Resource</EndPointURL>
 </data>
 <data>
  <Nation>Russia</Nation>
  <EndPointURL>customers/Resource</EndPointURL>
 </data>
</Constant>

和$body如下:

<body>
 <custID>1234</custID>
 <Country>India</Country>
 <ServiceURL>customers/1234/Resource</ServiceURL>
</body>

这里我必须检查一下,如果 $body/ServiceURL = $Constant/data/EndPointURL。

数据的基数是(1...无穷大)。

他们是我可以更改通过原始 CustID fr4om 输入并与客户/{$custID}/Resource 进行验证检查的一种方式。

目前,我正在使用以下代码进行检查。

let $ServiceURL :=$body/ServiceURL/text()
let $country :=$body/Country/text()
for $service  in ($Constant/data) where
        ($service/Nation/text() = $Country)
        and ($service/EndPointURL/text() = $ServiceURL) 

        return
            <ServiceURL>{fn:concat('/REALTime/',$service/EndPointURL/text())}</ServiceURL>
};

请告诉我,如何在 xquery 中更改 constant.xml 的数据

【问题讨论】:

    标签: xslt xquery osb


    【解决方案1】:

    以下查询向您展示如何扩展 EndPointURL 中的嵌入模板并仅在匹配时返回结果。

    (: find the `data` for our `Country` :)
    let $data := $Constant/data[Nation eq $body/Country]
    return
    
    (: expand the {$var} parts of the EndPointURL :)
    let $parts := tokenize($data/EndPointURL, "/")
    return
      let $expanded-parts :=
        for $part in $parts
        return
          if(matches($part, "\{\$[^}]+\}"))then
            let $src := replace($part, "\{\$([^}]+)\}", "$1")
            return
               $body/element()[local-name(.) eq $src]
          else
            $part
      return
        let $expanded-endpoint-url := concat("/", string-join($parts, "/"))
        return
    
           (: the body if the ServiceURL matches the expanded EndPointURL :)
           $body[ServiceURL eq $expanded-endpoint-url]
    

    【讨论】:

    • 嗨 Adamretter,你能解释一下代码的下面部分吗: if(matches($part, "\{\$[^}]+\}"))then let $src := replace($part, "\{\$([^}]+)\}", "$1") return $body/element()[local-name(.) eq $src] else $part return let $expanded -endpoint-url := concat("/", string-join($parts, "/")) return (: body 如果 ServiceURL 匹配扩展 EndPointURL :) $body[ServiceURL eq $expanded-endpoint-url] $1 在这里意味着什么,你也可以解释一下 if(matches($part, "\{\$[^}]+\}")) 条件。
    • For replace $1是正则表达式中使用的第一个匹配组,即正则表达式中()之间的位。
    • matches 表达式使用正则表达式来匹配任何以{$ 开头并以} 结尾的字符串。我用它来确定您的EndPointURL 的某些部分是否包含{$custID} 之类的内容。如果我找到这样的东西,我可以在比较常量数据和正文之前用实际值替换它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多