【问题标题】:Xquery for nested payload xml用于嵌套有效负载 xml 的 Xquery
【发布时间】:2018-09-14 08:05:46
【问题描述】:

这是我的输入 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 分配给 &lt;BIKey&gt;

我已经按照 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

请协助解决这个问题,因为我不确定在哪里循环有效负载以获得所需的输出。

谢谢

【问题讨论】:

    标签: xquery


    【解决方案1】:

    只需使用两次string-join,一个调用嵌套到另一个调用中:

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

    这样,外部string-join 加入了由条形分隔的字符串序列,内部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

    【讨论】:

    • 符号“!”在我的情况下无效,因为我的 xquery 没有对其进行身份验证。是否有任何替代解决方案。
    • 运算符 ! 在 XQuery 3.0 及更高版本中定义,请参阅 w3.org/TR/xquery-30/#id-map-operator。您应该可以使用for $bo in Input/BusinessObjects/BusinessObject return string-join($bo/BusinessIdentifiers/BusinessIdentifier/BValue, '|') 而不是Input/BusinessObjects/BusinessObject ! string-join(BusinessIdentifiers/BusinessIdentifier/BValue, '|')
    • 因为我没有看到':',所以我可以看到一个空格''。
    • 注释中明确指出应该用for .. return表达式替换哪个部分,完整示例见xqueryfiddle.liberty-development.net/eiQZDbi/2,只有之前使用!运算符的内部表达式需要替换为for .. return.
    猜你喜欢
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2016-05-13
    相关资源
    最近更新 更多