gpt4 book ai didi

xml - 从xml拾取的Powershell联接对象

转载 作者:行者123 更新时间:2023-12-02 23:17:25 25 4
gpt4 key购买 nike

我有以下内容:

$XPath = "//INVOICEHEADER"
$XPath2 = "//FILE"
# path where we need to look for the xml. * means all files that end in .xml
$Path = "$sourcelocation\*.xml"

# Select-Xml -Path $Path -XPath $Xpath |
# Select-Object -EA SilentlyContinue -ExpandProperty Node |
# # export results append to the file NNUH.csv
# Export-Csv -append -Path $resultfilepathtemp -NoTypeInformation
Get-ChildItem -Path $Path |

ForEach-Object {
$xml = Select-Xml -Path $_.FullName -XPath $Xpath
$results = Select-Object -InputObject $Xml -EA SilentlyContinue -ExpandProperty Node
# Write-Output $results
$xml = Select-Xml -Path $_.FullName -XPath $Xpath2
$results2 = Select-Object -InputObject $Xml -EA SilentlyContinue -ExpandProperty Node

# Need to join results together

Export-Csv -InputObject $results -append -Path $resultfilepathtemp -NoTypeInformation
}

我正在尝试加入从.xml中拾取的两个对象。挣扎于如何使它输出到文件。 .xml看起来像这样:
XML
<?xml version="1.0" encoding="UTF-8"?>
<INVOICEIMPORT TYPE="INVOICE">
<INVOICES>
<INVOICE>
<INVOICEHEADER>
<CATEGORY>1</CATEGORY>
<PURCHASEORDERNO>AA00000</PURCHASEORDERNO>
<OURREF>AA00000</OURREF>
<ATTRIB2>Client name</ATTRIB2>
<ATTRIB3>Address</ATTRIB3>
<SUPPLIERREF>1000000000</SUPPLIERREF>
<INVOICEDATE>20200302</INVOICEDATE>
<ARRIVAL>20200401</ARRIVAL>
<AMOUNT>100,401.20</AMOUNT>
<VATAMOUNT>13,900.20</VATAMOUNT>
<CURRENCY.CURRENCYID>GBP</CURRENCY.CURRENCYID>
<CLIENT.CODE>9999</CLIENT.CODE>
<ITVNCODE>No supplier found</ITVNCODE>
</INVOICEHEADER>
<FILES>
<FILE>
<TRANSACTIONTYPE>X104</TRANSACTIONTYPE>
<FULLFILENAME>PP_XXXX_00001.pdf</FULLFILENAME>
<PAGENUMBER>1</PAGENUMBER>
</FILE>
</FILES>
</INVOICE>
</INVOICES>
</INVOICEIMPORT>

并且输出应采用类似以下格式:
"CATEGORY","PURCHASEORDERNO","OURREF","ATTRIB2","ATTRIB3","SUPPLIERREF","INVOICEDATE","ARRIVAL","AMOUNT","VATAMOUNT","CURRENCY.CURRENCYID","CLIENT.CODE","ITVNCODE","TRANSACTIONTYPE","FULLTIMENAME","PAGENUMBER"
"1","AA00000","AA00000","Client name","Address","1000000000","20200302","20200401","100,401.20","13,900.20","GBP","9999","No supplier found","X104","PP_XXXX_00001.pdf","1"

以为我是刚接触PowerShell的人(一周多来),因此我对此已经越来越接近了。我在最终将在其上运行代码的服务器上安装了PowerShell 5.1。我试图加入他们:
# Need to join results together
$endresult = $results + $results2
Export-Csv -InputObject $endresult -append -Path $resultfilepathtemp -NoTypeInformation

但是我收到一条错误消息:
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Client\ClientX.ps1:74 char:29
+ Export-Csv -InputObject $rpc -append -Path $resultfilepathtemp -N ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
mmand

最佳答案

您的$results$results2变量包含 System.Xml.XmlElement 实例(可能是数组)。

  • 如果变量包含数组(如果您的输入XML有多个INVOICE元素,这似乎是可能的):
  • +与数组结合使用(创建一个包含LHS数组的所有元素,然后是RHS数组的所有元素的新数组)-这不会做您想要的事情,即合并相应XML中的子元素元素。
  • 如果变量包含标量(每个为XmlElement实例):
  • 使用+失败,因为XmlElement并未为+运算符定义重载。


  • 要获得所需的输出:
  • 您必须在元素对中处理数组。
  • 对于每对,您必须合并输入的XmlElement,以形成包含各个子节点的并集的元素。
  • 然后,您可以使用Export-Csv将合并的元素导出到CSV文件。
  • $XPath = "//INVOICEHEADER"
    $XPath2 = "//FILE"
    $Path = "$sourcelocation\*.xml"

    Get-ChildItem -Path $Path |
    ForEach-Object {

    # Load and parse the XML file once.
    [xml] $xml = Get-Content -Raw $_.FullName

    # Run the XPath queries.
    $results = $xml.SelectNodes($XPath)
    $results2 = $xml.SelectNodes($XPath2)

    # Process the results in pairs:
    # Create a new element containing the union of the child elements.
    foreach ($i in 0..($results.Count-1)) {
    # Append the child nodes of the RHS array element to the LHS
    # array element.
    # NOTE: @(...) ensures that the child nodes are collected in an aux.
    # array up front; without that, the .AppendChild() calls would
    # interfere with the enumeration of .ChildNodes.
    foreach ($child in @($results2[$i].ChildNodes)) {
    $null = $results[$i].AppendChild($child)
    }
    # Output the merged element.
    $results[$i]
    }

    } | Export-Csv -Path $resultfilepathtemp -NoTypeInformation

    关于xml - 从xml拾取的Powershell联接对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61208263/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com