gpt4 book ai didi

xml - Powershell,XML和 namespace

转载 作者:行者123 更新时间:2023-12-03 16:16:18 25 4
gpt4 key购买 nike

我有一些XML:

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<P2MessageServiceStatus>
<CONNECTION>CONNECTION_CONNECTED</CONNECTION>
<ROUTER>ROUTER_CONNECTED</ROUTER>
</P2MessageServiceStatus>
</SOAP:Body>
</SOAP:Envelope>

由于 namespace 而无法处理。尝试了很多,但是没有用。

[string]$xpath = '/SOAP:Envelope/SOAP:Body/P2MessageServiceStatus/@CONNECTION'

$wc = New-Object Net.WebClient
[xml]$stuff = $wc.DownloadString($url)

$ns = New-Object Xml.XmlNamespaceManager $stuff.NameTable
$ns.AddNamespace("SOAP", $xmlns)

$xmlvalue = $stuff.SelectSingleNode($xpath, $ns)

Error - need Namespace manager or XsltContext



如果

$xmlvalue = $stuff.SelectSingleNode($xpath,'SOAP')

Error - Unable to find overload for "SelectSingleNode"



如何使xpath查询与 namespace 一起使用?

最佳答案

我没有看到您定义的$xmlns。这有效:

[string]$xpath='/SOAP:Envelope/SOAP:Body/P2MessageServiceStatus/CONNECTION'

$namespaceMgr = New-Object System.Xml.XmlNamespaceManager $stuff.NameTable
$namespace = $stuff.DocumentElement.NamespaceURI
$namespaceMgr.AddNamespace("SOAP", $namespace)


$stuff.SelectSingleNode($xpath,$ns)

注意:您还可以使用 PowerShell来访问诸如属性之类的节点:
$stuff.DocumentElement.Body.P2MessageServiceStatus.CONNECTION

关于xml - Powershell,XML和 namespace ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37124340/

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