473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Select XML Nodes by Parent attribute

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 3668
Plater
7,872 Expert 4TB
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 Expert 100+
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.GetAttribute will return 'true' or 'false' (or null if that attribute does not exist for the current element).
Mar 6 '09 #3
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 Expert 4TB
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="true"]/*

Which i *think* means "select all nodes that are children of any nodes with attribute ForceUpdate = true"
Mar 6 '09 #5
@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
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>...
8
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...
4
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...
2
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...
8
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...
16
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 ...
1
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...
1
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.