Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 303516 - Last Review: August 30, 2004 - Revision: 1.2
How To Use the starts-with() XPath Function
This article was previously published under Q303516
It is a common programming requirement when you load and parse an XML document using the Microsoft XML (MSXML) Document Object Model (DOM) to identify elements and/or elements with attributes whose values begin with a specific character or sequence of characters. This article includes a code sample that demonstrates how you can use the
starts-with XML Path
Language (XPath) string function to implement this requirement.
Step-by-Step Example
- In Notepad, create a new XML document named Books.xml, and paste the following XML:
<?xml version="1.0"?>
<!-- This file represents a fragment of a bookstore inventory database -->
<bookstore specialty="novel">
<book style="autobiography">
<author>
<first-name>Joe</first-name>
<last-name>Bob</last-name>
<award>Trenton Literary Review Honorable Mention</award>
</author>
<price>12</price>
</book>
<book style="textbook">
<author>
<first-name>Mary</first-name>
<last-name>Bob</last-name>
<publication>Selected Short Stories of
<first-name>Mary</first-name>
<last-name>Bob</last-name>
</publication>
</author>
<price>55</price>
</book>
</bookstore>
- Save Books.xml in the root folder of drive C.
- Open a new Standard EXE project in Microsoft Visual Basic. Form1 is created by default.
- From the Project menu, click References, and then select the Microsoft XML 3.0 check box.
- Drag a Command button, and drop it onto Form1.
- Copy and paste the following code in the Click event procedure of the Command button:
Dim doc As MSXML2.DOMDocument
Dim nlist As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
Set doc = New MSXML2.DOMDocument
doc.setProperty "SelectionLanguage", "XPath"
doc.Load "c:\books.xml"
Set nlist = doc.selectNodes("//book/author/first-name[starts-with(.,'M')]")
MsgBox "Matching Nodes : " & nlist.length
For Each node In nlist
Debug.Print node.nodeName & " : " & node.Text
Next
- The preceding code loads the XML from Books.xml into an instance of the MSXML DOMDocument object. It then executes an XPath query that uses the starts-with XPath function to identify all authors whose first names begin with the letter M. Finally, the For loop iterates through the selected nodes and displays the first names of the matching author elements.
- The first parameter of the starts-with XPath function is used to specify the source node or string against which the comparison is to be executed. The second parameter is the pattern string that specifies the character or character sequence that is to be used in the comparison. It is important to remember that the pattern string that is supplied as the second parameter of the starts-with function is case sensitive.
APPLIES TO
- Microsoft XML Parser 3.0
- Microsoft XML Parser 3.0 Service Pack 1
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate