During my PDC talk I had shown some of the snippets that can come handy while using Web.Config Transformations… This post is intended to share the XDT snippets and instructions on how to install them to use them with VS 2010…
- Download XDT zip file from my SkyDrive
- Unzip the files and copy the inner XDT folder into %Program Files%\Microsoft Visual Studio 10.0\Xml\1033\Snippets
- After this the XDT snippets will be available for you to use within any XML file within VS 2010…
To know how to use XDTs for Web.Config file please check out the post about Web.Config Transformations
Few interesting points about Transforms & Locators which are worth noting to do pretty powerful stuff with your XML are:
- Transforms - Transforms act on a XML node i.e. from XDT engine standpoint the node on which a transform is found is plucked out of the document and passed as a node to the Transform itself (e.g. “SetAttributes” Transform)… The Transform class internally implements the logic of modifying the node with its own special logic (i.e. setting attributes in case of “SetAttributes” Transform) and returns back the node to the engine… The XDT engine then replaces the new node into the document… The engine also passes the handle of the parent node to the transform which the transform can then play around with (e.g. “Insert” Transform receives a new node as well as the parent node and has the DOM code of inserting the new node under the parent…)
- Locators – XDT usually traverses through the XML document and constructs the XPath all along e.g. when it hits the add node under connectionStrings in web.config it has already constructed the XPath (/configuration/connectionString/add)… Locators help with narrowing down the XPath so that the correct node can be picked up… Check out the example below:
<connectionStrings>
<add name="1stDB"
connectionString="Data Source=Server1;Initial Catalog=DB1;Integrated Security=True"
xdt:Transform="SetAttributes" />
<add name="2ndDB"
connectionString="Data Source=Server2;Initial Catalog=DB2;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Based on the standard logic of XPath traversing XDT will always pick up the first “add” node i.e. one with 1stDB in the example below… That may not be always desirable… In XDT we could have implemented the logic to do special things for appSettings & connectionStrings but then that would have made XDT specific to web.config instead we implemented Locators which now allows XDT to be general purposes XML transformation engine… In the above example if we use a Locator called xdt:Locator=”Match(name)” on the 2ndDB then as soon as the XDT engine encouters a locator it calls the class implementing it (i.e. “Match” and allows it to play with the XPath that is being generated… In this case the match locator will help construct the XPath as /configuration/connectionString/add[@name=’2ndDB’]
The high level takeaway here is that when you play around with the snippets for XDT keep the above concepts for Transforms & Locators in mind and with the quick snippet explanations of each transforms and locators you should be able to very easily use all of them… If you encounter any issues plz feel free to reach out…
Thanks!!
-Vishal