StefH / XPath2.Net

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

Is tokenize supported? #69

Closed smitron closed 6 months ago

smitron commented 6 months ago

Hello, would you kindly show me an example of tokenize?

In online xpath testers (freeformatter.com) using the xml below, the xpath2 expression: //ListContext[matches(.,'[0-9]')]/tokenize(., ' ')[1]" gives the result of 6.2.2

but In the code sample below all results evaluate to null.

               string xml = 
                    "<ItemContext>\r\n" +
                    "   <ListContext>6.2.2 Maximum Output Power @ Band7</ListContext>\r\n" +
                    "</ItemContext>";

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);

                XPathNavigator nav = xmlDoc.CreateNavigator();
                var result0 = (XPath2NodeIterator)nav.XPath2Evaluate("//ListContext[matches(.,'[0-9]')]/tokenize(., ' ')[1]");
                var result1 = nav.XPath2SelectSingleNode("//ListContext[matches(.,'[0-9]')]/tokenize(., ' ')[1]");
                var result2 = nav.XPath2SelectSingleNode("tokenize(//ListContext[matches(.,'[0-9]')], ' ')[1]");

Many thanks.

smitron commented 6 months ago

I got it to work using XDocument instead of XMLDocument and XPathNavigator.

                string xml = 
                    "<ItemContext>\r\n" +
                    "   <ListContext>6.2.2 Maximum Output Power @ Band7</ListContext>\r\n" +
                    "</ItemContext>";

                XDocument xDoc = XDocument.Parse(xml);
                var obj = xDoc.XPath2Select("//ListContext[matches(.,'[0-9]')]/tokenize(., ' ')[1]");`