ItsNotMyStrongPoint
ItsNotMyStrongPoint

Reputation: 137

Case insensitve xpath 1.0 in query string

This is a followup to Powershell update xml key value based on other key value

I have xml in the following format:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <plugins>
  <plugin pluginType="plugin1" polling_internal="2" polling_unit="M" enabled="true">
      <params>
        <add key="Server" value="server1.local" />
        <add key="database" value="DataBaseName1" />
      </params>
    </plugin>
  <plugin pluginType="plugin2" polling_internal="2" polling_unit="M" enabled="true">
      <params>
        <add key="Server" value="server2.local" />
        <add key="database" value="databasename2" />
      </params>
    </plugin>
 </plugins>
</configuration>

In the original posting, I wanted to search the 'database' key and update the value of the Server key, eg search for databasename1 and update the server value of server1.local to something else.

The original solution xpath was

"/configuration/plugins/plugin/params[add[@value='databasename1' and @key ='database']]/add[@key='Server']/@value"

I have since discovered that some of the database names are in mixed case, and xpath is sensitive so its not picking up some of the database names.

As this is xpath 1.0, I know I have to use the translate function, but I can't figure out from other examples here on where I have to insert it.

As this is for Powershell, using an external Powershell function to do the conversion is fine, as lone as it doesn't modify the case of the entire file, only the search value.

Thanks!

Upvotes: 1

Views: 200

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

So, only add[@key='database']/@value is mixed case sometimes? This is how you can use the translate() function:

"/configuration/plugins/plugin/params[add[translate(@value, 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz') ='databasename1' and @key ='database']]/add[@key='Server']/@value"

This lowercases any uppercased letter in the string value of the value attribute. That is, it does not manipulate the XML data in any way, the translation is only applied in the path expression.

Upvotes: 2

Related Questions