xslt / xquery用于在if条件下检查两个属性的值

时间:2019-03-05 22:12:38

标签: for-loop if-statement xslt-1.0 xquery

我被卡在xquery中的某个位置。在响应中,我想取回每个类型的电话号码,即HOME和CELL,但只返回序列号最高的电话号码。如果有两行的PhoneType =“ HOME”,我要取回序列号最高的电话的两个,即在我的情况下为Sequence =“ 3”。

使用我的xquery,我可以取回phoneType =“ HOME”和第一行,其类型为HOME。我也无法添加条件来检查序列。我在哪里以及如何添加它。请提出建议。预先感谢

我的Xquery的一部分:

 <acc:phones>
                 {
                for $PersonPhonesRow in $PersonMaintenanceResponse/ns2:CMPersonService/ns2:CMPersonDetails/ns2:PersonPhones/ns2:PersonPhonesRow[@PhoneType="HOME"][1]
                    return
                    if(fn:data($PersonMaintenanceResponse/ns2:CMPersonService/ns2:CMPersonDetails/ns2:PersonPhones/ns2:PersonPhonesRow/@PhoneType="HOME"[1]))
                    then
                    <com:phone>
                            <com:type>{'HOME'}</com:type>
                            <com:phoneNumber>{fn:data($PersonMaintenanceResponse/ns2:CMPersonService/ns2:CMPersonDetails/ns2:PersonPhones/ns2:PersonPhonesRow[@PhoneType="HOME"][1]/@PhoneNumber)}</com:phoneNumber>
                            <com:carrier>{fn:data($PersonMaintenanceResponse/ns2:CMPersonService/ns2:CMPersonDetails/ns2:PersonPhones/ns2:PersonPhonesRow[@PhoneType="HOME"][1]/@Extension)}</com:carrier>
                    </com:phone>
                    else
                   ()
                  }
                </acc:phones>

请求:

<CMPerson xmlns="http://splwg.com/CMPerson.xsd">
     <CMPersonService>    
        <CMPersonDetails>
           <PersonPhones>
              <PersonPhonesHeader PersonID="1234567890" LastSequenceNumber="9"/>
              <PersonPhonesRow PersonID="1234567890" Sequence="1" PhoneType="HOME" IntlPrefix="" PhoneNumber="(850) 123-0000" Extension="" Version="12" PhoneAlgorithmParamValue="(999) 999-9999"/>
              <PersonPhonesRow PersonID="1234567890" Sequence="2" PhoneType="CELL" IntlPrefix="" PhoneNumber="(850) 000-0000" Extension="" Version="3" PhoneAlgorithmParamValue="(999) 999-9999"/>
              <PersonPhonesRow PersonID="1234567890" Sequence="3" PhoneType="HOME" IntlPrefix="" PhoneNumber="(850) 123-1111" Extension="ATT" Version="1" PhoneAlgorithmParamValue="(999) 999-9999"/>
              <PersonPhonesRow PersonID="1234567890" Sequence="4" PhoneType="BUSN" IntlPrefix="" PhoneNumber="(904) 111-1111" Extension="" Version="3" PhoneAlgorithmParamValue="(999) 999-9999"/>              
           </PersonPhones>
        </CMPersonDetails>
     </CMPersonService>
  </CMPerson>

需要回复:

<acc:phones>
      <com:phone xmlns:com="******">
        <com:type>HOME</com:type>
        <com:phoneNumber>(850) 123-1111</com:phoneNumber>
        <com:carrier>ATT</com:carrier>
      </com:phone>
       <com:phone xmlns:com="******">
        <com:type>CELL</com:type>
        <com:phoneNumber>(904) 111-1111</com:phoneNumber>
        <com:carrier></com:carrier>
      </com:phone>
    </acc:phones>

1 个答案:

答案 0 :(得分:0)

要使用的查询:

        let $PersonMaintenanceResponse := 'Your request'
        let $uniqPhoneType    :=distinct-values($PersonMaintenanceResponse/ns2:CMPersonService/ns2:CMPersonDetails/ns2:PersonPhones/ns2:PersonPhonesRow/@PhoneType)
        for $each at $i in $uniqPhoneType
        return 
        <acc:phones>
        {
        let $seq    := $PersonMaintenanceResponse/ns2:CMPersonService/ns2:CMPersonDetails/ns2:PersonPhones/ns2:PersonPhonesRow[(@PhoneType = $each)]
        let $maxValue := max($seq/@Sequence)
        let $maxrow := $seq[@Sequence eq $maxValue]
        return 
        <com:phone xmlns:com="*******">
        <com:type>{$each}</com:type>
        <com:phoneNumber>{$maxrow/@PhoneNumber}</com:phoneNumber>
        <com:carrier>{$maxrow/@Extension}</com:carrier>
        </com:phone>
        }</acc:phones>