1

I'm really struggling to manipulate some XML in PowerShell that I need to send back to a web service as a body. Could anyone help me get the XML laid out the way it needs to be please?

<?xml version="1.0" encoding="UTF-8"?>
<EdgeGateway>
<Configuration>
    <GatewayInterfaces>
        <GatewayInterface>
            <InterfaceType>uplink</InterfaceType>
            <SubnetParticipation>
                <Gateway>1.2.3.4</Gateway>
                <Netmask>255.255.255.240</Netmask>
                <IpAddress>1.2.3.5</IpAddress>
# Missing the IpRange XML section - defined below
                <UseForDefaultRoute>true</UseForDefaultRoute>
            </SubnetParticipation>
            <UseForDefaultRoute>true</UseForDefaultRoute>
        </GatewayInterface>
    </GatewayInterfaces>
</Configuration>
</EdgeGateway>

needs to become:

<?xml version="1.0" encoding="UTF-8"?>
<EdgeGateway>
<Configuration>
    <GatewayInterfaces>
        <GatewayInterface>
            <InterfaceType>uplink</InterfaceType>
            <SubnetParticipation>
                <Gateway>1.2.3.4</Gateway>
                <Netmask>255.255.255.240</Netmask>
                <IpAddress>1.2.3.5</IpAddress>
# New Content added here
                <IpRanges>
                    <IpRange>
                        <StartAddress>1.2.3.5</StartAddress>
                        <EndAddress>1.2.3.5</EndAddress>
                    <IpRange>
                </IpRanges>
 # End of new content
                <UseForDefaultRoute>true</UseForDefaultRoute>
            </SubnetParticipation>
            <UseForDefaultRoute>true</UseForDefaultRoute>
        </GatewayInterface>
    </GatewayInterfaces>
</Configuration>
</EdgeGateway>

So far I've been able to create new XML Nodes/Elements for the new content, but I cannot get it to be inserted at the correct place. I can get the AppendChild() method to work, but it puts the content after the <UseForDefaultRoute> section - not before.

I've tried to do InsertBefore() and InsertAfter(), but it just doesn't want to work. Finally, when I do the AppendChild() approach, I also get a bit of extra text I wasn't expecting, something about xmlns?

<IpRanges xmlns=""><IpRange><StartAddress>1.2.3.5</StartAddress><EndAddress>1.2.3.5</EndAddress></IpRange></IpRanges>

This is what I managed to put together, bear in mind that it's broken :(

# load XML file
[xml]$doc = $response

# create node <StartAddress>
$startNode = $doc.CreateNode('element', 'StartAddress', '')
$start = $doc.CreateTextNode('1.2.3.5')
$startNode.AppendChild($start) | Out-Null

# create node <EndAddress>
$endNode = $doc.CreateNode('element', 'EndAddress', '')
$end = $doc.CreateTextNode('1.2.3.5')
$endNode.AppendChild($end) | Out-Null

# create node <IpRange> and append child nodes <StartAddress> and     <EndAddress>
$ipRange = $doc.CreateNode('element', 'IpRange', '')
$ipRange.AppendChild($startNode) | Out-Null
$ipRange.AppendChild($endNode) | Out-Null

# create node <IpRanges> and append child nodes <IpRange>
$ipRanges = $doc.CreateNode('element', 'IpRanges', '')
$ipRanges.AppendChild($ipRange) | Out-Null

# append node <IpRanges> to node <SubnetParticipation>
$subnetParticpation = $doc.EdgeGateway.Configuration.GatewayInterfaces.GatewayInterface[1].SubnetParticipation.AppendChild($ipRanges)

...following advice from Ansgar, this is my attempt at using a namespace. (broken)

[xml]$fragment = "<dummy xmlns:xsi='http://www.vmware.com/vcloud/v1.5'><IpRanges>$($ipRanges.InnerXml)</IpRanges></dummy>"

# $fragment.InnerXml ..returns..
# <dummy xmlns:xsi="http://www.vmware.com/vcloud/v1.5"><IpRanges><IpRange><StartAddress>185.39.247.98</StartAddress><EndAddress>185.39.247.98</EndAddress></IpRange></IpRanges></dummy>

# $body is the full XML Document I want to paste into
[xml]$xml = $body

$nsm = New-Object Xml.XmlNamespaceManager $xml.NameTable
$nsm.AddNamespace('xsi', $xml.NamespaceURI)
$node = $xml.ImportNode($fragment.DocumentElement.IpRanges, $true)

$subnetPart = $xml.SelectSingleNode("//IpAddress[text()='185.39.247.98']", $nsm)
$subnetPart

# returns nothing
4

2 回答 2

1

您可以在其他节点之后插入新节点,如下所示:

$node = $doc.SelectSingleNode("//IpAddress[text()='1.2.3.5']")
$node.ParentNode.InsertAfter($ipRanges, $node)
于 2017-02-17T10:50:15.413 回答
0

考虑一下XSLT,这是一种专用的、符合 W3C 的语言,旨在将 XML 文件转换为其他 XML、HTML 甚至文本格式。PowerShell 可以调用内置的XslCompiledTransform类,传入源文件、xslt 脚本和输出文件的参数。

XSLT (使用 Identity Transform 按原样复制并更新 SubnetParticipation 节点)

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>

  <!-- Identity Transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="SubnetParticipation">
    <xsl:copy>
      <xsl:apply-templates />
      <IpRanges>
        <IpRange>
          <StartAddress>1.2.3.5</StartAddress>
          <EndAddress>1.2.3.5</EndAddress>
        </IpRange>
      </IpRanges>
    </xsl:copy>
  </xsl:template>

</xsl:transform>

电源外壳

param ($xml, $xsl, $output)

if (-not $xml -or -not $xsl -or -not $output) {
    Write-Host "& .\xslt.ps1 [-xml] xml-input [-xsl] xsl-input [-output] transform-output"
    exit;
}

trap [Exception]{
    Write-Host $_.Exception;
}

$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;

$xslt.Load($xsl);
$xslt.Transform($xml, $output);

Write-Host "generated" $output;

命令行调用(或批处理脚本调用)

Powershell.exe -File "C:\Path\To\PowerShell\Script.ps1"
 "C:\Path\To\Source.xml" "C:\Path\To\Transform.xsl" "C:\Path\To\Output.xml"

输出

<?xml version="1.0" encoding="utf-8"?>
<EdgeGateway>
  <Configuration>
    <GatewayInterfaces>
      <GatewayInterface>
        <InterfaceType>uplink</InterfaceType>
        <SubnetParticipation>
          <Gateway>1.2.3.4</Gateway>
          <Netmask>255.255.255.240</Netmask>
          <IpAddress>1.2.3.5</IpAddress>
          <UseForDefaultRoute>true</UseForDefaultRoute>
          <IpRanges>
            <IpRange>
              <StartAddress>1.2.3.5</StartAddress>
              <EndAddress>1.2.3.5</EndAddress>
            </IpRange>
          </IpRanges>
        </SubnetParticipation>
        <UseForDefaultRoute>true</UseForDefaultRoute>
      </GatewayInterface>
    </GatewayInterfaces>
  </Configuration>
</EdgeGateway>
于 2017-02-17T04:17:57.260 回答