Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I identify duplicate nodes in XPath 1.0 using an XPathNavigator to evaluate?
    primarykey
    data
    text
    <p>I am trying to identify duplicate serial numbers from the following xml using XPath 1.0 and then evaluating it in .Net using an <code>XPathNavigator</code>.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;Inventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Items&gt; &lt;Item&gt; &lt;SerialNumber&gt;1111&lt;/SerialNumber&gt; &lt;/Item&gt; &lt;Item&gt; &lt;SerialNumber&gt;1112&lt;/SerialNumber&gt; &lt;/Item&gt; &lt;Item&gt; &lt;SerialNumber&gt;1112&lt;/SerialNumber&gt; &lt;/Item&gt; &lt;/Items&gt; &lt;/Inventory&gt; </code></pre> <p>I tried to do this by evaluating this</p> <pre><code>//Items/Item/SerialNumber </code></pre> <p>expression in a custom XSLT Context Function (implementing <code>IXsltContextFunction</code> <a href="http://msdn.microsoft.com/en-us/library/dd567715" rel="nofollow">like this MSDN example</a>) in .Net but the Invoke function gets called one result at a time so I have no visibility of the other results to find duplicates.</p> <p><strong>1) Is there a way of doing this using a single XPath 1.0 expression?</strong></p> <p>OR</p> <p><strong>2) Is there a way of passing in an array of elements into a single Invoke call of the custom XSLT Context Function class?</strong> I'm working in VB.Net but am happy with any C# examples anyone can share. </p> <p>Thanks,</p> <p>Gavin</p> <p><strong>Edit</strong></p> <p>Thanks to O R Mapper and Dimitre for their responses. I initially accepted O R Mapper's response since it did do what I asked. I've since accepted Dimitre's answer since I like that it provides a distinct list of values. Both responses very helpful though!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COGavin Sutherland: Are you aware that the currently accepted answer is incorrect? For the provided XML document it selects a single node. However, if there are more than two elements with the same string value (let's say three or more `<SerialNumber>1112</SerialNumber>`), then the XPath expression selects every duplicate, with the exception of the first. So, if there are 10 elements `<SerialNumber>1112</SerialNumber>` the expression selects nine text nodes "1112". It seems to me, that you want only one "1112" text node to be selected.
      singulars
    2. CO@DimitreNovatchev: The question asks for how to find serial numbers that are duplicates. So, if there are 10 elements `<SerialNumber>1112</SerialNumber>`, then 9 of those elements are duplicates. Hence, the originally accepted answer was exactly what was asked for. The question doesn't say what is going to happen with the respective nodes, so there is no reason to automatically assume the OP doesn't want a complete list of duplicate nodes occurring in the document.
      singulars
    3. CO@O.R.Mapper: Yes, this is why in my comment I asked if my guess were right -- turns out I was... In my experience, "realworld" questions, more often than not, mean something different than they are saying -- and we need to live with that fact and adapt. Developing one's guessing power isn't something bad, after all.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload