XPATH

Xpath 匹配节点的内容为空(inner text 为空)

分类: XML .net 695人阅读 评论(0) 收藏 举报

需求是从以下写出能从下面的xml中取出Title内容(inner text)为空的节点的XPath: 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <BookCatalog>  
  3. <Books>  
  4. <Book>  
  5. <Title>Asp.net</Title>  
  6. <Price>22.5</Price>  
  7. <Author>Abraham</Author>  
  8. </Book>  
  9. <Book>  
  10. <Title></Title>  
  11. <Price>22.5</Price>  
  12. <Author>Abraham</Author>  
  13. </Book>  
  14. </Books>  
  15. </BookCatalog>  
<?xml version="1.0" encoding="utf-8" ?>
<BookCatalog>
<Books>
<Book>
<Title>Asp.net</Title>
<Price>22.5</Price>
<Author>Abraham</Author>
</Book>
<Book>
<Title></Title>
<Price>22.5</Price>
<Author>Abraham</Author>
</Book>
</Books>
</BookCatalog>
容易写出错误的xPath有:
  1. //Book[Title[text()='']]   
  2.   
  3. //Book[Title[text() is null]]   
  4.   
  5. //Book[Title[node() is null]]   
  6.   
  7. //Book[Title[. is null]]   
  8.   
  9. //Book[Title[text()=]]   
//Book[Title[text()='']] 

//Book[Title[text() is null]] 

//Book[Title[node() is null]] 

//Book[Title[. is null]] 

//Book[Title[text()=]] 
正确的写法为:
  1. //Book[Title[not(text())]] 或    
  2.   
  3. //Book[Title[not(node())]]  
//Book[Title[not(text())]] 或  

//Book[Title[not(node())]]

验证程序为:

[csharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Xml;  
  6. namespace TestXPath  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             string[] errorXPaths = { "//Book[Title[text()='']]",   
  13.                 "//Book[Title[text() is null]]",   
  14.                 "//Book[Title[node() is null]]",   
  15.                 "//Book[Title[. is null]]",  
  16.                 "//Book[Title[text()=]]"};  
  17.             string[] correctXPaths = { "//Book[Title[not(text())]]""//Book[Title[not(node())]]" };  
  18.             string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> " + @"  
  19.                         <BookCatalog>  
  20.                         <Books>  
  21.                         <Book>  
  22.                         <Title>Asp.net</Title>  
  23.                         <Price>22.5</Price>  
  24.                         <Author>Abraham</Author>  
  25.                         </Book>  
  26.                         <Book>  
  27.                         <Title></Title>  
  28.                         <Price>22.5</Price>  
  29.                         <Author>Abraham</Author>  
  30.                         </Book>  
  31.                         </Books>  
  32.                         </BookCatalog>";  
  33.             XmlDocument xmlDocument = new XmlDocument();  
  34.             xmlDocument.LoadXml(xml);  
  35.             foreach (string errorxPath in errorXPaths)  
  36.             {  
  37.                 try  
  38.                 {  
  39.                     XmlNodeList nodeList = xmlDocument.SelectNodes(errorxPath);  
  40.                     Console.WriteLine("errorxPath:" + errorxPath);  
  41.                     Console.WriteLine("nodeList.Count:" + nodeList.Count);  
  42.                 }  
  43.                 catch (Exception ex)  
  44.                 {  
  45.                     Console.WriteLine(ex.Message);  
  46.                 }   
  47.             }  
  48.   
  49.             foreach (string correctXPath in correctXPaths)  
  50.             {  
  51.                 XmlNodeList nodeList = xmlDocument.SelectNodes(correctXPath);  
  52.                 Console.WriteLine("correctXPath:" + correctXPath);  
  53.                 Console.WriteLine("nodeList.Count:" + nodeList.Count);  
  54.             }  
  55.   
  56.             Console.Read();  
  57.         }  
  58.     }  
  59. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值