Excluding certain XML child elements by depth using XPath

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Dear all,
Having this sample XML:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Hardware<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Motherboard<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Audio<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Speaker<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Audio Card<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; Category<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Video<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Monitor<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; LCD<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 3<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Plasma<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 3<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; Category<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Graphic Card<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; TV Card<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; Category<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; CPU<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; Category<span style="color:Blue; >
[/code]
I would like for instance to control the elements I get by excluding them by depth; for instance, I wish to exclude elements that have Depth values greather than 2 (excluding Depth 3 in our case) could I get this following XML node set using XPath?
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Hardware<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 0<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Motherboard<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Audio<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Speaker<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Audio Card<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; Category<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Video<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Monitor<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Graphic Card<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; TV Card<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; Category<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Category <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; CPU<span style="color:Black; " <span style="color:Red; Depth<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; Category<span style="color:Blue; >
[/code]
<hr class="sig Ahmed Abuabdou, Senior SharePoint Developer, Bank AlBilad

View the full article
 
Back
Top