473,386 Members | 1,873 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.

Selecting nodes with exclusions

I need help selecting nodes while excluding some of them. Here's an
example of XML:

<my_xml>
<all_items>
<item>
<key>1</key>
<name>Item 1</name>
</item>
<item>
<key>2</key>
<name>Item 2</name>
</item>
<item>
<key>3</key>
<name>Item 3</name>
</item>
</all_items>
<excluded_items>
<key>1</key>
<key>3</key>
</excluded_items>
</my_xml>

I want to select the nodes from "all_items" that don't have a
coresponding value in "excluded_items". Is that possible to do? Or do
I need to do it in multiple steps by select all of the nodes and then
filtering through the ones that are excluded? Any help would be
appreciated.

Aug 8 '06 #1
7 1299
What do you mean by "select"? Javascript, XPath/XSLT, DOM traversal, other?
Aug 8 '06 #2
I want to select it into a variable in XSLT.

Aug 8 '06 #3
Pa************@hotmail.com wrote:
I want to select it into a variable in XSLT.
OK, then you want to find
All the <itemelements in <all_items>
Such that
The value of the <item>'s <keychild
Is not the same
As any of the <key>s in <excluded_items>

One way to write that XPath would be:

/my_xml/all_items/item[key != ../excluded_items/key]

This takes advantage of the fact that comparing two node sets reports
true if any value in the first set matches a value from the second set.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Aug 9 '06 #4
Joe Kesselman wrote:
One way to write that XPath would be:

/my_xml/all_items/item[key != ../excluded_items/key]

This takes advantage of the fact that comparing two node sets reports
true if any value in the first set matches a value from the second set.
Doesn't that only compare each "/my_xml/all_items[key]" to the first
"key" node of "exluded_items"?

In my example, only key=1 would be filtered out because it would only
compare to the first item in excluded_items, which is key=1.

Aug 9 '06 #5
Pa************@hotmail.com wrote:
Doesn't that only compare each "/my_xml/all_items[key]" to the first
"key" node of "excluded_items"?
Nope. Both the ./key and ../excluded_items/key expressions return
nodesets, and as the spec says:

"If both objects to be compared are node-sets, then the comparison will
be true if and only if there is a node in the first node-set and a node
in the second node-set such that the result of performing the comparison
on the string-values of the two nodes is true."

If we were explicitly taking string-value(../excluded_items/key), then
yes, that would obtain the value only of the first such node. But that's
due to the definition of string-value on a nodeset, not of comparison of
nodesets.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Aug 9 '06 #6
Maybe I'm doing something wrong, but it's not working for me. Here's
my stylesheet for this test case. Nothing is being returned for my
variable FilteredItems.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="FilteredItems"
select="/my_xml/all_items/item[key!=../excluded_items/key]"/>
<xsl:copy-of select="$FilteredItems"/>
</xsl:template>
</xsl:stylesheet>

Aug 9 '06 #7
Sorry; I was describing the right idea but had two details wrong. Try:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<doc>
<xsl:variable name="FilteredItems"
select="/my_xml/all_items/item[not(key=../../excluded_items/key)]"/>
<xsl:copy-of select="$FilteredItems"/>
</doc>
</xsl:template>
</xsl:stylesheet>
(I'd left out one ../, and in this case not(=) is not the same thing as
!=. (The equality test is effectively an "or".)

Sigh. That's what I get for not tossing it though the interpreter for a
test before posting.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Aug 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kj | last post by:
Suppose I have some XML document that contains tags of the form <... xmlns:foo="http://www.bar.org/foo"> <... xmlns:foo="baz"> <... xmlns:frobozz="http://www.bar.org/foo"> What's the...
2
by: Mike Kamermans | last post by:
I'm having some trouble using text() in an xsl:value-of xpath. I have the following xml: .... <graphemes> <grapheme>1</grapheme> <grapheme>2</grapheme> <grapheme>3</grapheme> </graphemes>...
2
by: Dag | last post by:
Hi I am pretty much an xml beginner; hopefully someone can easily answer this one... I want to select a node representing a worksheet in an xml document for the Office Web Components spreadsheet....
2
by: Saurabh Sharma | last post by:
Hi, I am using Dom Parsing in Xml. I am the parent node and it has many children and each children has many children. I want to select children with a given name . Is there any method by which we...
4
by: JoKur | last post by:
I'm currently using a context menu to allow the user to add, rename and delete nodes in a treeview control. When a node is added we give it a bogus name ("NewNode") and invoke the add method. We...
1
by: Stefan | last post by:
Hey guys ! Can someone pls help me and tell me how it is possible to Select a Node in 2nd row? I got a treeview which is build like follows: node2.Nodes.Add("blah", "blubb");...
2
by: Tymbow | last post by:
I'm building a web application that is analogous to the Windows XP file explorer in function. The left column contains a TreeView, and the right column a DataGrid populated by selecting TreeView...
2
by: dav61000 | last post by:
I am new to VB.net so I am not sure if there is a good way to do this or not but here is my problem. I have created a form with a TreeView control on it. When the user selects a node from the...
1
by: DeveloperX | last post by:
Hi, I should probably start with my XML as it makes it easier to explain the problem. <?xml version="1.0" encoding="utf-8" ?> <aa note="top level"> <b1 note="b1"> <c1 note="b1 c1"> <d1...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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.