C# XPath relative node values query...

Status
Not open for further replies.

fitten

Ars Legatus Legionis
55,634
Subscriptor++
OK, my brain just isn't functioning right today and I've spent too much time on this.

I have some XML that looks like this...

Code:
<root>
  <element>
    <child>
      <entry>value1</entry>
      <entry>value1</entry>
      <entry>value2</entry>
      <entry>value3</entry>
      <entry>value1</entry>
      <entry>value3</entry>
      <entry>value2</entry>
      <entry>value2</entry>
      <entry>value3</entry>
      <entry>value1</entry>
    </child>
  </element>
</root>

I have an XPathNavigator pointing to /root/element/child (call it nodeNav). I want to 'dedupe' the child elements... at the end, I want something that looks like this:

Code:
<root>
  <element>
    <child>
      <entry>value1</entry>
      <entry>value2</entry>
      <entry>value3</entry>
    </child>
  </element>
</root>

(the order of the values of the child/entry nodes does not matter in the final output).

I've tried stuff like this:

Code:
        private static void Dedupe(XPathNavigator nodeNav)
        {
            int index = 1;
            const string childQueryTemplate = "./*[position() = {0}]";
            string childQuery = string.Format(childQueryTemplate, index);
            XPathNavigator childElement = nodeNav.SelectSingleNode(childQuery);
            while (childElement != null)
            {
                const string childValueQueryTemplate = "./*[text() = ./[{0}]/text()]";
                string query = string.Format(childValueQueryTemplate, index);
                XPathNodeIterator ni = nodeNav.Select(query);
                if (ni.Count > 1)
                    childElement.DeleteSelf();
                else
                    index++;
                childElement = nodeNav.SelectSingleNode(string.Format(childQueryTemplate, index));
            }
        }

with no luck so far. The node iterator inside the loop always returns a .Count = 0. Basically, I'm looking for a query that will return the navigators to all the ndoes of the parent nodeNav that have the same .Value. There may be a better query from the childElement's standpoint (give me all my siblings, including myself, that have the same text()) but I haven't looked down that path yet.
 

Metasyntactic

Ars Legatus Legionis
31,808
I'm not in front of a computer, but wouldn't this be way easier with XLinq? Just do the following:

Code:
XElement source = ...

var result = new XElement(
  source.Name,
  source.Elements().Distinct((x1, x2) => x1.Value == x2.Value));

'Distinct' is order preserving, so it should get you precisely what you need. That said, it may take an IEqualityComparer instead of a Comparison (why god why?!), so you may have to create an extension that wraps the lambda in a comparer.
 

Apteris

Ars Tribunus Angusticlavius
9,590
Subscriptor
Wraith":1lk01u4x said:
That's XPath 2.0, which may not be implemented depending on his environment/toolkit.
I know. Saxon is always an option.

Do you need to do it in code or does the xpath i suggested not return the correct results for some reason?
Your code doesn't work for staggered duplicates.
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
I gave it a try... using this query:
Code:
"./*[not(.=preceding-sibling::*/.)]"

I'm given a navigator to child so all paths must be relative to it or its children and the element names may be different (or the same as other elements), like this (the tag names are not important, the values need to be unique in the output, though):

Code:
<root>
  <element>
    <child>
      <entry1>value1</entry1>
      <entry4>value1</entry4>
      <entry34>value2</entry34>
      <entry1>value3</entry1>
      <entry234>value1</entry234>
      <entryabc>value3</entryabc>
      <entry1b>value2</entry1b>
      <entry1>value2</entry1>
      <entryb>value3</entryb>
      <entry34>value1</entry34>
    </child>
  </element>
</root>

The above query, when SelectSingleNode from any child, always returns null (no match).
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
Wraith":73xd5jvn said:
You can't select the parent nodes like that. You can get distinct Entry* children with it from which you could construct a fragment which has the structure you want but to do the whole thing in one step you'd probably need an xslt.

You're probably best off using one of the functions.

Yeah, right now I do it a bit more bluntly... loop through remembering what values I've seen before and deleting any subsequent children that have a value seen before.
 
Status
Not open for further replies.