본문 바로가기

프로그램&DB/유용한팁

XML / XPath 쿼리 및 예제 Query Examples By Robbe Morris

XML / XPath Query Examples

By Robbe Morris

Here are a few little XPath snippets of how to query for a specific node or node list in an XML document looking for specific attribute values.

Ads by Google
1.   Password Manager -  Web-based Password Management Software. Free Trial Download -  ManageEngine.com
2.   Download Registry Fixer -  Powerful Registry cleaner others can't touch. Turbocharge your PC! -  iolo.com
3.   MS SQL database tool -  MS SQL Server Database Admin Tools Download Now! Windows, Linux, OSX -  aquafold.com
4.   Dynamics Ax Technology -  Enterprise Portal, Sharepoint, AIF Xml, .Net, Web, Integration -  larsmikkelsen.com
5.   ASP .NET Mapping Engine -  Address lookups, reverse geocoding Int'l support, scalable. Free eval -  geobase.info
6.   Graphics Component -  Use GoDiagram to add graphics and diagrams to your .NET applications -  nwoods.com
7.   Entity Framework Tools -  Model designer and code generator for Entity Framework -  devart.com
8.   SQL Database Query Tool -  Script, Edit, Execute, Explore Free. SQL Server, Sybase, DB2 -  sqldbx.com
9.   User Interface Design -  Create software UI prototypes fast. No coding. 30-day Free Trial. -  CarettaSoftware.com
10.   ASP.NET Calendar Control -  Popup calendar & date/time picker Works for all version of ASP.NET -  essentialobjects.com

The object model shown here is for the COM xml parser.  If you are using .NET's XmlDocument class instead of MSXML2.DOMDocument.4.0, the XPATH query syntax is the same.  In fact, much of the object model is the same.

"xml" string equals the following: 

<?xml version='1.0'?> 
  <EXAMPLE> 
      <CUSTOMER id="1" type="B">Mr.  Jones</CUSTOMER> 
      <CUSTOMER id="2" type="C">Mr.  Johnson</CUSTOMER> 
  </EXAMPLE> 

<% 

  Dim doc 
  Dim node 
  Dim nodeList 

   Set doc =  Server.CreateObject("MSXML2.DOMDocument.4.0"

          doc.LoadXML(xml) 

  Set node = doc.selectSingleNode("//EXAMPLE/CUSTOMER[@id='2' or @type='C']"

         ' node.Text will show "Mr Johnson"

  Set node = doc.selectSingleNode("//EXAMPLE/CUSTOMER[@id='1' and @type='B']"

         ' node.Text will show "Mr Jones"
         ' node.Attributes.getNamedItem("id").Text will show "1"
         ' node.Attributes.getNamedItem("type").Text will show "B"
       
  ' Select a node list where id equals 1 and the type equals B or C.  
  ' In our example, only Mr.  Jones will be returned.

   Set nodeList = doc.selectNodes("//EXAMPLE/CUSTOMER[@id='1' and (@type='B' or @type='C')]"

     ' node.Text will show "Mr Jones"

  ' Select a node with a specific text value

   Set node = doc.selectSingleNode("//EXAMPLE/CUSTOMER[.  ='Mr.  Johnson']"

         ' node.Text will show "Mr.  Johnson"

  ' Select node list of all nodes with CUSTOMER not equal to "EggHeadCafe"

   nodeList.selectNodes("//EXAMPLE/CUSTOMER[.  !='EggHeadCafe']")       

' Start of MSXML4 or higher features.   If you want to use MSXML3, then
'  you'll need to set the following:  

    doc.setProperty "SelectionLanguage""XPath" 

' Search for a substring in an attribute

Set node = doc.selectSingleNode("//EXAMPLE/CUSTOMER[substring(@type,1,2) ='DE']")       

' Search for a substring in a node value

Set node = doc.selectSingleNode("//EXAMPLE/CUSTOMER[substring(.,1,3) ='Mr.']")     

' Search for a value contained in an attribute

Set node = doc.selectSingleNode("//EXAMPLE/CUSTOMER[contains(@type,'DECEA')]")       

' Search for a contained in a node value

Set node =  doc.selectSingleNode("//EXAMPLE/CUSTOMER[contains(.,'Smith')]")     

' Search only from root of selected node.   This examples gets a list of nodes
' from a specific node not the whole XML document.   This XML document
' is too small to be a reasonable example but you get the idea.  
' The ".//" tells the parser to make the current node the "root" node of the
' document for this specific XPath query.   Without the ".", the XPath query
' would return all CUSTOMER nodes in the entire document even though
' they may be above or below the current node.

  Set node = doc.selectSingleNode("//EXAMPLE"

  Set treeList = node.selectNodes(".//CUSTOMER")         

         nTot = treeList.length - 1 
                 
         For nCnt = 0 to nTot 
                           
                 Set node = treeList.nextNode() 
                 msgbox  node.Attributes.getNamedItem("id").Text   
       
         Next 



'  End of MSXML4 or higher features    
         

Set node = nothing 
Set doc = nothing 

%> 

Popularity  (22827 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP