我在使用XQuery生成字符串组时遇到问题。每个连接都会添加一个接一个的空白区域。
例如,我的XML文件就像这样
<createRequest>
<transactionType>GAME1</transactionType>
<listOfBoard>
<board>
<gameType>RegularA</gameType>
<listOfBoardData>
<boardData>6</boardData>
<boardData>12</boardData>
</listOfBoardData>
</board>
<board>
<gameType>RegularB</gameType>
<listOfBoardData>
<boardData>7</boardData>
<boardData>8</boardData>
<boardData>11</boardData>
<boardData>21</boardData>
<boardData>28</boardData>
<boardData>47</boardData>
</listOfBoardData>
</board>
</listOfBoard>
<ticketPrice>600</ticketPrice>
</createRequest>
我的代码
let $requestMessage := //createRequest
let $boardData :=
for $eachOfListBoardData in 1 to count($requestMessage/listOfBoard/board/listOfBoardData)
return
fn:concat(fn:string-join($requestMessage/listOfBoard/board[$eachOfListBoardData]/listOfBoardData/boardData, ","),":")
return $boardData
但结果是在每个组之后输出额外的空格,如下所示
6,12: 7,8,11,21,28,47: 4,11,21,26,28,31: 7,28,31,45,46,49: 15,29,34,36,41,42: 5,7,14,21,42,46:
并且在数字'46'之后添加':'
我想要的输出是6,12:7,8,11,21,28,47:4,11,21,26,28,31:7,28,31,45,46,49:15,29,34,36,41,42:5,7,14,21,42,46
任何想法?
答案 0 :(得分:1)
&#34; ...但结果是在每个组之后输出额外的空格...&#34;
string-join()
传递空字符串作为第二个参数可以帮助您解决这个问题。
&#34;它正在添加&#39;:&#39;在数字&#39; 46&#39;&#34;
之后
只有当前项目不是最后一项时,您才可以使用if-else
构造来附加:
,请参阅下面的完整XQuery示例:
let $requestMessage := //createRequest
let $lastBoard := ($requestMessage/listOfBoard/board/listOfBoardData)[last()]
let $boards :=
for $board in $requestMessage/listOfBoard/board/listOfBoardData
return
if($board is $lastBoard)
then fn:string-join($board/boardData, ",")
else fn:concat(fn:string-join($board/boardData, ","),":")
let $boardData := fn:string-join($boards, "")
return $boardData