Ejemplo n.º 1
0
 public static void NodesBeforeSelfBeforeAndAfter()
 {
     XText aText = new XText("a"), bText = new XText("b");
     XElement a = new XElement("A", aText, bText);
     IEnumerable<XNode> nodes = bText.NodesBeforeSelf();
     Assert.Equal(1, nodes.Count());
     aText.Remove();
     Assert.Equal(0, nodes.Count());
 }
Ejemplo n.º 2
0
        public static void NodesBeforeSelfBeforeAndAfter()
        {
            XText               aText = new XText("a"), bText = new XText("b");
            XElement            a     = new XElement("A", aText, bText);
            IEnumerable <XNode> nodes = bText.NodesBeforeSelf();

            Assert.Equal(1, nodes.Count());
            aText.Remove();
            Assert.Equal(0, nodes.Count());
        }
        public static List <XText> GetPreceedingSiblingXTexts(this XText xtxt)
        {
            List <XText> xtxts = null;

            if (xtxt != null)
            {
                var texts = xtxt.NodesBeforeSelf().Where(x => x is XText).ToList();
                if (texts != null && texts.Count > 0)
                {
                    xtxts = new List <XText>();
                    foreach (var x in texts)
                    {
                        XText y = x as XText;
                        if (y != null)
                        {
                            xtxts.Add(y);
                        }
                    }
                }
            }
            return(xtxts);
        }
Ejemplo n.º 4
0
 public static string GetXPath(this XObject xobj)
 {
     if (xobj.Parent == null)
     {
         XDocument doc = xobj as XDocument;
         if (doc != null)
         {
             return(".");
         }
         XElement el = xobj as XElement;
         if (el != null)
         {
             return("/" + NameWithPredicate(el));
         }
         XText xt = xobj as XText;
         if (xt != null)
         {
             return(null);
         }
         //
         //the following doesn't work because the XPath data
         //model doesn't include white space text nodes that
         //are children of the document.
         //
         //return
         //    "/" +
         //    (
         //        xt
         //        .Document
         //        .Nodes()
         //        .OfType<XText>()
         //        .Count() != 1 ?
         //        "text()[" +
         //        (xt
         //        .NodesBeforeSelf()
         //        .OfType<XText>()
         //        .Count() + 1) + "]" :
         //        "text()"
         //    );
         //
         XComment com = xobj as XComment;
         if (com != null)
         {
             return
                 ("/" +
                  (
                      com
                      .Document
                      .Nodes()
                      .OfType <XComment>()
                      .Count() != 1 ?
                      "comment()[" +
                      (com
                       .NodesBeforeSelf()
                       .OfType <XComment>()
                       .Count() + 1) +
                      "]" :
                      "comment()"
                  ));
         }
         XProcessingInstruction pi = xobj as XProcessingInstruction;
         if (pi != null)
         {
             return
                 ("/" +
                  (
                      pi.Document.Nodes()
                      .OfType <XProcessingInstruction>()
                      .Count() != 1 ?
                      "processing-instruction()[" +
                      (pi
                       .NodesBeforeSelf()
                       .OfType <XProcessingInstruction>()
                       .Count() + 1) +
                      "]" :
                      "processing-instruction()"
                  ));
         }
         return(null);
     }
     else
     {
         XElement el = xobj as XElement;
         if (el != null)
         {
             return
                 ("/" +
                  el
                  .Ancestors()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  NameWithPredicate(el));
         }
         XAttribute at = xobj as XAttribute;
         if (at != null)
         {
             return
                 ("/" +
                  at
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  "@" + GetQName(at));
         }
         XComment com = xobj as XComment;
         if (com != null)
         {
             return
                 ("/" +
                  com
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      com
                      .Parent
                      .Nodes()
                      .OfType <XComment>()
                      .Count() != 1 ?
                      "comment()[" +
                      (com
                       .NodesBeforeSelf()
                       .OfType <XComment>()
                       .Count() + 1) + "]" :
                      "comment()"
                  ));
         }
         XCData cd = xobj as XCData;
         if (cd != null)
         {
             return
                 ("/" +
                  cd
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      cd
                      .Parent
                      .Nodes()
                      .OfType <XText>()
                      .Count() != 1 ?
                      "text()[" +
                      (cd
                       .NodesBeforeSelf()
                       .OfType <XText>()
                       .Count() + 1) + "]" :
                      "text()"
                  ));
         }
         XText tx = xobj as XText;
         if (tx != null)
         {
             return
                 ("/" +
                  tx
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      tx
                      .Parent
                      .Nodes()
                      .OfType <XText>()
                      .Count() != 1 ?
                      "text()[" +
                      (tx
                       .NodesBeforeSelf()
                       .OfType <XText>()
                       .Count() + 1) + "]" :
                      "text()"
                  ));
         }
         XProcessingInstruction pi = xobj as XProcessingInstruction;
         if (pi != null)
         {
             return
                 ("/" +
                  pi
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      pi
                      .Parent
                      .Nodes()
                      .OfType <XProcessingInstruction>()
                      .Count() != 1 ?
                      "processing-instruction()[" +
                      (pi
                       .NodesBeforeSelf()
                       .OfType <XProcessingInstruction>()
                       .Count() + 1) + "]" :
                      "processing-instruction()"
                  ));
         }
         return(null);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the X path.
        /// </summary>
        /// <param name="xobj">The xobj.</param>
        /// <returns>The x Path.</returns>
        public static string GetXPath(this XObject xobj)
        {
            if (xobj.Parent == null)
            {
                XDocument doc = xobj as XDocument;
                if (doc != null)
                {
                    return(".");
                }
                XElement el = xobj as XElement;
                if (el != null)
                {
                    return("/" + NameWithPredicate(el));
                }

                // the XPath data model does not include white space text nodes
                // that are children of a document, so this method returns null.
                XText xt = xobj as XText;
                if (xt != null)
                {
                    return(null);
                }
                XComment com = xobj as XComment;
                if (com?.Document != null)
                {
                    return("/" + (com.Document.Nodes().OfType <XComment>().Count() != 1 ? "comment()[" + (com.NodesBeforeSelf().OfType <XComment>().Count() + 1) + "]" : "comment()"));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi?.Document != null)
                {
                    return("/" + (pi.Document.Nodes().OfType <XProcessingInstruction>().Count() != 1 ? "processing-instruction()[" + (pi.NodesBeforeSelf().OfType <XProcessingInstruction>().Count() + 1) + "]" : "processing-instruction()"));
                }
                return(null);
            }
            else
            {
                XElement el = xobj as XElement;
                if (el != null)
                {
                    return("/" + el.Ancestors().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + NameWithPredicate(el));
                }

                XAttribute at = xobj as XAttribute;
                if (at?.Parent != null)
                {
                    return("/" + at.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + "@" + GetQName(at));
                }
                XComment com = xobj as XComment;
                if (com?.Parent != null)
                {
                    return("/" + com.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (com.Parent.Nodes().OfType <XComment>().Count() != 1 ? "comment()[" + (com.NodesBeforeSelf().OfType <XComment>().Count() + 1) + "]" : "comment()"));
                }
                XCData cd = xobj as XCData;
                if (cd?.Parent != null)
                {
                    return("/" + cd.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (cd.Parent.Nodes().OfType <XText>().Count() != 1 ? "text()[" + (cd.NodesBeforeSelf().OfType <XText>().Count() + 1) + "]" : "text()"));
                }
                XText tx = xobj as XText;
                if (tx?.Parent != null)
                {
                    return("/" + tx.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (tx.Parent.Nodes().OfType <XText>().Count() != 1 ? "text()[" + (tx.NodesBeforeSelf().OfType <XText>().Count() + 1) + "]" : "text()"));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi?.Parent != null)
                {
                    return("/" + pi.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (pi.Parent.Nodes().OfType <XProcessingInstruction>().Count() != 1 ? "processing-instruction()[" + (pi.NodesBeforeSelf().OfType <XProcessingInstruction>().Count() + 1) + "]" : "processing-instruction()"));
                }
                return(null);
            }
        }
        /// <summary>
        /// Recupera l'esspressione xpath di un determinato XObject
        /// </summary>
        /// <param name="xobj">Oggetto da valutare</param>
        /// <returns>xpath in formato stringa</returns>
        public static string GetXPath(this XObject xobj)
        {
            if (xobj.Parent == null)
            {
                var doc = xobj as XDocument;
                if (doc != null)
                {
                    return(".");
                }

                var el = xobj as XElement;
                if (el != null)
                {
                    return("/" + NameWithPredicate(el));
                }

                XText xt = xobj as XText;
                if (xt != null)
                {
                    return(null);
                }

                XComment com = xobj as XComment;
                if (com != null)
                {
                    return
                        ("/" +
                         (
                             com
                             .Document
                             .Nodes()
                             .OfType <XComment>()
                             .Count() != 1 ?
                             "comment()[" +
                             (com
                              .NodesBeforeSelf()
                              .OfType <XComment>()
                              .Count() + 1) +
                             "]" :
                             "comment()"
                         ));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi != null)
                {
                    return
                        ("/" +
                         (
                             pi.Document.Nodes()
                             .OfType <XProcessingInstruction>()
                             .Count() != 1 ?
                             "processing-instruction()[" +
                             (pi
                              .NodesBeforeSelf()
                              .OfType <XProcessingInstruction>()
                              .Count() + 1) +
                             "]" :
                             "processing-instruction()"
                         ));
                }
                return(null);
            }
            else
            {
                XElement el = xobj as XElement;
                if (el != null)
                {
                    return
                        ("/" +
                         el
                         .Ancestors()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         NameWithPredicate(el));
                }
                XAttribute at = xobj as XAttribute;
                if (at != null)
                {
                    return
                        ("/" +
                         at
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         "@" + GetQName(at));
                }
                XComment com = xobj as XComment;
                if (com != null)
                {
                    return
                        ("/" +
                         com
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             com
                             .Parent
                             .Nodes()
                             .OfType <XComment>()
                             .Count() != 1 ?
                             "comment()[" +
                             (com
                              .NodesBeforeSelf()
                              .OfType <XComment>()
                              .Count() + 1) + "]" :
                             "comment()"
                         ));
                }
                XCData cd = xobj as XCData;
                if (cd != null)
                {
                    return
                        ("/" +
                         cd
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             cd
                             .Parent
                             .Nodes()
                             .OfType <XText>()
                             .Count() != 1 ?
                             "text()[" +
                             (cd
                              .NodesBeforeSelf()
                              .OfType <XText>()
                              .Count() + 1) + "]" :
                             "text()"
                         ));
                }
                XText tx = xobj as XText;
                if (tx != null)
                {
                    return
                        ("/" +
                         tx
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             tx
                             .Parent
                             .Nodes()
                             .OfType <XText>()
                             .Count() != 1 ?
                             "text()[" +
                             (tx
                              .NodesBeforeSelf()
                              .OfType <XText>()
                              .Count() + 1) + "]" :
                             "text()"
                         ));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi != null)
                {
                    return
                        ("/" +
                         pi
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             pi
                             .Parent
                             .Nodes()
                             .OfType <XProcessingInstruction>()
                             .Count() != 1 ?
                             "processing-instruction()[" +
                             (pi
                              .NodesBeforeSelf()
                              .OfType <XProcessingInstruction>()
                              .Count() + 1) + "]" :
                             "processing-instruction()"
                         ));
                }
                return(null);
            }
        }