Search code examples
xmltransformationxml-namespacesxdt-transform

How to transform element with XMLNS attribute


I am trying to transform child element of another element that contains xmlns attribute, but seems my transformations are ignored until I remove xmlns.

So let say I have:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          internalLogLevel="Trace"
          internalLogFile="NLogInternal.log"
          autoReload="true">
        <targets>
        </targets>
    </nlog>

And I am trying to get rid of targets element with:

<nlog>
    <targets xdt:Transform="Remove" />
</nlog>

But that doesn't seem to work, however if I remove xmlns and xmlns:xsi attributes transformation work as expected.

What I am doing wrong?


Solution

  • I don't know if this will work, but try the following:

    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
        <targets xdt:Transform="Remove" />
    </nlog>
    

    By placing the xmlns attribute on nlog, you are specifying that you are targeting the {http://www.nlog-project.org/schemas/NLog.xsd}nlog element, and the {http://www.nlog-project.org/schemas/NLog.xsd}targets element within it.

    Also, you may want to do some research on XML namespaces.