Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath not working as expected... 2

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
Ok, using XML similar to this...
Code:
<Test>
  <opt>
    <name>Test1</name>
    <Min>1</Min>
    <Max>2</Max>
    [b]<type>A</type>[/b]
  </opt>
  <opt>
    <name>Test2</name>
    <Min>1</Min>
    <Max>2</Max>
    [b]<type>B</type>[/b]
  </opt>
  <opt>
    <name>Test3</name>
    <Min>3</Min>
    <Max>5</Max>
    [b]<type>A</type>
    <type>B</type>[/b]
  </opt>
</Test>

Note that the 1st and 2nd <Opt> tags have the same range (Min & Max) but different type values...
Then the 3rd <Opt> tag has a different range, and 2 type values...

I was trying to select a single node by using this query...
"//opt[type = '" & Type & "'][Min <= " & Size & "][Max >= " & Size & "]"

(above code is used with VB, XML DOM, SelectSingleNode function)

If the Size = 1 or 2 and Type = A or B, such as:
"//opt[type = 'A'][Min <= 1][Max >= 1]"
"//opt[type = 'B'][Min <= 1][Max >= 1]"
It works fine...

The problem is due to the 2 <type> tags in the 3rd node...

If the Size = 3 to 5 and Type = A, such as:
"//opt[type = 'A'][Min <= 3][Max >= 3]"
It works fine, because it finds the A first, and returns true...

If the Size = 3 to 5 and Type = B, such as:
"//opt[type = 'B'][Min <= 3][Max >= 3]"
It Does Not work, because (again) it finds the A first, returns false, then terminates before finding the B in the second tag...

Does anyone know a way to force it to search all tags, instead of short ciruiting???

Thanks in Advance ;-)
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
OK... Managed to come up with a work around, though I don't care for it much...

Code:
<Test>
  <opt>
    <name>Test1</name>
    <Min>1</Min>
    <Max>2</Max>
    [b]<typeA />[/b]
  </opt>
  <opt>
    <name>Test2</name>
    <Min>1</Min>
    <Max>2</Max>
    [b]<typeB />[/b]
  </opt>
  <opt>
    <name>Test3</name>
    <Min>3</Min>
    <Max>5</Max>
    [b]<typeA />
    <typeB />[/b]
  </opt>
</Test>

Then for the Query:
"//opt[type" & Type & "][Min <= " & Size & "][Max >= " & Size & "]"

This will work for now...

But a star is still available for anyone who figures out how to search multiple identical sub tags with different values as described above...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
This is interesting. Xpath works fine with XSL, but not with VB, XML DOM and SelectSingleNode.

Two work arounds:

1. If you only have finite types:
Code:
xml.SelectNodes("/Test/opt[type = 'B' or type[1] = 'B'][Min <= 3][Max >= 3]")
2. Infinite types:
Code:
For Each X In xml.SelectNodes("/Test/opt/type[. = 'B']")
  if TypeName(X.parentNode.SelectSingleNode(".[Min <= 3][Max >= 3]"))<>"Nothing" then
    set node = X.parentNode.SelectSingleNode(".[Min <= 3][Max >= 3]")
  end if
Next

Its annoying that it doesnt work the original way though.
 
Well, you managed to spark my imagination, so take a star ;-)

here is what I came up with:
xDom.selectSingleNode("//opt[Min <= " & nsize & "][Max >= " & nsize & "]/type[. = '" & ntype & "']").parentNode


Such as:
Code:
Private Sub Command2_Click()
  Dim nsize As Integer
  Dim ntype As String
  nsize = 3
  ntype = "B"
  Text1 = xDom.selectSingleNode("//opt[Min <= " & nsize & "][Max >= " & nsize & "]/type[. = '" & ntype & "']").parentNode.xml
End Sub


Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thanks, nice solution. Any idea why the normal xpath functions (such as contains()) dont work with vbscript XML DOM?

Cheers,

Jon
 
Just answered that in another thread. I think its WD-XSL and you need to:

Code:
domDoc.setProperty("SelectionLanguage", "XPath");

or somesuch thing.


 
Cool, I'll give it a try...

What is WD-XSL???

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Not sure if you caught it in the original post, but I'm using VB... Anyway... Same difference...

So... I tried:
Dim xDom As DomDocument
...
xDom.setProperty "SelectionLanguage", "XPath"


And it returned an error of method not found...?
(which I figured it would, since AutoComplete did not find it)

Though, I am using MSXML V2, if that matters... is this in a later version?...

I will continue to try with later versions... (I think I have up to V5.0 on this PC...)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
...Update...

Yes, setProperty is included in v5.0
As well as: v2.6, v3.0, & v4.0

Though in all 5 of them, none of them needed the property set...

Just v2.0 (msxml.dll) that did not work, but could not set the property... ?!?

Wierd...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
OK... Maybe it did need to be set in all of them... But it looks as though it sets a system variable or something... Because now even v2.0 works fine...

REALLY ODD...

Thanks for the info... Take a star ;-)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top