代码之家  ›  专栏  ›  技术社区  ›  Simran

xquery检查xquery如何检查布尔变量的值是否为真

  •  0
  • Simran  · 技术社区  · 6 年前

    我正在编写一个xquery,其中必须检查属性的值是否为true。属性/元素定义为布尔值。 我尝试了多个选项,但无法获得正确的值,该逻辑适用于其他元素,但不适用于“MainCustomer”,因为它是布尔定义的。我如何为它编写xquery?

    以下是我的请求示例:

    <Maintenance xmlns="*******">
    <AccountPersons>
      <APH AccountID="56987" LastFinancialRespSwitch="Y" LastMainCustomerSwitch="Y" LastPersonID="987569" QuickAddSwitch="false"/>
      <APR AccountID="98759" AccountRelationshipType="MIN" BillAddressSource="PER" PersonID="000000" MainCustomer="true"></APR>
      <APR AccountID="123456" AccountRelationshipType="MAIN" BillAddressSource="PERSON" PersonID="123456" MainCustomer="false"></APR>
    </AccountPersons>
    </Maintenance>
    

    if for loop.APR中的语句是一个数组。 我只想从MainCustomer=“true”中获取BillAddressSource的值 下面的xquery不起作用,它提供了所有APR的值。

     if (fn:boolean($MaintenanceResponse/ns1:AccountPersons/ns1:APR[$position]/@MainCustomer))
        then
           <acc:data>      
                   {if ($MaintenanceResponse/ns1:AccountPersons/ns1:APR[$position]/@BillAddressSource)
                    then <acc:addressSource>{fn:data($MaintenanceResponse/ns1:AccountPersons/ns1:APR[$position]/@BillAddressSource)}</acc:addressSource>
                    else ()
                }
           </acc:data> 
    

    我尝试的另一个xquery是,这给了我语法错误

            if ($MaintenanceResponse/ns1:AccountPersons/ns1:APR[$position]/@MainCustomer='true')
        then
           <acc:data>      
                   {if ($MaintenanceResponse/ns1:AccountPersons/ns1:APR[$position]/@BillAddressSource)
                    then <acc:addressSource>{fn:data($MaintenanceResponse/ns1:AccountPersons/ns1:APR[$position]/@BillAddressSource)}</acc:addressSource>
                    else ()
                }
           </acc:data>  
    

    请帮我找到正确的if语句。提前谢谢

    1 回复  |  直到 6 年前
        1
  •  2
  •   Michael Kay    6 年前

    首先,这有点取决于查询/处理器是否支持模式(是否有“导入架构”声明?)

    如果属性存在,则该属性的有效布尔值为true,而不管其值如何,也不管数据是否经过架构验证。二者都 if (@married) ... if (boolean(@married)) ...

    如果要测试该属性是否存在并具有值“true”或“1”,请使用强制转换: if (xs:boolean(@married)) ... . 无论数据是否被类型化(通过模式验证),这都将起作用。请注意 boolean() 函数(有时编写) fn:boolean() xs:boolean() cast或构造函数,执行数据转换。

    如果数据是非类型化的,则可以使用 if (@married = 'true') ,但如果数据类型为布尔值,则此操作将失败并出现类型错误。此外,它不会测试所有合法的布尔值(“true”、“1”、“1”等)。

    • if (data(@married)) ... 但是没有真正的理由喜欢这个。

    • if (@married = true()) -表情 @married = true()

    • if (@married eq true()) -表情 @married eq true() () (空序列)如果属性不存在;回归效应 if() 与返回false相同。使用“eq”而不是“=”会给性能带来微小的好处。

    @married = false() @married eq false() @married != true() not(@married = true()) 这更微妙。如果 @married 不存在,则使用 = , eq != , ne true() false() . 所以 不是(@marred=true()) 不等于 (@married != true()) (@married = false()) .

    关于这件事我可以写上几页。在我的书(XSLT2.0和XPath2.0程序员参考)中,我就是这么做的(参见第581至592页)。