本帖最后由 uhjjhjff11d 于 2019-11-14 17:34 编辑
回复 33# WHY
还是那句话:能够解决问题的方法,不喜欢你可以不用,甚至不相信它,但不要轻易指责它。
回复 32# WHY
拿不出完美的、比正则更好的解决方法,又极力指责“滥用正则”、“邪道”、“错误的技术”
用庫修改所有 type 節點的範例,大家自行比對哪一個好讀好改。- $xmlfile = 'C:\Users\username\Documents\sample.xml'
- [XML]$xmlcontent = Get-Content $xmlfile
-
- function hash($string) {
- new-object System.Security.Cryptography.SHA256Managed | ForEach-Object { $_.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($string)) } | ForEach-Object { $all += $_.ToString("x2") }
- "varhead" + $all
- }
-
- $xmlcontent.SelectNodes("//*") | ForEach-Object {
- if (-not ($_.Type -eq $null)){
- $_.Type = hash($_.Type)
- } else {
- Write-Output "pass"
- }
- }
-
- $xmlcontent.Save(".\sample2.xml")
复制代码 xml 範例(來自網絡)- <?xml version="1.0"?>
- <PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
- <Address Type="Shipping">
- <Name>Ellen Adams</Name>
- <Street>123 Maple Street</Street>
- <City>Mill Valley</City>
- <State>CA</State>
- <Zip>10999</Zip>
- <Country>USA</Country>
- </Address>
- <Address Type="Billing">
- <Name>Tai Yee</Name>
- <Street>8 Oak Avenue</Street>
- <City>Old Town</City>
- <State>PA</State>
- <Zip>95819</Zip>
- <Country>USA</Country>
- </Address>
- <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>
- <Items>
- <Item PartNumber="872-AA">
- <ProductName>Lawnmower</ProductName>
- <Quantity>1</Quantity>
- <USPrice>148.95</USPrice>
- <Comment>Confirm this is electric</Comment>
- </Item>
- <Item PartNumber="926-AA">
- <ProductName>Baby Monitor</ProductName>
- <Quantity>2</Quantity>
- <USPrice>39.98</USPrice>
- <ShipDate>1999-05-21</ShipDate>
- </Item>
- </Items>
- </PurchaseOrder>
复制代码
[url=http://bbs.bathome.net/redirect.php?goto=findpost&ptid=52856&pid=220490]60樓的代碼复制代码 那個帖子最後一個版本的代碼- $global:n = 0; $Hash=@{};
- $str = [IO.File]::ReadAllText('manifest.xml',[Text.Encoding]::Default);
-
- #修改name属性的值,如果name属性值以music_prev或music_display或music_next等开头,或者节点名为Extra,则不修改
- $reg1 = '(?<=<(?!Extra)[^<>]*\sname=")(?!notice)(?!music_(?:prev|display|next|play|pause|album_cover))(?!notification_(?:icon|title|content|time|info|subtext|key|list))[^"]+(?=")';
-
- #如果command与target同时出现,command="play",则修改target属性值,
- $reg2 = '(?<=\starget=")[^"]+(?="[^<>]*\scommand="play)|(?<=\scommand="play[^<>]*\starget=") [^"]+(?=")';
-
- $str = [regex]::Replace($str, $reg1 + '|' + $reg2, {
- param($m);
- $key = $m.Value;
- if( !$Hash.ContainsKey($key) ){
- $x = [Math]::floor( $global:n / 26 );
- if($x) { $chr1 = [char]( 64 + [int]$x ) } else { $chr1 = '' }
- $Hash[$key] = $chr1 + [char]( 65 + $global:n++ % 26 );
- }
- $Hash[$key];
- })
-
- $arrKey = $Hash.Keys | sort -Desc {$_.Length};
-
- #如果属性值包含@#字符,或者属性值以 .animation或.visibility 结尾,则修改
- $str = [regex]::Replace($str, '(?<=")([^"@#]*[@#][^"]+|[^"]+(?:\.animation|\.visibility))(?=")', {
- param($m);
- $s = $m.Groups[1].Value;
- forEach( $key In $arrKey ) {
- $s = $s.Replace( '@' + $Key, '@' + $Hash[$Key] );
- $s = $s.Replace( '#' + $Key, '#' + $Hash[$Key] );
- $s = $s.Replace( $Key + '.animation', $Hash[$Key] + '.animation' );
- $s = $s.Replace( $Key + '.visibility', $Hash[$Key] + '.visibility' );
- }
- $s;
- })
-
- [IO.File]::WriteAllText('manifest2.xml', $str, [Text.Encoding]::UTF8);
-
- $str = [IO.File]::ReadAllText('config.xml', [Text.Encoding]::Default);
- $str = [regex]::Replace($str, '(?<=\sid=")[^"]+(?=")', {param($m); if($Hash[$m.Value]){$Hash[$m.Value]}else{$m.Value}});
- [IO.File]::WriteAllText('config2.xml', $str, [Text.Encoding]::UTF8);
-
- echo 'Done';
- [Console]::ReadKey()
复制代码
|