Нужна помощь для вставки дочернего узла в XML файл - PullRequest
1 голос
/ 08 апреля 2020

Требуется помощь в PowerShell для исправления файла XML. Узел, который я хочу вставить сюда, не первый дочерний элемент файла XML. Структура текущего файла - это текущий файл, и мне нужна помощь по этому

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Shipment xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" >
    <ShipmentNumber>0012546776</ShipmentNumber>
    <Container>Selleys/Yates</Container>
    <Extnl_id/>
    <AddressForwardingAgent>
        <PartnerNumber>0000400260</PartnerNumber>
        <LanguageKey>EN</LanguageKey>
        <Name>TEST</Name>
        <HouseandStreet>TEST1</HouseandStreet>
        </AddressForwardingAgent>
    <AddressSender>
        <TransportPlanningPoint>4001</TransportPlanningPoint>
        <LanguageKey>EN</LanguageKey>
        <Name>TEST2</Name>
        <HouseandStreet>TEST2</HouseandStreet>
    </AddressSender>
    <Consignment>
        <ConsignmentNumber>0001</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
            <GrossWeight>2005.268</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>3.856</TotalVolume>
            <VolumeUOM>M3</VolumeUOM>
        </ConsignmentHeader>
        <ConsignmentNumber>0002</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
            <GrossWeight>21.12</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>0.044</TotalVolume>
            <VolumeUOM>M3</VolumeUOM>
        </ConsignmentHeader>
        <ConsignmentNumber>0003</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>Y</DangerousGoodsIndicator>
            <GrossWeight>12.45</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>0.056</TotalVolume>
            <VolumeUOM>P3</VolumeUOM>
        </ConsignmentHeader>
    </Consignment>

Ожидаемый результат - Такая структура мне нужна в XML с помощью PowerShell.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Shipment xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" >
    <ShipmentNumber>0012546776</ShipmentNumber>
    <Container>Selleys/Yates</Container>
    <Extnl_id/>
    <AddressForwardingAgent>
        <PartnerNumber>0000400260</PartnerNumber>
        <LanguageKey>EN</LanguageKey>
        <Name>TEST</Name>
        <HouseandStreet>TEST1</HouseandStreet>
        </AddressForwardingAgent>
    <AddressSender>
        <TransportPlanningPoint>4001</TransportPlanningPoint>
        <LanguageKey>EN</LanguageKey>
        <Name>TEST2</Name>
        <HouseandStreet>TEST2</HouseandStreet>
    </AddressSender>
    <Consignment>
    <ConsigmentLine>                 ## Need to insert this tag here
        <ConsignmentNumber>0001</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
            <GrossWeight>2005.268</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>3.856</TotalVolume>
            <VolumeUOM>M3</VolumeUOM>
        </ConsignmentHeader>
    </ConsigmentLine>
    <ConsigmentLine>    ## Need to insert this tag here
        <ConsignmentNumber>0002</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
            <GrossWeight>21.12</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>0.044</TotalVolume>
            <VolumeUOM>M3</VolumeUOM>
        </ConsignmentHeader>
    </ConsigmentLine>
    <ConsigmentLine>    ## Need to insert this tag here
        <ConsignmentNumber>0003</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>Y</DangerousGoodsIndicator>
            <GrossWeight>12.45</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>0.056</TotalVolume>
            <VolumeUOM>P3</VolumeUOM>
        </ConsignmentHeader>
    </ConsigmentLine>
    </Consignment>

Ответы [ 2 ]

2 голосов
/ 08 апреля 2020

Сначала необходимо захватить узлы «ConsignmentNumber» и «ConsignmentHeader», удалить узел «Consignment» из xml и восстановить его.

Для демонстрации я использую Here-String , но вам, вероятно, потребуется прочитать его из файла, используя [xml]$xml = Get-Content -Path 'D:\MyCurrentXml.xml'

[xml]$xml = @"
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Shipment xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" >
    <ShipmentNumber>0012546776</ShipmentNumber>
    <Container>Selleys/Yates</Container>
    <Extnl_id/>
    <AddressForwardingAgent>
        <PartnerNumber>0000400260</PartnerNumber>
        <LanguageKey>EN</LanguageKey>
        <Name>TEST</Name>
        <HouseandStreet>TEST1</HouseandStreet>
        </AddressForwardingAgent>
    <AddressSender>
        <TransportPlanningPoint>4001</TransportPlanningPoint>
        <LanguageKey>EN</LanguageKey>
        <Name>TEST2</Name>
        <HouseandStreet>TEST2</HouseandStreet>
    </AddressSender>
    <Consignment>
        <ConsignmentNumber>0001</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
            <GrossWeight>2005.268</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>3.856</TotalVolume>
            <VolumeUOM>M3</VolumeUOM>
        </ConsignmentHeader>
        <ConsignmentNumber>0002</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
            <GrossWeight>21.12</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>0.044</TotalVolume>
            <VolumeUOM>M3</VolumeUOM>
        </ConsignmentHeader>
        <ConsignmentNumber>0003</ConsignmentNumber>
        <ConsignmentHeader>
            <DangerousGoodsIndicator>Y</DangerousGoodsIndicator>
            <GrossWeight>12.45</GrossWeight>
            <WeightUOM>KG</WeightUOM>
            <TotalVolume>0.056</TotalVolume>
            <VolumeUOM>P3</VolumeUOM>
        </ConsignmentHeader>
    </Consignment>
</Shipment>
"@

Чтобы восстановить его так, как вы хотите, это должно работать:

# capture two arrays of nodes for 'ConsignmentNumber' and 'ConsignmentHeader'
$numbers = $xml.Shipment.Consignment.ChildNodes | Where-Object { $_.LocalName -eq 'ConsignmentNumber' }
$headers = $xml.Shipment.Consignment.ChildNodes | Where-Object { $_.LocalName -eq 'ConsignmentHeader' }

# remove the entire 'Consignment' node from the xml
$xml.Shipment.RemoveChild($xml.Shipment.Consignment)
# and create a new 'Consignment' node
$newConsignment = $xml.CreateElement('Consignment')

# loop through the arrays, create a new 'ConsigmentLine' node and add the captured nodes to it
for ($i = 0; $i -lt $numbers.Count; $i++) {
    $lineNode = $xml.CreateElement('ConsigmentLine')
    $lineNode.AppendChild($numbers[$i])
    $lineNode.AppendChild($headers[$i])
    # append this to the newly created 'Consignment' node
    $newConsignment.AppendChild($lineNode)
}
# finally, append the new 'Consignment' node to the 'Shipment' node
$xml.Shipment.AppendChild($newConsignment)

# and save the xml
$xml.Save('D:\MyCorrectedXml.xml')

Результат:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Shipment xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
  <ShipmentNumber>0012546776</ShipmentNumber>
  <Container>Selleys/Yates</Container>
  <Extnl_id />
  <AddressForwardingAgent>
    <PartnerNumber>0000400260</PartnerNumber>
    <LanguageKey>EN</LanguageKey>
    <Name>TEST</Name>
    <HouseandStreet>TEST1</HouseandStreet>
  </AddressForwardingAgent>
  <AddressSender>
    <TransportPlanningPoint>4001</TransportPlanningPoint>
    <LanguageKey>EN</LanguageKey>
    <Name>TEST2</Name>
    <HouseandStreet>TEST2</HouseandStreet>
  </AddressSender>
  <Consignment>
    <ConsigmentLine>
      <ConsignmentNumber>0001</ConsignmentNumber>
      <ConsignmentHeader>
        <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
        <GrossWeight>2005.268</GrossWeight>
        <WeightUOM>KG</WeightUOM>
        <TotalVolume>3.856</TotalVolume>
        <VolumeUOM>M3</VolumeUOM>
      </ConsignmentHeader>
    </ConsigmentLine>
    <ConsigmentLine>
      <ConsignmentNumber>0002</ConsignmentNumber>
      <ConsignmentHeader>
        <DangerousGoodsIndicator>X</DangerousGoodsIndicator>
        <GrossWeight>21.12</GrossWeight>
        <WeightUOM>KG</WeightUOM>
        <TotalVolume>0.044</TotalVolume>
        <VolumeUOM>M3</VolumeUOM>
      </ConsignmentHeader>
    </ConsigmentLine>
    <ConsigmentLine>
      <ConsignmentNumber>0003</ConsignmentNumber>
      <ConsignmentHeader>
        <DangerousGoodsIndicator>Y</DangerousGoodsIndicator>
        <GrossWeight>12.45</GrossWeight>
        <WeightUOM>KG</WeightUOM>
        <TotalVolume>0.056</TotalVolume>
        <VolumeUOM>P3</VolumeUOM>
      </ConsignmentHeader>
    </ConsigmentLine>
  </Consignment>
</Shipment>

PS в вашем примере xml отсутствует окончательное закрытие </Shipment> тег

0 голосов
/ 08 апреля 2020

Вы также можете попробовать следующий подход. Я добавил комментарии, чтобы объяснить решение. Также добавлен отсутствующий закрывающий тег </Shipment> в конце файла XML.

# Function to zip two arrays together using .NET LINQ
function Select-Zip {
    [CmdletBinding()]
    Param(
        $First,
        $Second,
        $ResultSelector = { ,$args }
    )

    [System.Linq.Enumerable]::Zip($First, $Second, [Func[Object, Object, Object[]]]$ResultSelector)
}

# Create XML object to load data into
$xml = New-Object -TypeName System.Xml.XmlDocument

# Load in XML file
$xml.Load("test.xml")

# Get root node where we make the changes
$consignmentRootNode = $xml.Shipment.Consignment

# Get both number and header nodes
$numberNodes = $consignmentRootNode.ConsignmentNumber
$headerNodes = $consignmentRootNode.ConsignmentHeader

# Zip above nodes to form number & header pairs
$zippedNodes = Select-Zip -First $numberNodes -Second $headerNodes

# Remove consignment root node
$xml.Shipment.RemoveChild($consignmentRootNode)

# Rebuild new consignment node 
$newConsignmentRootNode = $xml.CreateElement("Consignment")

# Iterate each pair
foreach ($pair in $zippedNodes)
{
    # Create new line node
    $consignmentLineNode = $xml.CreateElement("ConsigmentLine")

    # Build and append number node
    # Need to create a new one because number node is stored as a string
    $consignmentNumberNode = $xml.CreateElement("ConsignmentNumber")
    $consignmentNumberNode.InnerText = $pair[0]
    $consignmentLineNode.AppendChild($consignmentNumberNode)

    # Append header node
    $consignmentLineNode.AppendChild($pair[1])
    $newConsignmentRootNode.AppendChild($consignmentLineNode)
}

# Add rebuilt consignment node to shipment node
$xml.Shipment.AppendChild($newConsignmentRootNode)

# Save output to new file
# Don't want to corrupt original
$xml.Save("output.xml")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...