0
votes

I have XML content which I need to remove whole line from a particular element from a node.

XML File:

need remove whole line.

  <Host name="login.net">
    <Path name="Block">
      <Path name="webservices/login" authType="qwerty" requireSession="true" />
      <Path name="login" authType="qwerty" requireSession="true" />
      <Path name="weblogin" requireSession="true" authType="shibboleth" />
    </Path>
    <Path name="retailpdf" applicationId="retailpdf" authType="qwerty" requireSession="true" requireSessionWith="retailpdf" />
    <Path name="managemyaccount" applicationId="managemyaccount" authType="qwerty" requireSession="true" requireSessionWith="managemyaccount" />
    <Path name="approve" applicationId="approve" authType="qwerty" requireSession="true" requireSessionWith="approve" />
    <Path name="latesignin" applicationId="latesignin" authType="qwerty" requireSession="true" requireSessionWith="latesignin" />
    <Path name="fblogin" applicationId="fblogin" authType="qwerty" requireSession="true" requireSessionWith="fblogin" />
  </Host>

</RequestMap>

# need to remove complete node of id retailpdf

Tried to remove the lines through powershell script

$XMLfilepath = 'C:\Test\MYfile.xml' 
psedit $XMLfilepath
$Pathxml = [xml] (Get-Content $XMLfilepath)

$nodes = $xml.SelectNodes("/RequestMapper/Path");

{Remove-Item "<Path name="fblogin" applicationId="fblogin" authType="shibboleth" requireSession="true" requireSessionWith="fblogin" />"}

$XMLfilepathNew = 'C:\Test\Myfiledone.xml' 
$Pathxml.Save($XMLfilepathNew)
psedit $XMLfilepathNew

Need to remove mentioned line and node in XML content like below:

Expected output:

  <Host name="login.net">
    <Path name="Block">
      <Path name="webservices/login" authType="qwerty" requireSession="true" />
      <Path name="login" authType="qwerty" requireSession="true" />
      <Path name="weblogin" requireSession="true" authType="shibboleth" />
    </Path>

    <Path name="managemyaccount" applicationId="managemyaccount" authType="qwerty" requireSession="true" requireSessionWith="managemyaccount" />
    <Path name="approve" applicationId="approve" authType="qwerty" requireSession="true" requireSessionWith="approve" />
    <Path name="latesignin" applicationId="latesignin" authType="qwerty" requireSession="true" requireSessionWith="latesignin" />
    <Path name="fblogin" applicationId="fblogin" authType="qwerty" requireSession="true" requireSessionWith="fblogin" />
  </Host>

</RequestMap>

 <ApplicationOverride id="retailpdf" entityID="https://login.tcxqa.hrblock.net/retailpdf/shibboleth">
  <Sessions lifetime="60" timeout="20" checkAddress="false" consistentAddress="false" handlerURL="/retailpdf/Shibboleth.sso" handlerSSL="true">
    <SessionInitiator type="Chaining" Location="/retailpdf" isDefault="false" id="retailpdf" forceAuthn="true" entityID="https://qaidp.hrblock.net/idp/shibboleth" authnContextClassRef="urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport">
      <SessionInitiator type="SAML2" template="bindingTemplate.html" />
    </SessionInitiator>
  </Sessions>
2
# need remove <Path name="retailpdf" applicationId="retailpdf" authType="qwerty" requireSession="true" requireSessionWith="retailpdf" /> whole line. - user11876319
# need to remove complete node of id retailpdf - user11876319

2 Answers

0
votes

Have you tried something like this:

$original = Get-Content 'C:\Test\MYfile.xml'
$new = $original.replace('<Path name="retailpdf" applicationId="retailpdf" authType="qwerty" requireSession="true" requireSessionWith="retailpdf" />', '')
$new | Set-Content 'C:\Test\MYfile.xml'
0
votes

If you can create an XML file that is not broken, then you should be able to just remove the unwanted nodes using methods from the XmlDocument class.

$XMLfilepath = 'C:\Test\MYfile.xml' 
$Pathxml = [xml](Get-Content $XMLfilepath)
$nodeToRemove = $Pathxml.SelectSingleNode("//Host/Path[@name = 'retailpdf']")
$nodeToRemove.ParentNode.RemoveChild($nodeToRemove)
$nodeToRemove = $Pathxml.SelectSingleNode("//ApplicationOverride[@id = 'retailpdf']")
$nodeToRemove.ParentNode.RemoveChild($nodeToRemove)
$XMLfilepathNew = 'C:\Test\Myfiledone.xml' 
$Pathxml.Save($XMLfilepathNew)

Note that you can use wildcards in your XPaths. That would help in removing all attributes that contain specific values more concisely. However, you do need to understand the context levels of your XML data.