/// <summary> /// Vraca spisak svih xPathova - PREVAZIDJENO JER POSTOJI EFIKASNIJI METOD PREKO END NODE-a i XPatha /// </summary> /// <param name="node">Parent xmlNode</param> /// <param name="nsManager">Menadžer za namespace</param> /// <param name="prefixToTrim">Od svake putanje može odseći određeni prefiks</param> /// <param name="toDefaultList">Rezultat da bude u DefaultList / Preset list formatu</param> /// <returns>Spisak svih xPath putanja</returns> public static List <String> FindAllXPaths(XmlNode node, imbNamespaceSetup nsSetup = null, String prefixToTrim = "", Boolean toDefaultList = false) { List <String> output = new List <string>(); if (nsSetup == null) { if (node.OwnerDocument != null) { nsSetup = new imbNamespaceSetup(node.OwnerDocument); //nsManager = new XmlNamespaceManager(node.OwnerDocument.NameTable); } } String path = FindXPath(node, nsSetup.nsPrefix); // path = imbStringPathTools.removeCommonRoot(path, prefixToTrim); if (toDefaultList) { path = node.Name + "|" + path + ";"; } output.Add(path); foreach (XmlNode nd in node.ChildNodes) { output.AddRange(FindAllXPaths(nd, nsSetup, prefixToTrim, toDefaultList)); } return(output); }
/// <summary> /// Osigurava da prosledjeni XmlDocument ima element za dati xPath i vraća referencu prema poslednjem XmlNode-u koji je kreiran /// </summary> public static XmlNode makeNodeByxPath(XmlDocument doc, XmlNode parent, String xPath, XmlNode source, imbNamespaceSetup nsSetup = null) { XmlNode output; // = new XmlNode(); if (String.IsNullOrEmpty(xPath)) { return(parent); } string[] partsOfXPath = xPath.Trim('/').Split('/'); string nextNodeInXPath = partsOfXPath.First(); if (string.IsNullOrEmpty(nextNodeInXPath)) { return(parent); } if (nsSetup == null) { nsSetup = new imbNamespaceSetup(doc); } // get or create the node from the name //XmlNode node = parent.SelectSingleNode(nextNodeInXPath); String pt = "/" + nextNodeInXPath; XmlNode node = imbAdvancedXPath.xPathExecution(pt, parent, nsSetup, queryEngine.imbXPathQuery, true, 0); if (node == null) { String cleanName = imbStringPathTools.getTitleFromPath(nextNodeInXPath); node = doc.CreateNode(XmlNodeType.Element, nsSetup.nsPrefix, cleanName, nsSetup.nsSourceUrl); try { node = parent.AppendChild(node); } catch (Exception ex) { logSystem.log("Node on> " + node.FindXPath(nsSetup) + " failed: " + ex.Message, logType.Notification); } } // rejoin the remainder of the array as an xpath expression and recurse string rest = String.Join("/", partsOfXPath.Skip(1).ToArray()); if (partsOfXPath.Length == 1) { foreach (XmlAttribute att in source.Attributes) { throw new NotImplementedException(nameof(makeNodeByxPath)); // node.SetAttribute(att.Name, att.Value); } } return(makeNodeByxPath(doc, node, rest, source, nsSetup)); }
/// <summary> /// GLAVNI MEHANIZAM - low-level /// </summary> /// <param name="xPath"></param> /// <param name="source"></param> /// <param name="nsSetup"></param> /// <param name="engine"></param> /// <param name="report"></param> /// <returns></returns> public static List <imbXPathQuery> xPathExecution(String xPath, XmlNode source, imbNamespaceSetup nsSetup, queryEngine engine, Boolean trimPathWithSource) { List <imbXPathQuery> output = new List <imbXPathQuery>(); imbXPathQuery tmp = null; XmlNode nd = null; if (source == null) { logSystem.log("Source XML is empty", logType.Notification); return(output); } if (nsSetup == null) { nsSetup = new imbNamespaceSetup(source.OwnerDocument); } try { switch (engine) { case queryEngine.imbXPathQuery: List <XmlNode> inp = imbXmlXPathTools.queryXPath(xPath, nsSetup.namespaceManager, source, trimPathWithSource); foreach (XmlNode ndd in inp) { tmp = new imbXPathQuery(xPath, ndd); output.Add(tmp); } break; case queryEngine.xmlNodeSelect: XmlNodeList rez = source.SelectNodes(xPath, nsSetup.namespaceManager); foreach (XmlNode ndd in rez) { tmp = new imbXPathQuery(xPath, ndd); output.Add(tmp); } break; case queryEngine.xmlNodeSelectSingle: nd = source.SelectSingleNode(xPath, nsSetup.namespaceManager); tmp = new imbXPathQuery(xPath, nd); output.Add(tmp); break; } } catch (Exception ex) { tmp = new imbXPathQuery(xPath, null); tmp.report += Environment.NewLine + "Error: " + ex.Message; output.Add(tmp); } return(output); }
/// <summary> /// 2014c: glavni poziv za izvrsavanje xPath upita nad izvorom - ne vrsi lokalizaciju putanje /// </summary> /// <param name="xPath"></param> /// <param name="source"></param> /// <param name="ns"></param> /// <param name="engine"></param> /// <returns></returns> public static imbXPathQueryCollection xPathExecute(this String xPath, IXPathNavigable _source, imbNamespaceSetup ns = null, queryEngine engine = queryEngine.xmlNodeSelect) { XmlNode source = _source as XmlNode; if (ns == null) { ns = new imbNamespaceSetup(source.OwnerDocument); } var lst = imbAdvancedXPath.xPathExecution(xPath, source, ns, engine, false); imbXPathQueryCollection output = new imbXPathQueryCollection(lst); return(output); }
/// <summary> /// 2013a: Vrši xPath upit i vraća onaj element koji je definisan u Index parametru. /// </summary> /// <param name="xPath"></param> /// <param name="source"></param> /// <param name="nsSetup"></param> /// <param name="engine"></param> /// <param name="trimPathWithSource"></param> /// <param name="index"></param> /// <returns></returns> public static XmlNode xPathExecution(String xPath, XmlNode source, imbNamespaceSetup nsSetup, queryEngine engine, Boolean trimPathWithSource, Int32 index) { List <imbXPathQuery> output = xPathExecution(xPath, source, nsSetup, engine, trimPathWithSource); imbXPathQuery first = output.FirstOrDefault(); //.imbGetFirstValue<imbXPathQuery>(null, false, index); XmlNode nd = null; if (first != null) { nd = first.xmlNode; } return(nd); }
public static string FindXPath(this XmlNode node, imbNamespaceSetup nsSetup) { return(FindXPath(node, nsSetup.nsPrefix)); }