StefH / XPath2.Net

Lightweight XPath2 for .NET
Microsoft Public License
36 stars 14 forks source link

Using XPath2Evaluate() without a function in command there's always a comma at the end of the value #13

Closed SpartanNico94 closed 3 years ago

SpartanNico94 commented 6 years ago

When using .NET function XPath2Evalute with a command as "//Node1/Element1" and converting to string I always get a string like this "< value >, " (with comma AND space in string); but if I use a function as substring(), string-join() or number() I get the correct node value.

To avoid this behaviour I have to use concat( < command >, '') and I get the node value as is written in the xml file.

Is this the expected behavior or it is a bug?

StefH commented 4 years ago

@SpartanNico94 Can you provide a full example please?

StefH commented 3 years ago

@SpartanNico94 Can you provide a full example please?

SpartanNico94 commented 3 years ago

@StefH, here is an example. There are comments to help you understand better the problem. I solved using xpath functions such as substring() etc or .XPath2SelectNode().InnerText in order to get the value of the node

using System;
using System.Xml;
using Wmhelp.XPath2;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            string XmlTest = "<start>"
                                + "<node1>Value1</node1>"
                                + "<node2>"
                                    + "<subnode1>Value2</subnode1>"
                                + "</node2>"
                            + "</start>";

            XmlDocument Xml = new XmlDocument();
            Xml.LoadXml(XmlTest);

            string value = Xml.XPath2Evaluate("/start/node2/subnode1").ToString();
            //!!the pipe (|) is used to see the space in console!!
            Console.WriteLine($"|{value}|"); //value is "Value2, " (without double quotes but with space)
            Console.ReadKey();
        }
    }
}
StefH commented 3 years ago

I see.

However I think you should use:

static void Main(string[] args)
{

    string XmlTest = "<start>"
                     + "<node1>Value1</node1>"
                     + "<node2>"
                     + "<subnode1>Value2</subnode1>"
                     + "</node2>"
                     + "</start>";

    var doc = new XmlDocument();
    doc.LoadXml(XmlTest);

    XPathNavigator navigator = doc.CreateNavigator();

    var xpath = "string(/start/node2/subnode1)";

    string value = (string) navigator.Evaluate(xpath);
    Console.WriteLine($"XPath  = |{value}|");

    string value2 = (string) navigator.XPath2Evaluate(xpath);
    Console.WriteLine($"XPath2 = |{value2}|");
}

Which returns:

XPath  = |Value2|
XPath2 = |Value2|
StefH commented 3 years ago

The issue with the , is here: https://github.com/StefH/XPath2.Net/blob/master/src/XPath2/XPath2NodeIterator.cs#L177

I'll take a look.

SpartanNico94 commented 3 years ago

I think you should add a line of code which remove the last [, ] "token" in order to get a well formatted string.

Thanks for your help.

StefH commented 3 years ago

https://github.com/StefH/XPath2.Net/pull/45