473,395 Members | 1,413 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,395 software developers and data experts.

XPath Relational Query

To all,

I am trying to query an XML document of the following structure.

<ROOT>
<A>
<B id="1"/>
<B id="2"/>
<B id="3"/>
</A>
<A>
<C id="1"/>
<C id="2"/>
<C id="5"/>
<C id="6"/>
</A>
</ROOT>

I want to select all <C> nodes that do not have a <B> element
associated (by id). In other words, what i am looking for is <C
id="5"/> and <C id="6"/>. I realize XPath was not designed to perform
such queries but is there any way to accomplish this?

What I have in mind is something like this:

//C[count(//B[@id = @id]) = 0]

The first "@id" is referencing the "id" attribute of the <B> element. I
want the second "@id" to reference the "id" attribute of the <C>
element. I can't think of any way to do this with axis since they only
allow me to choose nodes relative to the current node which is B.

Thanks,
Michael B.

Jul 20 '05 #1
6 1157
Please try this XSL ..

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" encoding="UTF-8" indent="yes" />

<xsl:template match="/ROOT">
<result>
<xsl:for-each select=".//C">
<xsl:if test="not(@id = preceding::B/@id)">
<xsl:copy-of select="." />
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

Jul 20 '05 #2
Thanks Mukul,

I cannot use XSL. I am actually using an XmlDocument object as the
internal data structure for a windows .net application, I need to pick
nodes using XPath only. My other option is to write code to perform
this logic, but i was looking for a way to do this with XPath alone.

Thank you for your help.

Regards,
Michael B.

Jul 20 '05 #3
The XslTransform class of .NET framework allows to do XSLT (1.0)
transformation .. I guess it should help ..

Regards,
Mukul

Michael wrote:
Thanks Mukul,

I cannot use XSL. I am actually using an XmlDocument object as the
internal data structure for a windows .net application, I need to pick
nodes using XPath only. My other option is to write code to perform
this logic, but i was looking for a way to do this with XPath alone.

Thank you for your help.

Regards,
Michael B.


Jul 20 '05 #4
"Michael" <mb****@gmail.com> writes:
To all,

I am trying to query an XML document of the following structure.

<ROOT>
<A>
<B id="1"/>
<B id="2"/>
<B id="3"/>
</A>
<A>
<C id="1"/>
<C id="2"/>
<C id="5"/>
<C id="6"/>
</A>
</ROOT>

I want to select all <C> nodes that do not have a <B> element
associated (by id). In other words, what i am looking for is <C
id="5"/> and <C id="6"/>. I realize XPath was not designed to perform
such queries but is there any way to accomplish this?

What I have in mind is something like this:

//C[count(//B[@id = @id]) = 0]

The first "@id" is referencing the "id" attribute of the <B> element. I
want the second "@id" to reference the "id" attribute of the <C>
element. I can't think of any way to do this with axis since they only
allow me to choose nodes relative to the current node which is B.

Thanks,
Michael B.


//C[not(@id=//B/@id)]

David
Jul 20 '05 #5
Thanks a lot David, that did it.

Do you have any idea how the XPath processor executes this? Does it
execute //B/@id every time the <C> element is found or does it do it
only once and then checks the @id of <C> against that result set.

Thanks,
Michael

Jul 20 '05 #6
"Michael" <mb****@gmail.com> writes:
Thanks a lot David, that did it.

Do you have any idea how the XPath processor executes this? Does it
execute //B/@id every time the <C> element is found or does it do it
only once and then checks the @id of <C> against that result set.

Thanks,
Michael


one or the other:-)
it depends on the processor, some do take common expressions out of
loops and probably some don't. I think saxon does, for example.
Most XPath API presumably allow you to declare xpath variables
in which case you could give your processor a hint and stick //B/@id in
a variable and use a variable reference in the predicate, but if your
processor is doing that anyway that will make no difference.

It's not entirely trival for an optimiser to take an expression such as
//B/@id out of a loop as it's not a constant expression, the value of /
and hence the value of //B/@id depends on the document in which the
current current node sits. In this case all the nodes are necessarily in
the same docuument (as they are selected with //C) but in general this
can get tricky.

David
Jul 20 '05 #7

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

Similar topics

8
by: Terry P | last post by:
Are there any tools (java classes, tag libraries) which can translate xpath statements into a SQL query? Given an xpath query which has a predicate that filters node values or attributes, I want...
0
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
2
by: Echo 8 | last post by:
Is there a simple way of compare dates? I am trying to create a list of perishable stock in an inventory that is near or past its expiration date. I've tried telling it to compare a node's value...
0
by: chandrika.tripathy | last post by:
I am new to XML. I have the following question. I have data stored in a relational database. I want to use XPATH to query it. I have a schema defined which represents the data in the database. Is...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
3
by: werD | last post by:
Hello I have an xml document that im currently using a forward only .net repeater on and using some xpath queries to display the data The xml is quite simple <?xml version="1.0"...
2
by: werD | last post by:
Hello, Im wondering about using xml to store relational data. It works well with .net but im having some trouble with the xsl/xpath. Lets say that I have an xml like <data> <files> <file...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
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...
0
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...
0
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...

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.