如果 $arg 的值是一个空序列,则返回 True。否则,该函数返回 False。
语法
fn:empty($arg as item()*) as xs:boolean
参数
- $arg
 项序列。如果该序列为空,则此函数返回 True。否则,该函数返回 False。
备注
不支持 fn:exists() 函数。作为一种替代方法,可以使用 not() 函数。
示例
本主题提供了一些对 XML 实例的 XQuery 示例,这些实例存储在 AdventureWorks 数据库内不同的 xml 类型列中。有关这些列的概述,请参阅 AdventureWorks 数据库中的 xml 数据类型表示形式。
A. 使用 empty() XQuery 函数来确定属性是否存在
在产品型号 7 的生产过程中,此查询返回所有不具有 MachineHours 属性的生产车间。
SELECT ProductModelID, Instructions.query('
declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
     for $i in /AWMI:root/AWMI:Location[empty(@MachineHours)]
     return
       <Location
            LocationID="{ ($i/@LocationID) }"
            LaborHrs="{ ($i/@LaborHours) }" >
            { 
              $i/@MachineHours
            }  
       </Location>
') as Result
FROM Production.ProductModel
where ProductModelID=7
结果如下:
ProductModelID      Result        
-------------- ------------------------------------------
7              <Location LocationID="30" LaborHrs="1"/>
               <Location LocationID="50" LaborHrs="3"/>
               <Location LocationID="60" LaborHrs="4"/>
如果 MachineHour 属性不存在,则下面的查询(已稍做修改)返回“NotFound”:
SELECT ProductModelID, Instructions.query('
declare namespace p14="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
     for $i in /p14:root/p14:Location
     return
       <Location
            LocationID="{ ($i/@LocationID) }"
            LaborHrs="{ ($i/@LaborHours) }" >
            { 
                 if (empty($i/@MachineHours)) then
                    attribute MachineHours { "NotFound" }
                 else
                    attribute MachineHours { data($i/@MachineHours) }
            }  
       </Location>
') as Result
FROM Production.ProductModel
where ProductModelID=7
结果如下:
ProductModelID Result                       
-------------- -----------------------------------
7              
  <Location LocationID="10" LaborHrs="2.5" MachineHours="3"/>
  <Location LocationID="20" LaborHrs="1.75" MachineHours="2"/>
  <Location LocationID="30" LaborHrs="1" MachineHours="NotFound"/>
  <Location LocationID="45" LaborHrs="0.5" MachineHours="0.65"/>
  <Location LocationID="50" LaborHrs="3" MachineHours="NotFound"/>
  <Location LocationID="60" LaborHrs="4" MachineHours="NotFound"/>
请参阅
参考
针对 xml 数据类型的 XQuery 函数
exist() 方法(xml 数据类型)