簡體   English   中英

嵌套有效負載XML的Xquery

[英]Xquery for nested payload xml

這是我的輸入XML:

這是我修改后的xml數據作為輸入

 <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>

我想將以下輸出CDC|123|857895:CDC|123|34567分配給<BIKey>

我已經按照Martin的建議嘗試了這個Xquery,它實際上解決了我的查詢器問題,但是與我之前的問題相比,我的inputpayload更多:

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


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


    </Input>

但是我得到了這個輸出

CDC|123|857895

請協助執行此操作,因為我不確定在哪里循環有效負載以獲取所需的輸出。

謝謝

只需使用string-join兩次,一個調用嵌套到另一個調用中:

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

這樣,外部string-join將由小節分隔的字符串序列連接到內部Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|') Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|')返回冒號。

https://xqueryfiddle.liberty-development.net/eiQZDbi上的示例。

如果您沒有XQuery 3或更高版本,則可以替換使用!

<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM