Namespace prefixes in Microsoft Xml Document Transformation's SetAttributes and RemoveAttributes transforms
Link for the Codeplex fork I mention in this article: https://xdt.codeplex.com/SourceControl/network/forks/LordZoltan/XDTEx
Get this package from Nuget using the package ID Microsoft.Web.XdtEx.
Microsoft recently open-sourced the XDT library that's at the heart of the web.config transformations. Partner this up with a new feature of Nuget 2.5 which supports the auto-importing of MSBuild .props and .targets files into a project file, and that means I could try something out with XML files in a Xamarin.Android project I'm working on at the moment.
I'm building an Android app (obviously) and due to our company's needs, we want to build a vanilla app which can then be re-used for other brands within the same group. I had the idea that I could create the whole thing as a nuget package, now that Nuget supports the 'MonoAndroid' platform.
Deploying code to projects with nuget is easy - and with the partial class model in C# it's simple to deploy a core platform to a project with one set of code files that never change (thus letting us manage them with nuget's install/upgrade workflow), and then adding extensibility points with partial methods (in addition to virtual methods etc).
However, I also needed to be able to do the same for android resource files. Consider the default main.axml from a new Xamarin.Android project:
Now, in my scenario, I'd want to deploy this core layout as content in my nuget package, but then I want people to be able to customise it without touching the original.
Let's say, for example, that I want to change the text on the <Button /> element there from '@string/Hello' (an Android resource identifier) to the literal string 'Hello World'. This is how we'd want to do it in XDT:
See the 'xdt:Transform="SetAttributes(android:text)"' attribute there? (If it worked in the base version) That would instruct XDT to replace the android:text attribute of our input file to "Hello World", because that's the content used here.
There is a slight snag, however: the current (as of 10th May 2013) version of XDT (available on codeplex, or, on nuget.org as a package) does not support namespace prefixes in xdt:Transform operations. It does support them in the xdt:Locator attribute, however.
Again, since XDT is now open-source (although not yet accepting pull requests), I decided to fork it and have a go at putting it in - and an hour or so later I had it working. You can see the diff here on codeplex: the primary changes are in XmlElementContext.cs, XmlTransform.cs and XmlAttributeTransform.cs, the rest are tests. Note that in the next commit I renamed the test from LocatorNamespaceTests.cs to NamespacePrefixTests.cs.
If you're currently using the XDT library (either directly, from a codeplex build, or via the official nuget package) you will be able to switch over to using this version without issues. If you're coming from an official release, then one thing to note is that this DLL is not signed - as is the case with the original codeplex release.
So what about the Android transform?
If you're wondering - yes I've managed to get the build integration working from a nuget package - and to have my files added to the project like this:
The xtransform is the file that is edited, the xbase is the file that contains the default content, and main.axml is generated by the build task before build (and before Xamarin.Android compiles it).
However, it's not been easy at all to do this - as it involves writing an install.ps1 to add the <DependentUpon /> metadata to the .axml.xbase and .axml files - which unfortunately requires a VS reload of the project in order to show the hierarchy in the IDE. I use the Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection to get the MSBuild project (I'm not including the powershell scripts because it detracts from the main point in this post) in order to be able to add the metadata more easily, but I then have to save the project through that as well in order to trigger a reload.
If you do a search around, you'll find the official line is to save a project in a nuget package install/uninstall script through the $project parameter that is passed to your script, as this is a 'friendly' save, which doesn't reload the environment (and therefore almost always what you want to do). The problem is that VS will only update the file hierarchy of dependent files when a project is reloaded - and there's no command in VS you can execute to perform this grouping (unless VSCommands is installed). Bah!
And then on uninstall/upgrade, the file hierarchy causes big issues, because if nuget removes a file that is a parent of others, then Visual Studio will automatically remove the children. This means that my initial hierarchical attempt - which was to have xbase -> xtransform -> output as the hierarchy - kept dropping both the xtransform and output files even through they'd changed - because they were deleted when the xbase was deleted.
Furthermore, flattening the hierarchy again in the uninstall.ps1 script didn't make any difference, because the underlying change is not reflected in the environment unless a project reload occurs.
I'm not really sure if there's any way around this - so I went for putting the editable file at the root instead.
UPDATE - This doesn't work either - because nuget doesn't then find the xbase file when it goes looking for it. I've had to write an Uninstall.ps1 script for the package that does nuget's job, finding the item and deleting it pseudo-manually.
What about a nuget package?
Yup - I've updated the post to include the package ID and nuget.org link - but here it is again for completeness: Microsoft.Web.XdtEx.