这是一个XML文件,我想使用PowerShell代码从其中提取IP地址(此处为172.31.24.8):
<?xml version="1.0"?>
<Configuration xmlns="http://www.tandberg.com/XML/CUIL/2.0" product="Cisco Codec" version="TC7.3.2.14ad7cc" apiVersion="2">
<SIP item="1">
<Profile item="1" maxOccurrence="1">
<Proxy item="1" maxOccurrence="4">
<Address item="1" valueSpaceRef="/Valuespace/STR_0_255_NoFilt">172.31.24.8</Address>
<Discovery item="1" valueSpaceRef="/Valuespace/TTPAR_AutoManual">Manual</Discovery>
</Proxy>
</Profile>
</SIP>
</Configuration>
我尝试按照其他StackOverflow示例中的说明使用
Select-Xml
,但到目前为止仍未成功。正确实现此目标的最简单方法是什么?
最佳答案
我对Powershell并不熟悉,因此无法为您提供Powershell代码,但是我认为Select-Xml可与XPath表达式一起使用。
给定您的XML文档,并且可以通过某种方式注册默认 namespace ,以下XPath表达式将起作用:
/Configuration/SIP/Profile/Proxy/Address/text()
如果您无法注册默认 namespace ,则可以注册前缀和 namespace ,例如
/tb:Configuration/tb:SIP/tb:Profile/tb:Proxy/tb:Address/text()
其中
tb
必须对应于 namespace http://www.tandberg.com/XML/CUIL/2.0
。 Here is a link解释了如何声明 namespace 。如果Select-Xml无法处理 namespace ,请使用
/*[local-name() = 'Configuration']/*[local-name() = 'SIP']/*[local-name() = 'Profile']/*[local-name() = 'Proxy']/*[local-name() = 'Address']/text()
所有这些路径表达式的唯一结果是
172.31.24.8
关于xml - 尝试获取XML元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295498/