简体   繁体   中英

Xquery for nested payload xml

Here is my input XML:

This is my modified xml data as input

 <Input>
    <BIKey></BIKey>
    <BusinessObjects>
          <BusinessObject>
            <BusinessIdentifiers>
              <BusinessIdentifier>
                <BKey>BuCode</BKey>
                <BValue>CDC</BValue>
              </BusinessIdentifier>
              <BusinessIdentifier>
                <BKey>BuType</BKey>
                <BValue>123</BValue>
              </BusinessIdentifier>
              <BusinessIdentifier>
                <BKey>CsmNo</BKey>
                <BValue>857895</BValue>
              </BusinessIdentifier>
            </BusinessIdentifiers>
            <BusinessAttributes>
              <BusinessAttribute>
                <BKey>Version</BKey>
                <BValue>1</BValue> 
              </BusinessAttribute>
              <BusinessAttribute>
                <BKey>date</BKey>
                <BValue>2018-06-28</BValue>
              </BusinessAttribute>
            </BusinessAttributes>
          </BusinessObject>
          <BusinessObject>
            <BusinessIdentifiers>
              <BusinessIdentifier>
                <BKey>BuCode</BKey>
                <BValue>CDC</BValue>
              </BusinessIdentifier>
              <BusinessIdentifier>
                <BKey>BuType</BKey>
                <BValue>123</BValue>
              </BusinessIdentifier>
              <BusinessIdentifier>
                <BKey>CsmNo</BKey>
                <BValue>34567</BValue>
              </BusinessIdentifier>
            </BusinessIdentifiers>
            <BusinessAttributes>
              <BusinessAttribute>
                <BKey>Version</BKey>
                <BValue>1</BValue> 
              </BusinessAttribute>
              <BusinessAttribute>
                <BKey>date</BKey>
                <BValue>2018-06-28</BValue>
              </BusinessAttribute>
            </BusinessAttributes>
          </BusinessObject>      
        </BusinessObjects>
        </Input>

I would like to get the following output CDC|123|857895:CDC|123|34567 assigned to <BIKey>

I have tried this Xquery as suggested by Martin which actually fixed my ealrier problem but my inputpayload is more as compare to my earlier question:

<Input>  
    For $BusinessObject in Input/BusinessObjects/BusinessObject[1]
    retrun


      <BIKey>{ string-join(Input/BusinessObjects/BusinessObject[1]/BusinessIdentifiers/BusinessIdentifier/BValue, '|') }</BIKey>


    </Input>

But I got this output

CDC|123|857895

Please assist on this as i am not sure where to loop the payload to get the required output.

Thanks

Simply use string-join twice, one call nested into the other:

<BIKey>
{
    string-join(
        Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|'),
        ':'
    )
}
</BIKey>

That way the outer string-join joins the sequences of bar separated strings the inner Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|') Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|') returns with a colon.

Example at https://xqueryfiddle.liberty-development.net/eiQZDbi .

If you don't have XQuery 3 or later you can replace the use of ! with

<Input>
    <BIKey>
    {
        string-join(
            for $bo in Input/BusinessObjects/BusinessObject return string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, '|'),
            ':'
        )
    }
    </BIKey>
</Input>

https://xqueryfiddle.liberty-development.net/eiQZDbi/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM