Java XPath Prefix must resolve to a namespace - xpath

I'm having difficulty getting my XPath expression to work with Java. I am using XPath 1.0.
If I use https://www.freeformatter.com/xpath-tester.html the following works:
/*:configuration/*:connectionStrings/*:add[#*:name= 'Data.Server.ConnectionString']/#name
When I try the same in my java program, it gives me:
com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace:
I tried replacing the : with local-name() since I'm using XPath 1.0 but had no luck.
Here's the source document
<?xml version="1.0" encoding="UTF-8" standalone="no"?><configuration>
<configSections>
<sectionGroup name="telerik.web.ui">
<section allowDefinition="MachineToApplication" name="radCompression" requirePermission="false" type="Telerik.Web.UI.RadCompressionConfigurationSection, Telerik.Web.UI, PublicKeyToken=121fae78165ba3d4"/>
</sectionGroup>
</configSections>
<telerik.web.ui>
<radCompression enablePostbackCompression="true"/>
</telerik.web.ui>
<appSettings>
<add key="InfrastructureConfig.Server" value="[App.BaseDirectory]\infrastructure.config"/>
<add key="InfrastructureConfig.Debug" value="[App.BaseDirectory]\Infrastructure.config"/>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
<add key="Telerik.ScriptManager.TelerikCdn" value="Disabled"/>
<add key="Telerik.StyleSheetManager.TelerikCdn" value="Disabled"/>
<add key="vs:EnableBrowserLink" value="false"/>
</appSettings>
<connectionStrings>
<add connectionString="data source=[DB.Instance.Name];persist security info=True;user id=[DB.UserName];password=[DB.Password]" name="Data.Server.ConnectionString"/>
<add connectionString="data source=[DB.Instance.Name];persist security info=True;user id=[DB.UserName];password=[DB.Password]" name="Data.Debug.ConnectionString"/>
</connectionStrings>
</configuration>
I need to return connectionString and name.
Any ideas?
Thanks!

The syntax *:xxx requires XPath 2.0+. Upgrade to an XPath 2.0 processor (there are several available in the Java world), or if you must, use the workaround *[local-name()='xxx'].

One shortest (but less secure) way to grab this could be :
//*[#connectionString and #name]/#*
If you face an error, use :
//*[#connectionstring and #name]/#*

Related

Transform app.config entries only if a flag in app.debug.config is true

I would like to perform an xdt:Transform when in Debug configuration but only if the value of an entry in app.debug.config is a certain value, lets say true to keep it simple. For example:
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Value.First" value="foo" />
<add key="Value.Second" value="foo" />
<add key="Value.Third" value="foo" />
</appSettings>
</configuration>
App.Debug.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<!--Convenient flag to indicate if transform should happen-->
<add key="Perform.Transform.If.True" value="true" xdt:Transform="Insert" />
<!--Should only happen if the above is true-->
<add key="Value.First" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key="Value.Second" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
<add key="Value.Third" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
I would like all of the Value.* entries in app.config to be transformed only if the key Perform.Transform.If.True is set to true. If it is false nothing should happen. The reason being that sometimes during testing we would like to quickly turn things on and off which are controlled by the config files.
I have seen the options for Locator for things like Match, Conditional, XPath, etc. but none seem to allow for a condition from another entry. Can that be done with slowcheetah/xdt transforms?
Well, found a round-about way using XPath:
<add
key="Value.First"
value="bar"
xdt:Transform="Replace"
xdt:Locator="XPath(//appSettings/add[#key='Perform.Transform.If.True' and translate(#value, 'ERTU', 'ertu')='true']/../add[#key='Value.First'])"
/>
If the flag is not true it will fail to resolve the path. The translate makes it case-insensitive.
Failing to resolve (is false) leads to compile warnings which is annoying but since this is a debug tool we can live with that.

How would I remove Elmah modules from web.config using XML-Document-Transform?

In other words, in the following web.config xml, I want to remove all elements with a type attribute that starts with 'Elmah.'
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>
I have tried several combinations and structures of the basic transform, with various errors,
<add xdt:Locator="XPath([starts-with(#type,'Elmah.')" xdt:Transform="Remove"/>
before giving up and just removing the whole httpModules element, because no XPath is needed for that.
The XPath Locator expects a fully qualified XPath location, so your transform is not currently matching any elements. If you use the Condition Locator (which expects a relative XPath) instead, it should match:
<add xdt:Locator="Condition(starts-with(#type,'Elmah.')" xdt:Transform="RemoveAll"/>
Note also that the xdt:Transform Remove will only operate on the first matched element, so you need to use RemoveAll to achieve what you want.
The summary on msdn covers this quite well.
Have you tried removing each module individually?
<add name="ErrorLog" xdt:Locator="Match(name)" xdt:Transform="Remove"/>
<add name="ErrorMail" xdt:Locator="Match(name)" xdt:Transform="Remove"/>
<add name="ErrorFilter" xdt:Locator="Match(name)" xdt:Transform="Remove"/>

Why am I getting a "No attribute 'name' exists for the Match Locator" error when publishing?

I am so confused on this. I want to use the SQL Server on my desktop when I am developing and using the live SQL Server when I publish my project. I am playing with the transformation stuff in Visual Studio 2010.
I get the "No attribute 'name' exists for the Match Locator" when I try to publish my project.
My Web.config file contains:
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=db" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<sessionState mode="SQLServer" sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=eGov" timeout="20" allowCustomSqlDatabase="true" />
</system.web>
I am still testing it, so for now, my Web.Release.config file contains:
<connectionStrings>
<add name="EFDbContext"
connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=db"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<sessionState mode="SQLServer"
sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=app"
timeout="20" allowCustomSqlDatabase="true"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</system.web>
Anything I am seeing online is only confusing me more. Any help to get me up and running?
xdt:Locator="Match(name) mean that system will match nodes to replace using name tag. If you don't have name attribute then it fails. You must have some unique attribute to use this type of transformation.
Doh! The problem was in the sessionState section. It should be:
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=app"
timeout="20" allowCustomSqlDatabase="true"
xdt:Transform="SetAttributes" xdt:Locator="XPath(configuration/system.web/sessionState)" />
</system.web>
Using "name" in Match(name) is for a typical config setting like the following. The key in this case is "name".
<add name="errorAddress" email="me#google.com" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
If the key in your setting is something else, that is what you need to use:
<add token="UserToken" value="23jkl2klk2j3kja9d8f" xdt:Transform="SetAttributes" xdt:Locator="Match(token)"/>

Config Transform does not work?

I have this config in my Web.config file:
<connectionStrings>
<add name="LocalConnString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NCU.Joberwocky.Db.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
This in my Web.Release.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="LocalConnString" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=192.168.10.3;Initial Catalog=AeorionJobs;User Id=aeorionjobs;Password=aejobs3695;MultipleActiveResultSets=True""
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
When I switch the build to release, it still uses the config from Web.config. Any idea what the problem is?
I am also struggling with configuration transforms. Actually I am trying to have a different configuration on my environment than the common one uploaded in code control.
I have been also trying the same thing as you are doing and I would say those transforms are only applied on the deployment packages that you can build.
Hope it helps.

VS2010 Build Deployment Package web.release.config transform error

I am getting the transformed web.config in the deployment package incorrectly transformed.
The result is as follows (notice how the connection string gets generated):
<connectionStrings>
<add name="xxxConnectionStringNamexxx"
connectionString="$(ReplacableToken_xxxConnectionStringNamexxx-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Config:
<connectionStrings>
<add name="xxxConnectionStringNamexxx"
connectionString="Data Source=.\sqlexpress2005;Initial Catalog=xxxx;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Web.Release.config:
<connectionStrings>
<add name="xxxConnectionStringNamexxx" connectionString="Data Source=.\sqlexpress2005;Initial Catalog=xxxx;Integrated Security=True" providerName="System.Data.SqlClient" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)"/>
</connectionStrings>
Any ideas?
Thanks
So benoit found an answer on the asp.net forums : ReplacableToken_ when using web.config transform? .
I deployed to a folder on my desktop and the web.config file was transformed correctly.

Resources