public void TestInputDocumentsWithXmlNamespacesWorkAsExpected() { var input = XDocument.Parse(@" <configuration> <appSettings> <add key=""key1"" value=""value1"" /> </appSettings> <blah xmlns=""http://test.com""> <add key=""key2"" value=""value2"" /> </blah> <flop xmlns=""http://test.com""> <add key=""key3"" value=""value3"" xmlns="""" /> </flop> </configuration> "); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <appSettings> <add value=""value1-new"" xdt:Transform=""SetAttributes"" /> </appSettings> <blah xmlns=""http://test.com""> <add key=""key2"" value=""value2-new"" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" /> </blah> <flop xmlns=""http://test.com""> <add key=""key3"" value=""value3-new"" xmlns="""" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" /> </flop> </configuration> "); var output = new XdtTransformer().Transform(input, transform); XNamespace ns = "http://test.com"; var element2 = output .Element("configuration") .Elements(ns + "blah") .Elements(ns + "add") .Single(e => e.Attribute("key").Value == "key2"); Assert.IsTrue(element2.Name.NamespaceName == ns); Assert.IsTrue(element2.Attribute("value").Value == "value2-new"); var element3 = output .Element("configuration") .Elements(ns + "flop") .Elements("add") .Single(e => e.Attribute("key").Value == "key3"); Assert.IsTrue(element3.Name.NamespaceName == String.Empty); Assert.IsTrue(element3.Attribute("value").Value == "value3-new"); }
public void TestSetAttributesTransform() { // change the "debug" attribute to false var input = GetInputDocument(); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <system.web> <compilation debug=""false"" xdt:Transform=""SetAttributes"" /> </system.web> </configuration> "); var output = new XdtTransformer().Transform(input, transform); var element = output .Element("configuration") .Element("system.web") .Element("compilation"); Assert.IsTrue(element.Attribute("debug").Value == "false"); // ensure we haven't accidentally added attributes from the transform document Assert.IsFalse(output.Descendants().Any(e => e.Attributes().Any(a => a.Name.NamespaceName == Namespaces.Xdt))); }
public void TestMultipleElementsAreTransformed() { var input = GetInputDocument(); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <appSettings> <add value=""ha"" xdt:Transform=""SetAttributes"" /> </appSettings> </configuration> "); var output = new XdtTransformer().Transform(input, transform); var settings = output .Element("configuration") .Element("appSettings") .Elements("add"); Assert.IsTrue(settings.Count() == 3); Assert.IsTrue(settings.SelectMany(e => e.Attributes("value")).All(a => a.Value == "ha")); }
public void TestRemoveAttributesTransform() { // remove the "debug" attribute var input = GetInputDocument(); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <system.web> <compilation xdt:Transform=""RemoveAttributes(debug)"" /> </system.web> </configuration> "); var output = new XdtTransformer().Transform(input, transform); var element = output .Element("configuration") .Element("system.web") .Element("compilation"); Assert.IsTrue(element.Attribute("debug") == null); }
public void TestConditionLocator() { var input = GetInputDocument(); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <appSettings> <add key=""key2"" value=""value2-live"" xdt:Locator=""Condition(@key='key2')"" xdt:Transform=""SetAttributes"" /> </appSettings> </configuration> "); var output = new XdtTransformer().Transform(input, transform); var settings = output .Element("configuration") .Element("appSettings") .Elements("add"); var setting1 = settings.Single(e => e.Attribute("key").Value == "key1"); var setting2 = settings.Single(e => e.Attribute("key").Value == "key2"); Assert.IsTrue(setting1.Attribute("value").Value == "value1"); Assert.IsTrue(setting2.Attribute("value").Value == "value2-live"); }
public void TestSetAttributesTransformWithLocator() { var input = GetInputDocument(); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <appSettings> <add key=""key2"" value=""value2-live"" xdt:Locator=""Match(key)"" xdt:Transform=""SetAttributes"" /> </appSettings> </configuration> "); var output = new XdtTransformer().Transform(input, transform); var settings = output .Element("configuration") .Element("appSettings") .Elements("add"); var setting1 = settings.Single(e => e.Attribute("key").Value == "key1"); var setting2 = settings.Single(e => e.Attribute("key").Value == "key2"); Assert.IsTrue(setting1.Attribute("value").Value == "value1"); Assert.IsTrue(setting2.Attribute("value").Value == "value2-live"); }
public void TestReplaceTransform() { // replace the entire system.web element var input = GetInputDocument(); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <system.web xdt:Transform=""Replace""> <extra content=""here"" /> </system.web> </configuration> "); var output = new XdtTransformer().Transform(input, transform); var element = output .Element("configuration") .Element("system.web") .Element("extra"); Assert.IsTrue(element.Attribute("content").Value == "here"); // ensure there are no xdt attributes from the transform doc! Assert.IsFalse(output.Descendants().Attributes().Any(a => a.Name.NamespaceName == Namespaces.Xdt)); }
public void TestSetAttributesTransformWithArguments() { // change the "debug" attribute to false var input = GetInputDocument(); var transform = XDocument.Parse(@" <configuration xmlns:xdt=""http://schemas.microsoft.com/XML-Document-Transform""> <system.web> <compilation debug=""false"" xdt:Transform=""SetAttributes(debug)"" /> </system.web> </configuration> "); var output = new XdtTransformer().Transform(input, transform); var element = output .Element("configuration") .Element("system.web") .Element("compilation"); Assert.IsTrue(element.Attribute("debug").Value == "false"); // ensure we haven't accidentally added attributes from the transform document Assert.IsFalse(output.Descendants().Any(e => e.Attributes().Any(a => a.Name.NamespaceName == Namespaces.Xdt))); }