FatAntelope icon indicating copy to clipboard operation
FatAntelope copied to clipboard

Microsoft.Web.XmlTransform.XmlNodeException: No element in the source document matches

Open chucklu opened this issue 6 years ago • 8 comments

Hi, I have two xml files base.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <appSettings>
    <add key="test20180421" value="1" />
     <add key="test20180422" value="2" />
  </appSettings>
</configuration>

target.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appSettings>
      <add key="test20180421" value="1" />
     <add key="test20180423" value="3" />
     <add key="test20180422" value="2" />
  </appSettings>
</configuration>

I got the patch.xml as following

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" xdt:Transform="RemoveAttributes(xmlns)" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="test20180423" value="3" xdt:Transform="InsertBefore(/configuration/appSettings/add[(@key='test20180422')])" />
  </appSettings>
</configuration>

when I try to apply the source, I got the exception

Microsoft.Web.XmlTransform.XmlNodeException: No element in the source document matches '/configuration/appSettings/add[(@key='test20180422')]' ---> Microsoft.Web.XmlTransform.XmlTransformationException: No element in the source document matches '/configuration/appSettings/add[(@key='test20180422')]' at Microsoft.Web.XmlTransform.InsertBase.get_SiblingElement() at Microsoft.Web.XmlTransform.InsertBefore.Apply() at Microsoft.Web.XmlTransform.Transform.ApplyOnce() at Microsoft.Web.XmlTransform.Transform.Execute(XmlElementContext context, String argumentString) --- End of inner exception stack trace --- at Microsoft.Web.XmlTransform.XmlTransformationLogger.LogErrorFromException(Exception ex) at Microsoft.Web.XmlTransform.XmlTransformation.Apply(XmlDocument xmlTarget) at FatAntelope.CommandLine.Program.Main(String[] args) in E:\GitHub\Other\FatAntelope\FatAntelope.CommandLine\Program.cs:line 104 Microsoft.Web.XmlTransform.XmlTransformationException: No element in the source document matches '/configuration/appSettings/add[(@key='test20180422')]' at Microsoft.Web.XmlTransform.InsertBase.get_SiblingElement() at Microsoft.Web.XmlTransform.InsertBefore.Apply() at Microsoft.Web.XmlTransform.Transform.ApplyOnce() at Microsoft.Web.XmlTransform.Transform.Execute(XmlElementContext context, String argumentString)

chucklu avatar Apr 23 '18 10:04 chucklu

when I remove the namespace from base.xml ,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appSettings>
    <add key="test20180421" value="1" />
     <add key="test20180422" value="2" />
  </appSettings>
</configuration>

then the patch generated as following

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="test20180423" value="3" xdt:Transform="InsertBefore(/configuration/appSettings/add[(@key='test20180422')])" />
  </appSettings>
</configuration>

and there is no problem to apply source.

I am not sure if this is a bug in this repository or just bug from Microsoft.Web.Xdt. I have opened an issue here.

chucklu avatar Apr 23 '18 10:04 chucklu

Hi @chucklu

Unfortunately, I wasn't able to reproduce the error you described. It could be related to the version of Microsoft.Web.Xdt being used.

I did however get a different error when I pasted your example into https://fatantelopetester.apphb.com to generate a patch. The error was related to the XML namespace on the node in base.xml: xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"

I removed the namespace and generated a patch, and then applied the patch using the FatAntelope.exe command line tool, and it correctly applied the patch.

So I suggest removing the XML namespace and trying again as it might be reason for the issue.

Cheers, Cameron

CameronWills avatar May 07 '18 13:05 CameronWills

Hi @CameronWills , The version of Microsoft.Web.Xdt I used is latest version 2.1.2. I also tried the version you used 2.1.1 . Both of them have the same problem.

The workaround about remove the namespace I have found that, but that's weird.

What's the different error you got?

Actually I can encounter the same exception everytime. When you try with the provide base.xml and target.xml, could you paste the patch.xml generated?

chucklu avatar May 08 '18 01:05 chucklu

I know this is quite old. I've picked up this and I'm encountering the same problem. Though in my case it's No element in the source document matches '/configuration/runtime/assemblyBinding/dependentAssembly[4]'

I'm not sure why it's occurring since XPath supports this syntax. Changing the index to a lower or higher number doesn't change the error. Updated the XDT nuget to 3.1.1 but no luck there either.

To get around this, instead of calling "InsertAfter(xyz)", I changed it to just "Insert". This solves my problem but means that I have to manually check my transformations to avoid this occurring during deployments.

agility-matth avatar Jul 07 '20 00:07 agility-matth

@agility-matth Finally I give up to upgrade the config file through the library, because there are some custom setting in client's config file. I just upgrade it by linq to xml, operate the xml node one by one programmatically.

chucklu avatar Jul 07 '20 05:07 chucklu

That sounds like an interesting idea. I’ll see if I can do a similar approach since our situations are similar. Thanks!

agility-matth avatar Jul 07 '20 06:07 agility-matth

@agility-matth are you able to provide the source & target files? Or at least just the /configuration/runtime/assemblyBinding sections of both files? Obviously remove anything sensitive from the files. Thanks

CameronWills avatar Jul 10 '20 01:07 CameronWills

Sure thing @CameronWills , I'll see what I can do early next week and send it over.

agility-matth avatar Jul 10 '20 01:07 agility-matth