Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chucklu opened this issue Apr 23, 2018 · 8 comments

Comments

@chucklu
Copy link

chucklu commented Apr 23, 2018

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
Copy link
Author

chucklu commented Apr 23, 2018

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.

@CameronWills
Copy link
Owner

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

@chucklu
Copy link
Author

chucklu commented May 8, 2018

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?

@agility-matth
Copy link

agility-matth commented Jul 7, 2020

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.

@chucklu
Copy link
Author

chucklu commented Jul 7, 2020

@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.

@agility-matth
Copy link

agility-matth commented Jul 7, 2020 via email

@CameronWills
Copy link
Owner

@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

@agility-matth
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants