473,748 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to build Dynamic Xpath expressions?

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

<EventEnrollmen t InstanceNumber = "675">
<EventSource>
<ObjectReferenc e ObjectKey="xxxx x">
..
..
..
<EventEnrollmen t InstanceNumber = "676">
<EventSource>
<ObjectReferenc e 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
6 4936


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
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
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


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
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_EventEnrol lInstNum =
doc.SelectNodes ("//EventEnrollment/@InstanceNumber ")

For Each nEventEnrollIns tNum In list_EventEnrol lInstNum

sInstNum = nEventEnrollIns tNum.Value

ObjectKeyList =
doc.SelectNodes ("/AutomationContr ol/EventEnrollment s/EventEnrollment[@InstanceNumber ='"
+ sInstNum +
"']/EventEnrollment Properties/EventSources/EventSource/ObjectReference/@ObjectKey")

For Each ObjectKeyNode In ObjectKeyList
cboEventSources .Items.Add(Obje ctKeyNode.Inner Xml)
Next
Next
End Select
Next

and this is a fragment of the xml

<EventEnrollmen ts>
<EventEnrollmen t InstanceNumber= "675">
<BackwardRefere nces>
<Reference ObjectKey="800. 7415_316.11_311 .3702_311.1072_ 410.3729"
PropertyName="T rendEnrollments "/>
<Reference ObjectKey="800. 7415_316.11_311 .3702_311.1083_ 410.3741"
PropertyName="T rendEnrollments "/>
</BackwardReferen ces>
<EventEnrollmen tProperties AckRequired="0" EventEnable="4" EventType="64"
NotifyClass="0" NotifyType="102 5" PresentValue="0 " ToFaultText=""
ToNormalText="" ToOffNormalText ="">
<EnabledReferen ce ObjectKey="" PropertyName=""/>
<EventParameter s BitMask="0" Deadband="" HighDiffLimit=" " HighLimit=""
ListOfBitString Values="" ListOfValues="" LowDiffLimit="" LowLimit=""
ReferencedPrope rtyIncrement="1 " TimeDelay="999" TimeInterval="9 960">
<SetpointRefere nce ObjectKey="" PropertyName=""/>
</EventParameters >
<EventSources >
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e ObjectKey="800. 7415_801.101_8. 115_216.12_217. 570"
PropertyName="V aluePointProper ties.PresentVal ue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e ObjectKey="800. 7415_801.101_8. 115_216.12_217. 572"
PropertyName="V aluePointProper ties.PresentVal ue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e ObjectKey="800. 7415_801.101_8. 115_216.12_217. 574"
PropertyName="V aluePointProper ties.PresentVal ue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e ObjectKey="800. 7415_801.101_8. 115_216.12_217. 576"
PropertyName="V aluePointProper ties.PresentVal ue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e ObjectKey="800. 7415_801.101_8. 115_216.12_217. 578"
PropertyName="V aluePointProper ties.PresentVal ue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e ObjectKey="800. 7415_801.101_8. 115_216.12_217. 580"
PropertyName="V aluePointProper ties.PresentVal ue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e ObjectKey="800. 7415_801.101_8. 115_216.12_217. 582"
PropertyName="V aluePointProper ties.PresentVal ue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e
ObjectKey="800. 7415_801.101_8. 115_200.391_199 .392_829.1"
PropertyName="P resentValue"/>
</EventSource>
<EventSource EventState="0">
<FeedbackRefere nce ObjectKey="" PropertyName=""/>
<ObjectReferenc e
ObjectKey="800. 7415_801.101_8. 115_200.391_199 .394_829.1"
PropertyName="P resentValue"/>
</EventSource>
</EventSources>
</EventEnrollment Properties>
<GeneralPropert ies Attr="0" InstanceNumber= "675" IsConfigured="0 "
ObjectName="Pla ntHistory" ObjectType="9" SchemaVer="1.4" TraceOptions="0 ">
<Description/>
</GeneralProperti es>
</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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5827
by: Kevin | last post by:
I know this has probably been discussed many times before (I found answers when I searched yesterday), but I still can't get it to work... I have an attribute @OID that can contain any characters (including &quot; and &apos;) I have java code using Xerces to select a node based on it's @OID attribute using an XPath like this:
7
16659
by: Sebastian Petzelberger | last post by:
Hi group, please give me an example of a xpath with regex or better a link with examples. Thanks in advance, Sebastian
0
1100
by: Steve Jorgensen | last post by:
I'm wondering if there's an approach to writing consistent code to read/write XML data in arbitrary order that I'm simply missing. It seems to be easy getting stuff -out- of a DOM via XPath, but it's much tougher building a DOM document in arbitrary order. Yes - I can get the parent context element first, using XPath, but then I build custom wrappers and helpers to simplify the building and adding fragments in the correct namespace,...
0
1478
by: Thor W Hammer | last post by:
Is it possible to use dynamic xpath expressions with the <%#XPath("..")%> syntax and XmlDataSource? This is a couple of examples to illustrate what I think about: 1: <%#XPath("*")%> 2: <%#XPath("*%>]")%>
1
2232
by: Arndt Jonasson | last post by:
The definition of XPath 1.0 says "A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null (this is the same way attribute names are expanded). It is an error...
4
9131
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
4
2801
by: Weston | last post by:
Are there any quick, reliable shortcuts to determine if two arbitrary XPath expressions yield result sets that intersect? The obviously way would be to iterate over the set of nodes from one expression, checking each one to see if it's also contained in the other set, but I'm wondering if there might be ways of doing this by looking at or rewriting the actual XPath expressions themselves, without evaluating them.
1
3109
by: reyesvsn | last post by:
Hello everybody, I have a question concerning XPath expressions and namespaces. Consider this XML: <?xml version="1.0" encoding="UTF-8"?> <newsMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rtr="http://www.reuters.com/ns/2003/08/content" xmlns="http://iptc.org/std/nar/2006-10-01/"> <header>
4
9043
by: ducttape | last post by:
Hi, I have been trying for several days to read XML files into my program. I have been following several good tutorials on the internet, but I am struggling because the particular XML files that I am reading have several nested fields: <?xml version="1.0" encoding="UTF-8" ?> <xml> <records> <record> <database name="test.enl">test</database> <contributors> <authors> <author>
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.