How to read o:xml with powershell?

∥☆過路亽.° 提交于 2020-12-13 04:24:06

问题


I have following xml file but don't know how to read it with Powershell, Anyone can help with? Thanks!

I need to get the url value from Powershell.

<o:OfficeConfig xmlns:o="urn:xxx:xxx:xxx">
<o:services>
<o:service o:name="xxxx">
<o:url>https://xxx.xxx</o:url>
</o:service>
</o:services>
</o:OfficeConfig>

Thanks in advance!


回答1:


You can take advantage of the fact that PowerShell's convenient, property-based adaptation of the XML DOM essentially ignores namespaces, allowing to you simply drill down to the element of interest by the unqualified element names:

([xml] (Get-Content -Raw file.xml)).OfficeConfig.services.service.url

By contrast, the XPath-based Select-Xml cmdlet is namespace-aware, and therefore requires explicit namespace handling - or a workaround via the local-name() function, as shown in Mathias R. Jessen's answer.

If you want to use proper namespace handling - which is ultimately more robust, but not always necessary - use the following:

(
  Select-Xml '//o:url' file.xml -Namespace @{ o='urn:schemas-microsoft-com:office:office' }
).Node.InnerText
  • Note the need to pass a hashtable (@{ ... }) that declares the namespace prefixes and URLs used, which is the prerequisite for being able to use the prefixes (o:, in this case) in the XPath query.

    • The prefix names need not match the ones in the original, as long as they're consistent with the -Namespace argument and are mapped to the original URLs.
  • Select-Xml returns wrapper objects around the matched System.Xml.XmlNode instances, so .Node is required to access the latter, and .InnerText then returns the node's text content.

    • As an aside: This need to access .Node is inconvenient, as the typical use case is to care about the XmlNode only; GitHub suggestion #13669 seeks to ease the pain via a
      -Raw switch that returs the XmlNode instances directly.



回答2:


You could use Select-Xml:

$rawXml = @'
<o:OfficeConfig xmlns:o="urn:schemas-microsoft-com:office:office">
<o:services>
<o:service o:name="GetFederationProvider">
<o:url>https://odc.officeapps.live.com/odc/emailhrd/getfederationprovider</o:url>
</o:service>
</o:services>
</o:OfficeConfig>
'@ 

$urlNode = $rawXml |Select-Xml -XPath '//*[local-name() = "url"]' |Select -Expand Node
$url = $urlNode.innerText

$url will now contain the string "https://odc.officeapps.live.com/odc/emailhrd/getfederationprovider"




回答3:


Since you are returning json, you can just convert from json to a PowerShell object:

$configServiceUrl = "https://officeclient.microsoft.com/config16processed?rs=en-us&build=16.0.7612"
$headers = @{'Accept' = 'application/json'}
$getFederationProviderEndpoint = Invoke-WebRequest -Uri "$($configServiceUrl)&services=GetFederationProvider" -Headers $headers -Method GET

$obj = $getFederationProviderEndpoint.Content | ConvertFrom-Json
$obj.'o:officeconfig'.'o:services'.'o:service'.'o:url'


来源:https://stackoverflow.com/questions/64699388/how-to-read-oxml-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!