使用FLWOR XQuery返回多个XML节点和“自定义"父标记 [英] Return multiple XML nodes and Custom parent tag using FLWOR XQuery

查看:121
本文介绍了使用FLWOR XQuery返回多个XML节点和“自定义"父标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用FLWOR表达式获取以下输出.

I need to get the following output using a FLWOR expression.

<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所在的平面.

This doesn't gives me the expected output to find the planes where Year < 1970.

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

How to set a custom parent node as <oldPlanes>

如何返回2个节点作为预期输出?

推荐答案

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

You only want to create one oldPlanes element so its construction needs to be outside the FLWOR expression:

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

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

Except that you don't need a FLWOR for this at all, a simple path expression will do the job:

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

这篇关于使用FLWOR XQuery返回多个XML节点和“自定义"父标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆