471,086 Members | 899 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

Trouble with XPath query

I have a query where i need to look for a value of a lower level xml
element based on the value of a parent element existing first. Here is
a simple example of the xml

<S3Client>
<Buttons>
<Button>Activity
<RestrictedClientType>
<ClientType>02</ClientType>
</RestrictedClientType>
</Button>
...
</Buttons>
</S3Client>

Here is my query, I am trying see if ClientType '02' exists when the
Button element has a value of "Activity". I wanted to do this in one
xpath query. Any help would be appreciated.
/Security/S3Client/Buttons[Button='Activity']/
RestrictedClientType[ClientType='02']

Tim

Mar 15 '07 #1
5 2316
* tschulken wrote in microsoft.public.dotnet.xml:
>I have a query where i need to look for a value of a lower level xml
element based on the value of a parent element existing first. Here is
a simple example of the xml

<S3Client>
<Buttons>
<Button>Activity
<RestrictedClientType>
<ClientType>02</ClientType>
</RestrictedClientType>
</Button>
...
</Buttons>
</S3Client>

Here is my query, I am trying see if ClientType '02' exists when the
Button element has a value of "Activity". I wanted to do this in one
xpath query. Any help would be appreciated.
/Security/S3Client/Buttons[Button='Activity']/
RestrictedClientType[ClientType='02']
For example:

//ClientType[ . = '02' and
ancestor::Button[ normalize-space() = 'Activity' ]

or

/S3Client/Buttons/Button[ normalize-space() = 'Activity' ]/
RestrictedClientType/ClientType[ . = '02' ]

Your expression is basically correct, but you missed the space
characters in the <Buttonelement.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Mar 15 '07 #2
On Mar 15, 5:21 pm, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
* tschulken wrote in microsoft.public.dotnet.xml:


I have a query where i need to look for a value of a lower level xml
element based on the value of a parent element existing first. Here is
a simple example of the xml
<S3Client>
<Buttons>
<Button>Activity
<RestrictedClientType>
<ClientType>02</ClientType>
</RestrictedClientType>
</Button>
...
</Buttons>
</S3Client>
Here is my query, I am trying see if ClientType '02' exists when the
Button element has a value of "Activity". I wanted to do this in one
xpath query. Any help would be appreciated.
/Security/S3Client/Buttons[Button='Activity']/
RestrictedClientType[ClientType='02']

For example:

//ClientType[ . = '02' and
ancestor::Button[ normalize-space() = 'Activity' ]

or

/S3Client/Buttons/Button[ normalize-space() = 'Activity' ]/
RestrictedClientType/ClientType[ . = '02' ]

Your expression is basically correct, but you missed the space
characters in the <Buttonelement.
--
Björn Höhrmann · mailto:bjo...@hoehrmann.de ·http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 ·http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 ·http://www.websitedev.de/- Hide quoted text -

- Show quoted text -
Bjorn,

That makes sense but it seems that I am having trouble getting it to
work. Since the <RestrictedClientTypewas added as a child node of
Button, seems that the first part of my query doesn't work. I guess I
am used to parent nodes just having no values or maybe just attributes
but seems that since Button has a value of "Activity" and then the
child node, does querying for the Activity Text change? Here is an
example, before <RestrictedClientTypewas added, this xpath query
would work fine:
_xmlDocument.SelectSingleNode( "/Security/S3Client/Buttons/
Button['Activity']" );

Now that query does not work, any ideas?

Thanks.

Tim

Mar 16 '07 #3
* tschulken wrote in microsoft.public.dotnet.xml:
><S3Client>
<Buttons>
<Button>Activity
<RestrictedClientType>
<ClientType>02</ClientType>
</RestrictedClientType>
</Button>
...
</Buttons>
</S3Client>
Here the content of the <Buttonelement is this:

1. the word "Activity" followed by
a line feed character followed by space characters
2. the RestrictedClientType element
3. a line feed character followed by space characters

I do not know whether the code above has been pretty printed for
readability, or whether it reflects what is actually in the tree
in memory, but assuming this is how it is in memory your original
.... Button='Activity' ... would not work as that would match if
the content was just 'Activity'.
>That makes sense but it seems that I am having trouble getting it to
work. Since the <RestrictedClientTypewas added as a child node of
Button, seems that the first part of my query doesn't work. I guess I
am used to parent nodes just having no values or maybe just attributes
but seems that since Button has a value of "Activity" and then the
child node, does querying for the Activity Text change? Here is an
example, before <RestrictedClientTypewas added, this xpath query
would work fine:
_xmlDocument.SelectSingleNode( "/Security/S3Client/Buttons/
Button['Activity']" );
I am not sure whether this is a typo, but this does not do what you
want at all. The string literal 'Activity' is converted to a boolean
(true) so this would select the first 'Button' element that has the
ancestors Buttons, S3Client, Security, in that order. You could also
write

/Security/S3Client/Buttons/Button['HelloWorld']

and the result would be the same.
>Now that query does not work, any ideas?
I do not see a reason why this would happen.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Mar 16 '07 #4
On Mar 15, 9:30 pm, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
* tschulken wrote in microsoft.public.dotnet.xml:
<S3Client>
<Buttons>
<Button>Activity
<RestrictedClientType>
<ClientType>02</ClientType>
</RestrictedClientType>
</Button>
...
</Buttons>
</S3Client>

Here the content of the <Buttonelement is this:

1. the word "Activity" followed by
a line feed character followed by space characters
2. the RestrictedClientType element
3. a line feed character followed by space characters

I do not know whether the code above has been pretty printed for
readability, or whether it reflects what is actually in the tree
in memory, but assuming this is how it is in memory your original
... Button='Activity' ... would not work as that would match if
the content was just 'Activity'.
That makes sense but it seems that I am having trouble getting it to
work. Since the <RestrictedClientTypewas added as a child node of
Button, seems that the first part of my query doesn't work. I guess I
am used to parent nodes just having no values or maybe just attributes
but seems that since Button has a value of "Activity" and then the
child node, does querying for the Activity Text change? Here is an
example, before <RestrictedClientTypewas added, this xpath query
would work fine:
_xmlDocument.SelectSingleNode( "/Security/S3Client/Buttons/
Button['Activity']" );

I am not sure whether this is a typo, but this does not do what you
want at all. The string literal 'Activity' is converted to a boolean
(true) so this would select the first 'Button' element that has the
ancestors Buttons, S3Client, Security, in that order. You could also
write

/Security/S3Client/Buttons/Button['HelloWorld']

and the result would be the same.
Now that query does not work, any ideas?

I do not see a reason why this would happen.
--
Björn Höhrmann · mailto:bjo...@hoehrmann.de ·http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 ·http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 ·http://www.websitedev.de/
Bjorn,

Yes, what i had was a typo and I apologize. The xpath query should be
"/Security/S3Client/Buttons[Button='Activity']". This used to work for
all Button lookups and now that I added the Child Node for the
"Activity" button, it doesn't work on that one (but still works for
others since they don't have children). Is there a way for a single
query to be used for both scenarios?

Tim

Mar 16 '07 #5
On Mar 16, 8:40 am, "tschulken" <tschul...@gmail.comwrote:
On Mar 15, 9:30 pm, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:


* tschulken wrote in microsoft.public.dotnet.xml:
><S3Client>
<Buttons>
<Button>Activity
<RestrictedClientType>
<ClientType>02</ClientType>
</RestrictedClientType>
</Button>
...
</Buttons>
></S3Client>
Here the content of the <Buttonelement is this:
1. the word "Activity" followed by
a line feed character followed by space characters
2. the RestrictedClientType element
3. a line feed character followed by space characters
I do not know whether the code above has been pretty printed for
readability, or whether it reflects what is actually in the tree
in memory, but assuming this is how it is in memory your original
... Button='Activity' ... would not work as that would match if
the content was just 'Activity'.
>That makes sense but it seems that I am having trouble getting it to
>work. Since the <RestrictedClientTypewas added as a child node of
>Button, seems that the first part of my query doesn't work. I guess I
>am used to parent nodes just having no values or maybe just attributes
>but seems that since Button has a value of "Activity" and then the
>child node, does querying for the Activity Text change? Here is an
>example, before <RestrictedClientTypewas added, this xpath query
>would work fine:
>_xmlDocument.SelectSingleNode( "/Security/S3Client/Buttons/
>Button['Activity']" );
I am not sure whether this is a typo, but this does not do what you
want at all. The string literal 'Activity' is converted to a boolean
(true) so this would select the first 'Button' element that has the
ancestors Buttons, S3Client, Security, in that order. You could also
write
/Security/S3Client/Buttons/Button['HelloWorld']
and the result would be the same.
>Now that query does not work, any ideas?
I do not see a reason why this would happen.
--
Björn Höhrmann · mailto:bjo...@hoehrmann.de ·http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 ·http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 ·http://www.websitedev.de/

Bjorn,

Yes, what i had was a typo and I apologize. The xpath query should be
"/Security/S3Client/Buttons[Button='Activity']". This used to work for
all Button lookups and now that I added the Child Node for the
"Activity" button, it doesn't work on that one (but still works for
others since they don't have children). Is there a way for a single
query to be used for both scenarios?

Tim- Hide quoted text -

- Show quoted text -
Seems that having the child nodes alters the text for the Button so in
the case of the 'Activity' Button, its text is Activity02. Can I just
specify that I don't want to consider the child nodes text at this
point?

Mar 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by 0wl | last post: by
8 posts views Thread by Terry P | last post: by
3 posts views Thread by gfrommer | last post: by
7 posts views Thread by Ot | last post: by
2 posts views Thread by mel_dev | last post: by
10 posts views Thread by Michael C# | last post: by
5 posts views Thread by Gnic | last post: by
6 posts views Thread by dotnetnoob | last post: by
3 posts views Thread by Jason Mobarak | last post: by

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.