XPathNavigator.Matches doesn't work as I expect

Hi,
I cannot guess, why some XPath expressions in my code returns false and some 
even errors, while the others returne true as I expected. I'am workin with VS 
2008 Express Edition.

Dim xEl As XElement = <root><child1><child2/></child1></root>
Dim nav As XPathNavigator = xEl.CreateNavigator

nav.MoveToRoot()

Console.WriteLine(nav.Matches("*")) 'returns FALSE
Console.WriteLine(nav.Matches("root")) 'returns FALSE
Console.WriteLine(nav.Matches("/root")) 'returns FALSE

Console.WriteLine("--------------------------")

nav.MoveToFirstChild()

Console.WriteLine(nav.Matches("*")) 'OK, returns TRUE
Console.WriteLine(nav.Matches("child1")) 'OK, returns TRUE
Console.WriteLine(nav.Matches("child1[parent::root]")) 'OK, returns TRUE
Console.WriteLine(nav.Matches("//root/child1")) 'returns FALSE

Console.WriteLine(nav.Matches("*[self::child1]")) 'OK, returns TRUE
'Console.WriteLine(nav.Matches("self::child1")) 'raise error
'Console.WriteLine(nav.Matches("*/self::child1")) 'raise error

Console.ReadLine()
0
karuzo (4)
9/5/2008 12:54:00 PM
dotnet.xml 7266 articles. 0 followers. Follow

4 Replies
828 Views

Similar Articles

[PageSpeed] 50

"karuzo" <karuzo@discussions.microsoft.com> wrote in message 
news:24F9B3F5-154D-4D74-A7C3-4CBD42230FA5@microsoft.com...
> Hi,
> I cannot guess, why some XPath expressions in my code returns false and 
> some
> even errors, while the others returne true as I expected. I'am workin with 
> VS
> 2008 Express Edition.
>
> Dim xEl As XElement = <root><child1><child2/></child1></root>
> Dim nav As XPathNavigator = xEl.CreateNavigator
>
> nav.MoveToRoot()
>
> Console.WriteLine(nav.Matches("*")) 'returns FALSE
> Console.WriteLine(nav.Matches("root")) 'returns FALSE
> Console.WriteLine(nav.Matches("/root")) 'returns FALSE

Your problem is that you're trying to use an XElement (and not a 
full-fledged XDocument) for XPath, which screws up the XPath Document Model.

Consider: in XPath, an XML document has a root (aka document) _node_ ("/"), 
and a root _element_ ("/*"). When you do MoveToRoot on XPathNavigator, you 
navigate to the document node, and you'll need to MoveToFirstChild to 
position it on the root element. When you deal with an XmlDocument or 
XDocument, the document itself is that nameless root document node. However, 
when you deal with an XML fragment, there's no document, so the topmost 
node - that is, your "root" - becomes the document node for XDM purposes; 
except that it doesn't really qualify as such, because the document node is 
not supposed to be an element - it has its own distinct type. This seems to 
confuse XPath provider here, so "root" ends up matching neither an element 
(probably because an element should always have a parent node), nor "/" 
(probably because "/" is not supposed to be an element node).

If you rewrite your example and make xEl an XDocument, you'll see that it 
works as expected (though you'll need to MoveToFirstChild immediately after 
MoveToRoot).


0
int19h (6)
9/5/2008 1:20:50 PM
"Pavel Minaev" <int19h@gmail.com> wrote in message 
news:O$s24o1DJHA.3904@TK2MSFTNGP02.phx.gbl...
>
> "karuzo" <karuzo@discussions.microsoft.com> wrote in message 
> news:24F9B3F5-154D-4D74-A7C3-4CBD42230FA5@microsoft.com...
>> Hi,
>> I cannot guess, why some XPath expressions in my code returns false and 
>> some
>> even errors, while the others returne true as I expected. I'am workin 
>> with VS
>> 2008 Express Edition.
>>
>> Dim xEl As XElement = <root><child1><child2/></child1></root>
>> Dim nav As XPathNavigator = xEl.CreateNavigator
>>
>> nav.MoveToRoot()
>>
>> Console.WriteLine(nav.Matches("*")) 'returns FALSE
>> Console.WriteLine(nav.Matches("root")) 'returns FALSE
>> Console.WriteLine(nav.Matches("/root")) 'returns FALSE
>
> Your problem is that you're trying to use an XElement (and not a 
> full-fledged XDocument) for XPath, which screws up the XPath Document 
> Model.
>
> Consider: in XPath, an XML document has a root (aka document) _node_ 
> ("/"), and a root _element_ ("/*"). When you do MoveToRoot on 
> XPathNavigator, you navigate to the document node, and you'll need to 
> MoveToFirstChild to position it on the root element. When you deal with an 
> XmlDocument or XDocument, the document itself is that nameless root 
> document node. However, when you deal with an XML fragment, there's no 
> document, so the topmost node - that is, your "root" - becomes the 
> document node for XDM purposes; except that it doesn't really qualify as 
> such, because the document node is not supposed to be an element - it has 
> its own distinct type. This seems to confuse XPath provider here, so 
> "root" ends up matching neither an element (probably because an element 
> should always have a parent node), nor "/" (probably because "/" is not 
> supposed to be an element node).
>
> If you rewrite your example and make xEl an XDocument, you'll see that it 
> works as expected (though you'll need to MoveToFirstChild immediately 
> after MoveToRoot).
>

If this is true its very unsatisfactory.  XPath does not define that the 
root node should be the Document.  Indeed in many cases its important that 
it isn't.  E.g applying XSL to a child node in a document should have that 
child node treated as the root as far as any XPath present in the XSL is 
concerned.

-- 
Anthony Jones - MVP ASP/ASP.NET

0
9/5/2008 1:38:47 PM
Thank you, Pavel,
it solves my problem. I still don't understand the errors in the last two 
(commented) line of code but I doesn't matter much.
Regards, Martin

"Pavel Minaev" wrote:

> 
> "karuzo" <karuzo@discussions.microsoft.com> wrote in message 
> news:24F9B3F5-154D-4D74-A7C3-4CBD42230FA5@microsoft.com...
> > Hi,
> > I cannot guess, why some XPath expressions in my code returns false and 
> > some
> > even errors, while the others returne true as I expected. I'am workin with 
> > VS
> > 2008 Express Edition.
> >
> > Dim xEl As XElement = <root><child1><child2/></child1></root>
> > Dim nav As XPathNavigator = xEl.CreateNavigator
> >
> > nav.MoveToRoot()
> >
> > Console.WriteLine(nav.Matches("*")) 'returns FALSE
> > Console.WriteLine(nav.Matches("root")) 'returns FALSE
> > Console.WriteLine(nav.Matches("/root")) 'returns FALSE
> 
> Your problem is that you're trying to use an XElement (and not a 
> full-fledged XDocument) for XPath, which screws up the XPath Document Model.
> 
> Consider: in XPath, an XML document has a root (aka document) _node_ ("/"), 
> and a root _element_ ("/*"). When you do MoveToRoot on XPathNavigator, you 
> navigate to the document node, and you'll need to MoveToFirstChild to 
> position it on the root element. When you deal with an XmlDocument or 
> XDocument, the document itself is that nameless root document node. However, 
> when you deal with an XML fragment, there's no document, so the topmost 
> node - that is, your "root" - becomes the document node for XDM purposes; 
> except that it doesn't really qualify as such, because the document node is 
> not supposed to be an element - it has its own distinct type. This seems to 
> confuse XPath provider here, so "root" ends up matching neither an element 
> (probably because an element should always have a parent node), nor "/" 
> (probably because "/" is not supposed to be an element node).
> 
> If you rewrite your example and make xEl an XDocument, you'll see that it 
> works as expected (though you'll need to MoveToFirstChild immediately after 
> MoveToRoot).
> 
> 
> 
0
karuzo (4)
9/5/2008 1:50:04 PM
"Anthony Jones" <AnthonyWJones@yadayadayada.com> wrote in message 
news:e4Dm6y1DJHA.4132@TK2MSFTNGP03.phx.gbl...

> If this is true its very unsatisfactory.  XPath does not define that the 
> root node should be the Document.

Well, XPath 2.0 does explicitly call that type of node a "document node" 
(this of course doesn't mean that it must necessarily be a ***Document class 
in .NET). XPath 1.0 calls it a "root node" without further clarification, 
but it does specify that it is distinct from element nodes:

http://www.w3.org/TR/xpath#data-model

    The tree contains nodes. There are seven types of node:

    - root nodes
    - element nodes
    - text nodes
    - attribute nodes
    - namespace nodes
    - processing instruction nodes
    - comment nodes

    ...

    Every node other than the root node has exactly one parent, which is 
either an element node or the root node.

    ...

    The root node is the root of the tree. A root node does not occur except 
as the root of the tree. The element node for the document element is a 
child of the root node. The root node also has as children processing 
instruction and comment nodes for processing instructions and comments that 
occur in the prolog and after the end of the document element.

    ...

    The root node does not have an expanded-name.

Note that this final requirement clearly marks root node as distinct from 
element node. An element node _must_ have a non-empty expanded name (because 
you can't have a nameless element in XML).

>  Indeed in many cases its important that it isn't.  E.g applying XSL to a 
> child node in a document should have that child node treated as the root 
> as far as any XPath present in the XSL is concerned.

The XSLT spec explicitly says that there is always a single root (i.e., 
non-element node) in the source (input) tree, even though it allows that to 
have more than one-child (i.e., represent an XML fragment rather than 
document), "when the source tree is created in some other way, for example 
by using the DOM".


0
int19h (6)
9/6/2008 7:59:18 AM
Reply:

Similar Artilces:

Working with dates
I've spent far too long on a seemingly simple problem, calculating date windows. I have a cell that calculates a date based on another date. This is a template with no data entered yet. So for instance A1 is a column where a date will be entered, A2 adds 15 to that date and A3 adds 30 to that date providing a "window." Being that this is a template and no data has been entered, when the formulas are written in to A2 and A3 the value is 15-Jan-1900 and 30-Jan-1900. Is there anyway to leave these fields Blank until a value is entered in A1? -- matt330 ---------------------...

XPathNavigation and Namespaces.
I have the following code that is attempting to read nodes from an xml file... XmlDocument xpdStatus = new XmlDocument(); xpdStatus.Load("status.xml"); XPathNavigator xpnStatus = xpdStatus.CreateNavigator(); XPathNodeIterator xpniStatus = xpnStatus.Select("descendant::Status"); while (xpniStatus.MoveNext()) { Console.WriteLine("Book title: {0}", xpniStatus.Current.Value); XmlNode node = ((xpniStatus.Current) as IHasXmlNode).GetNode(); Console.WriteLine("Status: {0}", node.Value); } The code works fine until I attempt to add namespaces, with prefixes...

balances don't match
Hi anyone have an idea how to resolve this: I have an investment account set up with an associated cash account...which is baqsically used as a checking account. It is setup with online update via Schwab. The actual balance of the account matches the balance on the "cash transactions" page of the account register. But my "net worth" report and the home page show the balance as significantly lower (thousands of dollars) and when my balance is low, it even shows negative... all the while the register still shows the accurate balance. any clues ? In microsoft.public....

Can VISIO 2007 work on multiple pages of an org chart simultaneous
I created an org chart with over 100 pages using the Org Chart Wizard and Excel, I’m assigning colors to various shapes using the Data Graphic feature. To apply the colors I choose “Edit” then “select all” which selects all the shapes on the current sheet, then I select “Apply To Selected Shapes” from the Data Graphics pane, this works fine for the current sheet but is there a way to apply this to all the sheets at once. Thanks Mark, I've written a macro for you to try in my blog... See Applying Selected DataGraphic to the Whole Document - http://bvisual.spaces.live.com/...

Match words
I want to fill a cell with color if it contains a word that contai letters that I specify. For example words then end with letter "s". Wildcard search doesn't seem to work? What is the formula to use -- KH_G ----------------------------------------------------------------------- KH_GS's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=3292 View this thread: http://www.excelforum.com/showthread.php?threadid=52741 use conditional formatting and formula is =RIGHT(A1)="s" click the format button and pattern and select the colour -- Reg...

matching only once
If you could help me tweak this. I am using this formula: =If(COUNTIF(C$1:C$7,A1),"",A1) and I want it to put in Col B the amounts from col a that do not have a match in col c. My problem is it matches both of the $25.00 amounts from col a when there was only one $25.00 amount in column c. I would want it to show 1 $25.00 in col b as not being a match. A B C $ 50.00 $ 25.00 $ 3.00 $ 75.00 $ 467.00 $ 467.00 $ 95.00 $ 30.00 $ 30.00 $ 3.00 $ 25.00 $ 50.00 $ 25.00 $ 90.00 ...

Calculated fields on a form simply don't work
I am trying to create a calculated control on a form as below =amount * 2 all this gives is #name? error the field exists and is either number or currency data type. Even using the full table and field name syntax returns the same I even created the code using the expression builder but the result is always the same Every website I have looked at, as well a couple of books say that this should work. Why can't I get the most simple calculated control to work. This is really starting to make me hate msaccess. Please help Stahlhelm wrote: > I am trying to create a calculated contr...

Any plans for the desktop search to work with CRM data?
Our prospect yesterday asked "Any plans for the desktop search to work with CRM data?" Yes, take a look at this post: http://blogs.msdn.com/joris_kalz/archive/2005/08/21/454243.aspx HTH, -- Jeffry van de Vuurst CWR Mobility www.cwrmobility.com -- "Morris" <Morris@discussions.microsoft.com> wrote in message news:2935B60B-E2FC-430D-9C89-F2C6EE0A3DBE@microsoft.com... > Our prospect yesterday asked "Any plans for the desktop search to work > with > CRM data?" > > Hi Jeffery, Terrific answer! Thanks for the info. we're one step ...

Screenupdate = False not working
Hi, I'm experiencing in some code I wrote that the statement Appliation.Screenupdate = False doesn't seem to work. Despite the line of code, the screen still changes when I select or activate another worksheet in the workbook in my code. Any ideas? I'm still on Excel 97, not by choice =) Cade Application.ScreenUpdating = False HTH Paul -------------------------------------------------------------------------------------------------------------- Be advised to back up your WorkBook before attempting to make changes. ------------------------------------------------------------...

Work week holiday for different countries
Is it possible to refer to a holiday list somewhere in my windows installation when I work with the function =WORKWEEK? If not, can I collect the holiday lists for different countries from Microsoft webpage somwhere? ...

Formula Won't Work
Hi all, Aim: to select from a drop down box one's salary range and then spit out the marginal tax rate. The rates are as follows: Australian 2006/2007 Income Tax Rates $0 - $6,000 Nil $6,001 - $25,000 15c for each $1 over $6,000 $25,001 - $75,000 $2,850 plus 30c for each $1 over $25,000 $75,001 - $150,000 $17,850 plus 40c for each $1 over $75,000 Over $150,000 $47,850 plus 45c for each $1 over $150,000 Cell B183 is where the user selects their salary range from a drop down box. The formula I have used (which doesn't work) is given below: =IF(...

Match invoice by receipt instead of by PO
When going in the PO Match Invoice entry, the vendor usually send one invoice by reveiving, and GP match the invoice with the PO quantities received over all the receiving not matched. When customers are having many items by PO, they need to manually change every quantities to match it to one receiving entry at the time. It would be really usefull to present items by receiving entries instead of the PO overall quantities in the Auto-invoice entry. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this s...

=QuarterNum(): How to make it work in Excel 2003
I recently switched from Excel 97 to Excel 2003 Today I tried to use Excel 2003 to enter data into a worksheet I originally created with Word 97. One of the columns in the original worksheet contains the formula =QuarterNum(cell reference) Under Excel 97 this formula would display the quarter number for a date entered into the referenced cell. But under Excel 2003 it displays ####. When I place the cursor over the cell a tan colored box pops up displaying #NAME? And when I click on the cell a white box containing an exclmation point inside a diamond shape pops up to the left of the cell...

WLM beta
For some reason my WLM Beta fails to indent text in newsgroup messages with arrows when I reply to them. I've checked the Options dialog, and the settings are correct: Options | Send | News Sending Format | Plain Text Settings | Indent the original text (etc). Is anyone else having this problem? SteveT Steve It is a known bug at this point -- Peter Please Reply to Newsgroup for the benefit of others Requests for assistance by email can not and will not be acknowledged. http://www.microsoft.com/protect "Steve Thackery" <nobody@nowhere.com>...

funny working ListBox or a bug in VS 6.0 ?
Hi! I have a problem with setting the character set in my ListBox (List). When I create a CFont object with a specific character set and I set that font in the Device Context recieved from the ListBox, then it is OK. I can write through that DC in the chosen charset. See Code : CFont * font = new CFont; CFont *oldFont; CPaintDC dc( List ); font->CreateFont (28,0,0,0,700,0,0,0, RUSSIAN_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial"); oldFont = dc.SelectObject( font); dc.ExtTextOut(10,170,0, NULL,"some text", str...

Deployment over Citrix not working in Access
pls see my post from yesterday below. Am I in the wrong group to get an answer on this? Thanks, Rob I have an application that I have developed with extensive support from this group. I just want to say thank you for all your help. Now, my app is deployed over a Citrix network and I keep having these weird things hapen: 1) on trying to add a record to a particular form (fsubDevices) I get the following msg: Error 3201 (you cannot add or change a record because a related record is required in table 'tblVisit.') in procedure cboDeviceLUno_AfterUpdate of VBA document Form_fsu...

Best Match Query
I need to create a query or queries, that return the number of matches on corresponding fields in two tables. Table 1 is Applicants, Table 2 is Mentors. Fields A1, A2, A3..., M1, M2, M3... are populated with a/b/c/d type values. I think it should be fairly simple to find the number of interests in common between Applicant1 and Mentor1, Applicant1 and Mentor2, etc. but I can't get started. Can someone point me in the right direction? The ultimate output is to identify the applicant/mentor pairs with the most in common, but that might be obvious if we can accomplish the above. TI...

Inventory HATB and ATB don't match
I'm working on an inventory reporting project and while testing my solution, I've found that the Inventory Historical Aged Trial Balance and the Aged Trial Balance don't match when both are run up to the minute. There's not enough activity going on for it to be timing and they've been out by the same amount all day. It's not much, $946, but I can't figure out why. The report I'm creating balances to the Aged Trial Balance perfectly. I'm running an inventory reconcile again now but I'm wondering if anyone else has had this issue? Otherwise, tomorrow, so...

Junk mail filters dont work in conjuction with rules
I have recently upgraded to outlook 2003 and I have found that if I use rules, the junk mail filters dont work. I have 2 basic rules setup that move mail to specific folders depending on which account they come from. When the rules are enabled, the junk filters dont work regardless of the protection level. I have the feeling that the rules are also pulling the junk mail out of the junk folder and sorting them as when I do a large mail download, you can see mails flashing in an out of the junk mail folder. Is there any way to make the junk mail filter overide all other filters? Amit ...

Working with time
I have set up a worksheet calculating employees time worked. As the data inputted is shift type e.g. e, l, n. I have setup a formula to work out each shift by its time length e.g. e = 7.5 hrs etc. As my employees do not all work set convenient hours some work overtime. How can I set a formula for excel to report back if someone works 9hrs 22.5 minutes that they have either worked more or less than their contracted hours in a time vale and not as a part of 100? ...

XPathNavigator
When I try this //WebSRFTemplate/Data/Field[Name='SERVICEStaticRoute2IPnetwork1']/text() I get back the values of ALL Fields in my XML below. I only want the value (Test1) of the field name specified. Am I doing something wrong here? <WebSRFTemplate Version="1" Type="SERVICE"> <Data> <Field Name="SERVICEStaticRoute2IPnetwork1">"Test1"</Field> <Field Name="SERVICEStaticRoute3Subnetmask">"Test2"</Field> <Field Name="SERVICEStaticRoute3IPnetwork1">"Test3"<...

locate array index with a -match or regex.
basically im trying to find out what line a -match is on. i know i could do it with a for loop and check each line, but with everything that PS can do i was wondering if there was some little trick for it. Thanks Justin U2VsZWN0LVN0cmluZyByZXR1cm5zIDxNYXRjaEluZm8+IG9iamVjdHMsIHRoZXNlIGhhdmUgYSBM aW5lTnVtYmVyIHByb3BlcnR5IHRoYXQgaXMgMS1iYXNlZC4gUG93ZXJTaGVsbCBhcnJheXMsIGxp a2UgLk5ldCdzIGFycmF5cywgYXJlIDAtYmFzZWQuIEFsbCB5b3UnZCBoYXZlIHRvIGRvIGlzIHN1 YnRyYWN0IDEgZnJvbSBNYXRjaEluZm8uTGluZU51bWVyIHRvIGhhdmUgdGhlIGluZGV4IGZvciBh IDAtYmFzZWQgYXJyYXk6DQoNCiRhcnJheSA9IDEsIDIsIDMsIDQs...

Account Transactions GoTo not working for history data?
We had a weird situation today (and of course, it was with an auditor looking over our shoulder!).... 1. User creates an Account Transactions SmartList of data from a closed year. 2. User double-clicks on a transaction and is taken to the Transaction Entry Zoom window, which shows the detail behind the selected journal entry. 3. User highlights the line of the JE with the selected data then clicks on the 'Source Document' link on the Transaction Entry Zoom window. It opens the Receivings Transaction Inquiry Zoom window for the receipt associated with a random line of the journa...

Can someone help; pipe delimited text import not working
Im trying to import a pipe delimited text file into access 2007 and I get the following message when I try to finish the import "Method Execute TempImexSpec' of object '_Wizhook'failed. It doen't seem to be recognizing the "|" pipe sysmbol I entered? Help nothing seems to be working for me. Thanks Char Char - Can you import the text manually (which is how you usually set up import specs anyway)? It could be a data issue or an import spec issue... Can you copy the import text file and remove all but maybe the first few records and see if o...

Matching data #2
I want to match data from two lists A and B. And match the closest descriptions between the lists. The spelling of the descriptions in the lists in many cases will be different. An example of the problem List A Computer centre compaq dell microsoft List B cmputeR center cmpaq del 1Micrsft ...