XPATH- Find Nearest Node

  • Thread starter Thread starter nkunapa
  • Start date Start date
N

nkunapa

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="MHHSEntity">
<MHHSEntity Name="FB-MHHS" LNum="0" FmtValue="12,748"
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,904"
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,608"
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>
 
This should return the nearest node that has a LNum attribute that has a
value of 2

SelectSingleNode("//*@LNum[2])
 
Oops! Make that:

SelectSingleNode("//*@LNum[2]")


Scott M. said:
This should return the nearest node that has a LNum attribute that has a
value of 2

SelectSingleNode("//*@LNum[2])


nkunapa said:
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="MHHSEntity">
<MHHSEntity Name="FB-MHHS" LNum="0" FmtValue="12,748"
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,904"
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,608"
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>
 
Back
Top