By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,050 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,050 IT Pros & Developers. It's quick & easy.

How to build Dynamic Xpath expressions?

P: n/a
i would like to know how i can build xpath expression dynamiclly. let's say i
have a following xml file:

<EventEnrollment InstanceNumber = "675">
<EventSource>
<ObjectReference ObjectKey="xxxxx">
..
..
..
<EventEnrollment InstanceNumber = "676">
<EventSource>
<ObjectReference ObjectKey = "yyyyy">

and so on

i want to query the objectkey under specific EventEnrollment InstanceNumber
let's say 675, i can do that by hard coded in the InstanceNumber = 675 and
query out all it's objectkey but how can i do it dynamicly? how do i build a
dynamic xpath expressions. is it possible since xpath query are string.

thank you in advance.
Jun 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a


dotnetnoob wrote:
i want to query the objectkey under specific EventEnrollment InstanceNumber
let's say 675, i can do that by hard coded in the InstanceNumber = 675 and
query out all it's objectkey but how can i do it dynamicly? how do i build a
dynamic xpath expressions. is it possible since xpath query are string.


Sure it is possible, you usually pass in a .NET string to a method like
SelectNodes that expects an XPath expression so you can do all .NET
string manipulation to dynamically create that XPath.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 12 '06 #2

P: n/a
Can you give me an example?

thanks

jack

"Martin Honnen" wrote:


dotnetnoob wrote:
i want to query the objectkey under specific EventEnrollment InstanceNumber
let's say 675, i can do that by hard coded in the InstanceNumber = 675 and
query out all it's objectkey but how can i do it dynamicly? how do i build a
dynamic xpath expressions. is it possible since xpath query are string.


Sure it is possible, you usually pass in a .NET string to a method like
SelectNodes that expects an XPath expression so you can do all .NET
string manipulation to dynamically create that XPath.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 12 '06 #3

P: n/a
this is the xpath query that i use:
//EventEnrollment[@InstanceNumber='675']//EventSources/EventSource/ObjectReference/@ObjectKey

are you saying that to query out each EventEnrollment's InstanceNumber then
how do you place the value '675' to the prediate @InstanceNumber. i
understand that xpath query is a string but i don't understand how build that
string dynamically.

thank you
"Martin Honnen" wrote:


dotnetnoob wrote:
i want to query the objectkey under specific EventEnrollment InstanceNumber
let's say 675, i can do that by hard coded in the InstanceNumber = 675 and
query out all it's objectkey but how can i do it dynamicly? how do i build a
dynamic xpath expressions. is it possible since xpath query are string.


Sure it is possible, you usually pass in a .NET string to a method like
SelectNodes that expects an XPath expression so you can do all .NET
string manipulation to dynamically create that XPath.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 12 '06 #4

P: n/a


dotnetnoob wrote:
this is the xpath query that i use:
//EventEnrollment[@InstanceNumber='675']//EventSources/EventSource/ObjectReference/@ObjectKey

are you saying that to query out each EventEnrollment's InstanceNumber then
how do you place the value '675' to the prediate @InstanceNumber. i
understand that xpath query is a string but i don't understand how build that
string dynamically.


I assume that you have a variable e.g.
instanceNumber
with the number you are looking for and then you would simply build the
string using string concatenation e.g.
"//EventEnrollment[@InstanceNumber='" + instanceNumber +
"']//EventSources/EventSource/ObjectReference/@ObjectKey"

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 13 '06 #5

P: n/a
i still run into the problem of the xpath query out ever ObjectKey under the
EventSource and i have check my code and it did get the right instance number
to plug into the xpath expression. i use altova xmlspy to evaluate my xpath
expression and it show all nine EventSource under instance number 657

Can you or someone look over my code and tell me what i miss?

For a = 0 To i Step 1
Select Case cboEventEnrolls.SelectedIndex
Case a
list_EventEnrollInstNum =
doc.SelectNodes("//EventEnrollment/@InstanceNumber")

For Each nEventEnrollInstNum In list_EventEnrollInstNum

sInstNum = nEventEnrollInstNum.Value

ObjectKeyList =
doc.SelectNodes("/AutomationControl/EventEnrollments/EventEnrollment[@InstanceNumber='"
+ sInstNum +
"']/EventEnrollmentProperties/EventSources/EventSource/ObjectReference/@ObjectKey")

For Each ObjectKeyNode In ObjectKeyList
cboEventSources.Items.Add(ObjectKeyNode.InnerXml)
Next
Next
End Select
Next

and this is a fragment of the xml

<EventEnrollments>
<EventEnrollment InstanceNumber="675">
<BackwardReferences>
<Reference ObjectKey="800.7415_316.11_311.3702_311.1072_410.3 729"
PropertyName="TrendEnrollments"/>
<Reference ObjectKey="800.7415_316.11_311.3702_311.1083_410.3 741"
PropertyName="TrendEnrollments"/>
</BackwardReferences>
<EventEnrollmentProperties AckRequired="0" EventEnable="4" EventType="64"
NotifyClass="0" NotifyType="1025" PresentValue="0" ToFaultText=""
ToNormalText="" ToOffNormalText="">
<EnabledReference ObjectKey="" PropertyName=""/>
<EventParameters BitMask="0" Deadband="" HighDiffLimit="" HighLimit=""
ListOfBitStringValues="" ListOfValues="" LowDiffLimit="" LowLimit=""
ReferencedPropertyIncrement="1" TimeDelay="999" TimeInterval="9960">
<SetpointReference ObjectKey="" PropertyName=""/>
</EventParameters>
<EventSources>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference ObjectKey="800.7415_801.101_8.115_216.12_217.570"
PropertyName="ValuePointProperties.PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference ObjectKey="800.7415_801.101_8.115_216.12_217.572"
PropertyName="ValuePointProperties.PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference ObjectKey="800.7415_801.101_8.115_216.12_217.574"
PropertyName="ValuePointProperties.PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference ObjectKey="800.7415_801.101_8.115_216.12_217.576"
PropertyName="ValuePointProperties.PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference ObjectKey="800.7415_801.101_8.115_216.12_217.578"
PropertyName="ValuePointProperties.PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference ObjectKey="800.7415_801.101_8.115_216.12_217.580"
PropertyName="ValuePointProperties.PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference ObjectKey="800.7415_801.101_8.115_216.12_217.582"
PropertyName="ValuePointProperties.PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference
ObjectKey="800.7415_801.101_8.115_200.391_199.392_ 829.1"
PropertyName="PresentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackReference ObjectKey="" PropertyName=""/>
<ObjectReference
ObjectKey="800.7415_801.101_8.115_200.391_199.394_ 829.1"
PropertyName="PresentValue"/>
</EventSource>
</EventSources>
</EventEnrollmentProperties>
<GeneralProperties Attr="0" InstanceNumber="675" IsConfigured="0"
ObjectName="PlantHistory" ObjectType="9" SchemaVer="1.4" TraceOptions="0">
<Description/>
</GeneralProperties>
</EventEnrollment>

thank you
"Martin Honnen" wrote:


dotnetnoob wrote:
this is the xpath query that i use:
//EventEnrollment[@InstanceNumber='675']//EventSources/EventSource/ObjectReference/@ObjectKey

are you saying that to query out each EventEnrollment's InstanceNumber then
how do you place the value '675' to the prediate @InstanceNumber. i
understand that xpath query is a string but i don't understand how build that
string dynamically.


I assume that you have a variable e.g.
instanceNumber
with the number you are looking for and then you would simply build the
string using string concatenation e.g.
"//EventEnrollment[@InstanceNumber='" + instanceNumber +
"']//EventSources/EventSource/ObjectReference/@ObjectKey"

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 13 '06 #6

P: n/a
i got it.

Thank you for the help on building dynamic xpath query

"Martin Honnen" wrote:


dotnetnoob wrote:
this is the xpath query that i use:
//EventEnrollment[@InstanceNumber='675']//EventSources/EventSource/ObjectReference/@ObjectKey

are you saying that to query out each EventEnrollment's InstanceNumber then
how do you place the value '675' to the prediate @InstanceNumber. i
understand that xpath query is a string but i don't understand how build that
string dynamically.


I assume that you have a variable e.g.
instanceNumber
with the number you are looking for and then you would simply build the
string using string concatenation e.g.
"//EventEnrollment[@InstanceNumber='" + instanceNumber +
"']//EventSources/EventSource/ObjectReference/@ObjectKey"

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.