user2628187
user2628187

Reputation: 307

Conditional replace in XML files

I'm replacing a text in XML files recursively using PowerShell. The script is working fine in replacing. However the XML files also have file paths which should not be replaced. This is the script currently being used

if ( $content -match ' web site | web-site ' ) {
    $content -replace ' web site ',' New Site ' -replace ' web-site ',' New Site ' |
        Out-File $file.FullName -Encoding utf8

For example if the XML file has

<title>web site</title>
<subtitle>web-site</subtitle>
<path>c:/web site/website.xml</path>

the expected output is should look like below. The matching text in file paths should be ignored. How can I add a condition to ignore the string if its between /web site/ or /web-site.xml?

<title>New Site</title>
<subtitle>New Site</subtitle>
<path>c:/web site/website.xml</path>

Upvotes: 2

Views: 1852

Answers (2)

mklement0
mklement0

Reputation: 437100

Here's a quick fix, but note that a more robust solution would use PowerShell's XML parsing features: see Ansgar Wiecher's helpful answer:

Note: This answer assumes that the strings of interest do not conflict with syntactical elements of the XML document, such as element names and attribute names (which happens to work for the specific strings in question), which illustrates why using a real XML parser is the better choice.

$content = @'
<doc>
<title>web site</title>
<subtitle>web-site</subtitle>
<path>c:/web site/website.xml</path>
</doc>
'@

$modifiedContent = $content -replace ''([^/])web[ -]site([^/])', '$1New Site$2'
# Replace 'web site' and 'web-site' if not preceded or followed by a '/'.
# Note: `web[ -]site` is the equivalent of `web site|web-site`

if ($modifiedContent -cne $content) { # If contents have changed, save.
  Out-File -InputObject $modifiedContent $file.FullName -Encoding utf8
}

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

It's usually far more efficient and far less error-prone to handle XML as XML. Select the nodes you want to update, then save the modified data back to a file.

$filename = 'C:\path\to\your.xml'

[xml]$xml = Get-Content $filename
$xml.SelectNodes('//*[self::title or self::subtitle]') |
    Where-Object { $_.'#text' -match 'web.site' } |
    ForEach-Object { $_.'#text' = 'New Site' }
$xml.Save($filename)

If you need to modify a substring of the node text you could do something like this:

$filename = 'C:\path\to\your.xml'

[xml]$xml = Get-Content $filename
$xml.SelectNodes('//*[self::title or self::subtitle]') |
    Where-Object { $_.'#text' -match 'web.site' } |
    ForEach-Object { $_.'#text' = $_.'#text' -replace 'web.site', 'New Site' }
$xml.Save($filename)

Upvotes: 4

Related Questions