Search This Blog

Tuesday 10 September 2019

Powershell XML translate for case insensitive attribute value matching

Powershell XML translate to allow attribute values to be matched against values in different cases


 [xml]$exampleXML = @"  
 <servers>  
   <server name="AAA" />  
   <server name="BBB" />  
   <server name="CCC" />  
 </servers>  
 "@  
   
   
 # this will match 'AAA'  
 $exampleXML.SelectNodes("/servers/server[@name='AAA']")  
   
 # this will match not 'AAA', because it's looking for 'aaa'  
 $exampleXML.SelectNodes("/servers/server[@name='aaa']")  
   
 # this will match 'AAA' because it translates (i.e. lowercase in this example) the attribute value from 'AAA' to 'aaa' and then matches this with 'aaa'  
 $exampleXML.SelectNodes("/servers/server[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='aaa']")  
   

No comments:

Post a Comment