private XDocument CopyAncestorsAndDescendantsOf(XDocument document, XElement element)
 {
     var xpath = new AbsoluteXPathWriter().Write(element);
     var elementsToKeep = GetElementsToKeep(document, xpath);
     var allElements = document.Root.DescendantsAndSelf().ToArray();
     foreach(var copy in allElements)
     {
         if(!elementsToKeep.Contains(copy))
         {
             copy.Remove();
         }
     }
     return document;
 }
        public void WriteOutputIsValid(string xml, string expectedXPath)
        {
            // Arrange
            var includeAllAttributesExceptNamespaceDeclarations = Substitute.For<IAttributeFilter>();
            includeAllAttributesExceptNamespaceDeclarations.IsIncluded(Arg.Any<XAttribute>()).Returns(info => !info.Arg<XAttribute>().IsNamespaceDeclaration);
            var filters = new[] {includeAllAttributesExceptNamespaceDeclarations};
            var testNode = xml.SelectSingleNode(expectedXPath);

            // Act
            var actualXPath = new AbsoluteXPathWriter(filters).Write(testNode);

            // Assert
            Assert.That(actualXPath, Is.EqualTo(expectedXPath));
        }