473,625 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select XML Nodes by Parent attribute

12 New Member
Hi Guys,

I have an xml similar to this one:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <SystemUpdate ForceUpdate="false" >
  3.   <A ForceUpdate="false">
  4.     <Book name="Black" />
  5.     <Book name="BlackRed" />
  6.     <Book name="Red" />
  7.     <Book name="Green" ForceUpdate="true"/>
  8.   </A>
  9.   <B ForceUpdate="true">
  10.      <Book name="Old" />
  11.      <Book name="New"/>
  12.   </B>
  13. </CapUpdate>
  14.  
Can someone help me out and explain whats the simplest way of selecting nodes that have it's own or parent node's attribute: ForceUpdate set to true.

Ie.: SystemUpdate's ForceUpdate is set to true = that will select all Book nodes,
A node's ForceUpdate set to true = this will select Black,BlackRed, Red and Green Book nodes.
B node's ForceUpdate set to true = this will select "Old" and "New" Book nodes.
So in the quoted xml example selected nodes would be: Green,Old and New.

Hope I explained it correctly.
I was trying to find something that works similar but had no luck.

Thanks in advance.
Adrian
Mar 5 '09 #1
5 3680
Plater
7,872 Recognized Expert Expert
Well in the javascript world, you can use an XPATH statement to do that.
I think the XML objects in .NET have similar abilities?
Mar 5 '09 #2
vekipeki
229 Recognized Expert New Member
You can use XmlReader, to get values of your attributes:

Expand|Select|Wrap|Line Numbers
  1. XmlReader reader = new XmlTextReader(inputStream);
  2. reader.ReadToFollowing("A");
  3. string attributeValue = reader.GetAttribute("ForceUpdate");
XmlReader.GetAt tribute will return 'true' or 'false' (or null if that attribute does not exist for the current element).
Mar 6 '09 #3
AdrianGawrys
12 New Member
Thanks for your responses guys.

@vekipeki - I should have mentioned that I am able to select a node and test for attribute's value, but I am more interested in some sort of xpath statement that will get select all nodes that have a parent node with attribute ForceUpdate set to 'true' and store them into XmlNodeList variable.
@plater - would you be able to sketch statement in Javascript that does that?

Thanks,

Adrian
Mar 6 '09 #4
Plater
7,872 Recognized Expert Expert
Well I can never keep my head wrapped around XPath to be sure, but I really liked the examples found here:
http://www.zvon.org/xxl/XPathTutoria.../examples.html

Example #6 looks most like what you want for selecting by an attribute.
I think you can combine it with /*

I came up with:
//*[@ForceUpdate="t rue"]/*

Which i *think* means "select all nodes that are children of any nodes with attribute ForceUpdate = true"
Mar 6 '09 #5
AdrianGawrys
12 New Member
@Plater you have no idea how grateful I am for the link to examples you've sent.
I was never too good with XPath but that helped me to understand it.
Currently I've achieved what I wanted with two XPath queries - one for all book nodes that have 'ForceUpdate' attribute set to true and another query that selects all Book nodes whose Parent nodes have ForceUpdate set to true.

What I need to do now is just to combine them into one.

I will post my XPath query shortly.

Thanks again for your help!

Adrian
Mar 11 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1840
by: bearclaws | last post by:
I want to select certain nodes from an XML file whose parent nodes contain a particular attribute id. For instance, here's a sample XML file: <AAA> <BBB id="111">text <CCC>text</CCC> </BBB> <BBB id="222">
8
1528
by: Xamle Eng | last post by:
One of the things I find most unnatural about most XML APIs is that they try to abstract both elements and text into some kind of "node" object when they have virtually nothing in common. The reason these APIs do it is to make it possible for both text and elements to be children of elements. But there is another way. The XPath/XQuery data model does not allow two consecutive text nodes. As far as I can tell, most XML processing...
4
6111
by: Bart van Deenen | last post by:
Hi all I have a script where I dynamically create multiple inputs and selects from a script. The inputs and selects must have an associated onchange handler. I have the script working fine on Firefox, Safari and Konqueror, but the onchange event just doesn't fire on IE6. Firefox's javascript console shows no errors, and the IE script debugger shows nothing. onchange is not triggered.
2
10685
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
8
2632
by: Mikey | last post by:
I have an XML document as follows: <Menu> <Group> <Item Text="About Us" AccessRoles="All"> <Group> <Item Text="Option 1" AccessRoles="All" /> <Item Text="Option 2" AccessRoles="All" /> <Item Text="Option 3" AccessRoles="All" /> </Group>
16
13559
by: Brian D | last post by:
I have a multiple select list that is created dynamically based on a previous selection on an asp page. The first thing I do is to clear the curent option list by document.form1.itemcross.length = 0; The only problem is that it leaves the optgroups. How do I also get rid of the optgroups? Thanks
1
3272
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file exists just to sort of synchronize them by changing the xml nodes attribute(s). somehow i always catch an exception "blabla has an invalid token" but i cannot find a solution for this. maybe someone of you people can tell me how to do this...
1
2592
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file exists just to sort of synchronize them by changing the xml nodes attribute(s). somehow i always catch an exception "blabla has an invalid token" but i cannot find a solution for this. maybe someone of you people can tell me how to do this...
4
3579
by: jmDesktop | last post by:
I have searched everywhere and tried several things. I have a treeview with and want to be able to only select a parent node. For example: root //don't want to drag this -parent1 //yes, drag this an only this because it is a parent --childOfParent1 //cannot drag this, only the parent --childOfParent1 //can only drag parent -parent2 //yes, parent, can select and drag --childOfParent2 //no, cannot drag
0
8688
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
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7178
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...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4085
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
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
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.