public void GetXPathTest2() { var p = new Paragraph(new Run(), new BookmarkEnd(), new Run()); var body = new Body(p); AssertNamespace(XmlPath.GetXPath(p.FirstChild), @"/w:body[1]/w:p[1]/w:r[1]", new NS("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")); }
public void GetXPathTest5() { var p = new Paragraph(new Run(), new BookmarkEnd(), new Run()); var body = new Body(p); body.PrependChild(new Paragraph()); var r = p.LastChild; var unknown1 = p.PrependChild(new OpenXmlUnknownElement("my:test", "http://my")); AssertNamespace(XmlPath.GetXPath(unknown1), @"/w:body[1]/w:p[2]/my:test[1]", new NS("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"), new NS("my", "http://my")); }
public void GetXPathTest7() { var p = new Paragraph(new Run(), new BookmarkEnd(), new Run()); var body = new Body(p); body.PrependChild(new Paragraph()); var r = p.LastChild; var unknown1 = p.PrependChild(new OpenXmlUnknownElement("my:test", "http://my")); var unknown2 = p.AppendChild(new OpenXmlUnknownElement("my:test", "http://my")); var miscNode = p.AppendChild(new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- comment -->")); AssertNamespace(XmlPath.GetXPath(miscNode), @"/w:body[1]/w:p[2]/<!-- comment -->", new NS("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")); }
public void GetXPathTest8() { var p = new Paragraph(new Run(), new BookmarkEnd(), new Run()); var body = new Body(p); body.PrependChild(new Paragraph()); var r = p.LastChild; var unknown1 = p.PrependChild(new OpenXmlUnknownElement("my:test", "http://my")); var unknown2 = p.AppendChild(new OpenXmlUnknownElement("my:test", "http://my")); AssertNamespace(XmlPath.GetXPath(r), @"/w:body[1]/w:p[2]/w:r[2]", new NS("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")); using (var stream = new MemoryStream()) using (var doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document)) { doc.AddMainDocumentPart(); doc.MainDocumentPart.Document = new Document(body); AssertNamespace(XmlPath.GetXPath(r), doc.MainDocumentPart.Uri, @"/w:document[1]/w:body[1]/w:p[2]/w:r[2]", new NS("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")); } }
public void GetXPathTest() { XmlPath actual; Paragraph p = new Paragraph(new Run(), new BookmarkEnd(), new Run()); Body body = new Body(p); actual = XmlPath.GetXPath(p); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[1]", actual.XPath); Assert.Equal(1, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); actual = XmlPath.GetXPath(p.FirstChild); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[1]/w:r[1]", actual.XPath); Assert.Equal(1, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); body.PrependChild(new Paragraph()); actual = XmlPath.GetXPath(p.FirstChild.NextSibling()); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[2]/w:bookmarkEnd[1]", actual.XPath); Assert.Equal(1, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); var r = p.LastChild; actual = XmlPath.GetXPath(r); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[2]/w:r[2]", actual.XPath); Assert.Equal(1, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); var unknown1 = p.PrependChild(new OpenXmlUnknownElement("my:test", "http://my")); actual = XmlPath.GetXPath(unknown1); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[2]/my:test[1]", actual.XPath); Assert.Equal(2, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); Assert.Equal(@"xmlns:my=""http://my""", actual.NamespacesDefinitions[1]); var unknown2 = p.AppendChild(new OpenXmlUnknownElement("my:test", "http://my")); actual = XmlPath.GetXPath(unknown2); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[2]/my:test[2]", actual.XPath); Assert.Equal(2, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); Assert.Equal(@"xmlns:my=""http://my""", actual.NamespacesDefinitions[1]); var miscNode = p.AppendChild(new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, "<!-- comment -->")); actual = XmlPath.GetXPath(miscNode); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[2]/<!-- comment -->", actual.XPath); Assert.Equal(1, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); actual = XmlPath.GetXPath(r); Assert.Null(actual.PartUri); Assert.Equal(@"/w:body[1]/w:p[2]/w:r[2]", actual.XPath); Assert.Equal(1, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); using (var stream = new MemoryStream()) using (var doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document)) { doc.AddMainDocumentPart(); doc.MainDocumentPart.Document = new Document(body); actual = XmlPath.GetXPath(r); Assert.Equal(doc.MainDocumentPart.Uri, actual.PartUri); Assert.Equal(new Uri("/word/document.xml", UriKind.Relative), actual.PartUri); Assert.Equal(@"/w:document[1]/w:body[1]/w:p[2]/w:r[2]", actual.XPath); Assert.Equal(1, actual.NamespacesDefinitions.Count); Assert.Equal(@"xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""", actual.NamespacesDefinitions[0]); } }