Xquery实例演示

Xquery实例演示
本文将重点演示两个Xquery实例,以此来说明Xquery的使用:

  1.FLOW的使用
 2.Xquery构造器

本文示例代码均在Eclipse  oXygen插件下通过测试!

1.xml源文件:
xml version="1.0" encoding="gb2312" ?>
xml-stylesheet type="text/xsl" href="users.xsl" ?>

< users  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation
="users.xsd" >
  
< user  id ="0" >
    
< usertype > admin usertype >
    
< username > tony username >
    
< password > 23456-adm-4578 password >
    
< userphone > 13241470658 userphone >
    
< email > [email protected] email >
    
< introduction > 管理员 introduction >
  
user >
  
< user  id ="1" >
    
< usertype > prof usertype >
    
< username > mark king username >
    
< password > 11111-sdf-3333 password >
    
< userphone > 13423456789 userphone >
    
< email > [email protected] email >
    
< introduction > 知名的XXXXXX introduction >
  
user >
  
< user  id ="2" >
    
    
< usertype > prof usertype >
    
< username > tom username >
    
< password > 22223-ffk-3333 password >
    
< userphone > 13501234567 userphone >
    
< email > [email protected] email >
    
< introduction > 国内著名的XXXXX introduction >
  
user >
< user  id ="3" >
    
< usertype > pro usertype >
    
< username > gate username >
    
< password > 33334-huj-3456 password >
    
< userphone > 13634567890 userphone >
    
< email > [email protected] email >
    
< introduction > 全球著名的XXXXX introduction >
  
user >
  ........(略...)
users >

实例1(XQ1) :for 与let的比较
< results >

{
    
 for $user in  doc("users.xml")/users/user
 return
   
< userInfo >
     { $user/username }
   
userInfo >

}
{
 let $userlet := doc("users.xml")/users/user
 return
     
< userInfomation >
     { $userlet/username }
    
userInfomation >
   }
   
   {
 let $userlet := doc("users.xml")/users/user
 return
     
< userInfomation >
      
< username >
     { $userlet/username/text()}
     
username >
    
userInfomation >
   }
results >

-----------------------------------------------------------------
results:

xml version="1.0" encoding="UTF-8" ?>
< results >
   

   
< userInfo >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > tony username >
   
userInfo >
   
< userInfo >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > mark king username >
   
userInfo >
   
< userInfo >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > tom username >
   
userInfo >
   
< userInfo >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > gate username >
   
userInfo >
 

   
< userInfomation >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > tony username >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > mark king username >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > tom username >
      
< username  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > gate username >
   
userInfomation >

   
< userInfomation >
      
< username > tonymark kingtomgate username >
   
userInfomation >
results >

实例2(XQ2):构造器演示
< results >
{

    
 for $user in  doc("users.xml")/users/user
 let $name:=$user/username,
     $introduction:=$user/introduction,
     $email:=$user/email
 where 
      $user/@id="3"
 return
  element user{
      element a{
      attribute href {"www.google.com/search?q=tony"},
      $name/text()
      },
      element introduction{
       $introduction/text()
      },
      element email{
      /users/user[@id="3"]/email/text() //即$email/text()
      },
      
< usertype >
        { /users/user[@id="3"]/usertype/text()}
      
usertype >
      
      
      }
      
 }

results >

///

results:

xml version="1.0" encoding="UTF-8" ?>
< results >
   
< user >
      
< href ="www.google.com/search?q=tony" > gate a >
      
< introduction > 全球著名的XXXXX introduction >
      
< email > [email protected] email >
   
user >
results >


总结:1.return子句以xml格式返回.
      2.两种构造方式可以混合使用。

你可能感兴趣的:(Xquery实例演示)