tags:

views:

1020

answers:

2

I'm using VB.net (2003), and calling the SelectNodes method on an xml document. If I have a document:
<InqRs>
<DetRs>
<RefInfo>
<RefType>StopNum</RefType>
<RefId>0</RefId>
</RefInfo>
<RefInfo>
<RefType>Id</RefType>
<RefId>0</RefId>
</RefInfo>
</DetRs>
<DetRs>
<RefInfo>
<RefType>StopNum</RefType>
<RefId>0</RefId>
</RefInfo>
<RefInfo>
<RefType>Id</RefType>
<RefId>1</RefId>
</RefInfo>
</DetRs>
</InqRs>

How can I select just for the DetRs that has RefType=Id and RefId=0, ie, the 'first' one above.

I've tried several different attempts, among others:
InqRs/DetRs[RefInfo/RefType='Id' and RefInfo/RefId='0']
InqRs/DetRs[RefInfo/RefType='Id'][RefInfo/RefId='0']

But these select both of the DetRs sections (because of the StopNum RefId of 0, I presume).

Thanks!
Mark

+1  A: 

Like so. You don't need the top-level InqRs in your XPath expression, though it doesn't hurt. You may not care about the DetRs either, but assuming you do, you want to say "give me the parent of the Refinfo element which has the following specification"

DetRs/Refinfo[RefType='Id' and RefId='0']/..
Toby White
+1  A: 

You want all DetRs children of the top element:

    /*/DetRs

That have a RefInfo child:

    /*/DetRs
             [RefInfo]

That has RefType with value "Id":

    /*/DetRs
             [RefInfo
                   [RefType='Id']
              ]

and that has a RefId with value 0:

    /*/DetRs
             [RefInfo
                   [RefType='Id'
                 and
                   RefId=0
                   ]
              ]

And this XPath expression correctly selects just the wanted first DetRs element in the provided XML document.

Certainly, if someone has other stylistic preferences, the above expression could be written also as:

    /*/DetRs[RefInfo[RefType='Id' and RefId=0]]

Dimitre Novatchev