.netxmlxdt-transform

Is it possible to mark an XML node as 'must be transformed'?


Is it possible to mark a node within an XML file (such as a config file) as 'must be transformed' and for the transformation to fail if you don't specify it within your transformation file?

For example, take the following made up example of a .config file with a node that must be transformed:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <!-- Mark this key to be transformed -->
    <add key="MyValue" MustBeTransformed="true" />
  </appSettings>
    <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>

</configuration>

Because the key value is marked as MustBeTransformed, the following would ensure that it is transformed correctly:

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <!-- Without the line below, the transform would fail -->
    <add key="MyValue" xdt:Transform="Set a value" />
  </appSettings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
</configuration>

I ask this because at the moment I have only seen this to be possible using .ps1 scripts and XPath


Solution

  • If you want to mark a node as "must be transformed" and the only data you have around is the XML tree, then you'll have to modify the XML with a mark.

    You can do this by adding a reserved attribute MustBeTransformed to a node, or by wrapping the node in a reserved tag ... . You'll need to apply your transforms, and then run a check to see if any of these reserved attributes/tags are still present.

    If you don't want to mark the XML itself, your "mustbetransformed" signal must by definition be outside the XML; now you need some way to point. You could write down a set of XPATH expressions to "point" to nodes that need transforming; this is fragile because transformations can move around XML subtrees and the XPATHs may become invalid unless they are adjusted too, and that's awkward.