Return 多个 XML 节点和使用 FLWOR XQuery 的自定义父标签

Return multiple XML nodes and Custom parent tag using FLWOR XQuery

我需要使用 FLWOR 表达式获得以下输出。

<oldPlanes>
  <make>Cessna</make>
  <model>Centurian</model>
  <make>Piper</make>
  <model>Tripacer</model>
</oldPlanes>

使用

CREATE TABLE XMLO1 (xDoc XML NOT NULL)

INSERT INTO XMLO1 VALUES ('
 <planes>
   <plane>
     <year>1977</year>
     <make>Cessna</make>
     <model>Skyhawk</model>
     <color>Light blue and white</color>
   </plane>
   <plane>
     <year>1975</year>
     <make>Piper</make>
     <model>Apache</model>
     <color>White</color>
   </plane>
   <plane>
     <year>1960</year>
     <make>Cessna</make>
     <model>Senturian</model>
     <color>Yellow and White</color>
   </plane>
   <plane>
     <year>1956</year>
     <make>Piper</make>
     <model>ripacer</model>
     <color>Blue</color>
   </plane>
 </planes>')

我尝试了以下查询

SELECT xDoc.query('for $p in //plane
                    let $x:=$p/year
                    where $x < 1970
                    return <oldPlanes><make>{data($p/make)}</make> 
                           </oldPlanes>
                    ')
FROM XMLO1

这没有给我找到 Year < 1970.

所在平面的预期输出

如何设置自定义父节点为<oldPlanes>

如何return2个节点作为预期输出?

您只想创建一个 oldPlanes 元素,因此其构造需要在 FLWOR 表达式之外:

<oldPlanes>{for $p in //plane
            let $x:=$p/year
            where $x < 1970
            return $p/(make, model)}</oldPlanes>

除了您根本不需要 FLWOR 之外,一个简单的路径表达式就可以完成这项工作:

<oldPlanes>{//plane[year < 1970]/(make, model)}</oldPlanes>