473,782 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XPATH Nearest Node

Hi:
Is there a way in XPATH to find the nearest node of the node in
context with a certain attribute value. Here is my problem. I have the
following XML and I am trying to add all the nodes with attribute value

LNum=1 as child nodes of the nearest node above it with attribute
LNum=0....and add all the nodes with attribute value LNum=2 as child
nodes of the nearest node above it with attribute LNum=1 and so on. The

LNum value can go upto 10 or more but the pattern is always the same as

shown below. Any help on this will be greatly appreciated.
Thanks,
<Axes1 Axes1="1" Hierarchy="MHHS Entity">
<MHHSEntity Name="FB-MHHS" LNum="0" FmtValue="12,74 8"
Value="12748" />
<MHHSEntity Name="File Failure" LNum="1" FmtValue="8" Value="8"

/>
<MHHSEntity Name="File Success" LNum="1" FmtValue="3,179 "
Value="3179" />
<MHHSEntity Name="IIS Success" LNum="1" FmtValue="3,187 "
Value="3187" />
<MHHSEntity Name="MSMQ Failure" LNum="1" FmtValue="0" Value="0"

/>
<MHHSEntity Name="MSMQ Success" LNum="1" FmtValue="3,187 "
Value="3187" />
<MHHSEntity Name="File Failure" LNum="2" FmtValue="8" Value="8"
/>
<MHHSEntity Name="File Success" LNum="2" FmtValue="3,179 "
Value="3179"
/>
<MHHSEntity Name="IIS Success" LNum="2" FmtValue="3,187 "
Value="3187"
/>
<MHHSEntity Name="SQL Failure" LNum="1" FmtValue="1,443 "
Value="1443" />
<MHHSEntity Name="SQL Success" LNum="1" FmtValue="1,744 "
Value="1744" />
<MHHSEntity Name="KT-MHHS" LNum="0" FmtValue="12,90 4"
Value="12904" />
<MHHSEntity Name="File Failure" LNum="1" FmtValue="7" Value="7"

/>
<MHHSEntity Name="File Success" LNum="1" FmtValue="3,219 "
Value="3219" />
<MHHSEntity Name="IIS Failure" LNum="1" FmtValue="0" Value="0"
/>
<MHHSEntity Name="IIS Success" LNum="1" FmtValue="3,226 "
Value="3226" />
<MHHSEntity Name="MSMQ Success" LNum="1" FmtValue="3,226 "
Value="3226" />
<MHHSEntity Name="SQL Failure" LNum="1" FmtValue="1,301 "
Value="1301" />
<MHHSEntity Name="SQL Success" LNum="1" FmtValue="1,925 "
Value="1925" />
<MHHSEntity Name="MC-MHHS" LNum="0" FmtValue="14,60 8"
Value="14608" />
<MHHSEntity Name="File Failure" LNum="1" FmtValue="10"
Value="10" />
<MHHSEntity Name="File Failure" LNum="2" FmtValue="8" Value="8"
/>
<MHHSEntity Name="File Success" LNum="2" FmtValue="3,179 "
Value="3179"
/>
<MHHSEntity Name="File Failure" LNum="3" FmtValue="8" Value="8"
/>
<MHHSEntity Name="File Success" LNum="3" FmtValue="3,179 "
Value="3179"
/>
<MHHSEntity Name="IIS Success" LNum="3" FmtValue="3,187 "
Value="3187"
/>
<MHHSEntity Name="IIS Success" LNum="2" FmtValue="3,187 "
Value="3187"
/>
<MHHSEntity Name="File Success" LNum="1" FmtValue="3,642 "
Value="3642" />
<MHHSEntity Name="IIS Failure" LNum="1" FmtValue="0" Value="0"
/>
<MHHSEntity Name="IIS Success" LNum="1" FmtValue="3,652 "
Value="3652" />
<MHHSEntity Name="MSMQ Success" LNum="1" FmtValue="3,652 "
Value="3652" />
<MHHSEntity Name="SQL Failure" LNum="1" FmtValue="1,470 "
Value="1470" />
<MHHSEntity Name="SQL Success" LNum="1" FmtValue="2,182 "
Value="2182" />
</Axes1>

Jul 20 '05 #1
2 2629
nkunapa wrote:
Hi:
Is there a way in XPATH to find the nearest node of the node in
context with a certain attribute value. Here is my problem. I have the
following XML and I am trying to add all the nodes with attribute value

LNum=1 as child nodes of the nearest node above it with attribute
LNum=0....and add all the nodes with attribute value LNum=2 as child
nodes of the nearest node above it with attribute LNum=1 and so on. The


Hi,

Have you thought about selecting all suitable nodes in the
preceding-sibling axis, and then pick the first of them? That should be
the nearest above (unless I have messed up something). The FIRST is
becuase the preceding-sibling is reverse; the first is the nearest to
the context.

preceding-sibling::*[@LNum=$MyLNumMi nus1][1]

I tried it in an XSLT (just because that's all I have working right now
for evaluating XPath):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="thing">
<xsl:variable name="myLNumMin us1" select="@LNum - 1"/>
<xsl:variable name="catch"
select="precedi ng::*[@LNum=$myLNumMi nus1][1]/@name"/>
<xsl:if test="$catch">
This node had a suitable ancestor:
<thing myName="{@name} " myLNum="{@LNum} "
ImLookingFor="{ $myLNumMinus1}"
my-nearest-predecessor-with-that-LNum="{$catch}"/>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
For input file:

<?xml version="1.0"?>
<thing name="ancestor0 " LNum="0">
<thing name="ancestor1 " LNum="0">
<thing name="ancestor2 " LNum="0">
<thing name="a-predecessor-but-not-an-ancestor-to-the-successor"
LNum="1"/>
<thing name="depth3" LNum="1">
<thing name="depth4" LNum="2"/>
<thing name="depth4, again" LNum="1"/>
</thing>
<thing name="depth3, again" LNum="2"/>
</thing>
</thing>
</thing>

I got
[dongfang@granad a nearest-ancestor]$ xsltproc nearest-predecessor.xsl
things.xml
<?xml version="1.0"?>
This node had a suitable ancestor:
<thing myName="depth4" myLNum="2" ImLookingFor="1 "
my-nearest-predecessor-with-that-LNum="a-predecessor-but-not-an-ancestor-to-the-successor"/>
This node had a suitable ancestor:
<thing myName="depth3, again" myLNum="2" ImLookingFor="1 "
my-nearest-predecessor-with-that-LNum="depth4, again"/>
[dongfang@granad a nearest-ancestor]$
With your "flat" documents it doesn't matter whether you use precesing
or preceding-sibling; it's the same.

Soren

Jul 20 '05 #2
Soren:
Wow! This worked. Thanks a lot. You really gave me a big hint . I
never came across "preceding-sibling" nor "preceding" XPATH queries. I
really appreciate your help on this.

Thanks,
NVK

Jul 20 '05 #3

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

Similar topics

1
6826
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
0
1929
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
9
2896
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY> </REGION> <REGION Name="NorthAmerica" WingDing="Yellow"> <COUNTRY Name="Canada"> <TOWN Name="Quebec" WingDing="Brown" />
9
2155
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
5
7932
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
2265
by: Jeff | last post by:
Hi all, I'm wondering if there is a function that will return the xpath to a specific node given its context node. Essentially, I want the reverse of the document.evaluate functionality. I've seen examples where people build up the xpath themselves by walking the DOM, but I thought there has to be a better way. I've included a simple example of this, based on code from the Solvent project at MIT. This code works reasonably well, but...
3
2630
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; We have a TreeView that represents an xml file (or it's schema is a more accurate statement). We want to have a double click on a node in the tree generate the XPath to get to that node. For the full tree it's easy. We walk up the parents getting the name of each node and put a / between each so we end up with /node1/node2 or node1/node2/@attr1
0
10146
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...
0
9944
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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
7494
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
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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...
1
4044
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
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.