简体   繁体   中英

Read KML from powershell

I have kml (xml). I want to read it and further process it. Example:

<?xml version="1.0" encoding="UTF-8"?> 
 <kml xmlns="http://www.opengis.net/kml/2.2"> 
 <Document><Placemark> 
 <Point> 
 <coordinates> 30.142550,49.797350,0 </coordinates> 
 </Point> 
 </Placemark><Placemark> 
 <Point> 
 <coordinates> 30.142533,49.797333,0 </coordinates> 
 </Point> 
 </Placemark><Placemark> 
 <Point> 
 <coordinates> 30.142517,49.797283,0 </coordinates> 
 </Point> 
 </Placemark></Document> 
 </kml>

And I'm trying to read it:

Select-Xml -Path $Path -XPath $Xpath -Namespace $XmlNamespace | Select-Object -ExpandProperty Node
$Path = "D:\temp\test\4.kml"
$xpath = "kml/Document/Placemark/Point"
$XmlNamespace = @{ default = '"http://www.opengis.net/kml/2.2"'; };
Select-Xml -Path $Path -XPath $Xpath -Namespace $XmlNamespace | Select-Object -ExpandProperty Node

But there is no result... Why?

Since you are using the default namespace and have chosen to name it (default), you need to then prefix all of your nodes with that name. Also, your name space definition does not need to include literal quotes.

$Path = "D:\temp\test\4.kml"
$xpath = "default:kml/default:Document/default:Placemark/default:Point"
$XmlNamespace = @{ default = "http://www.opengis.net/kml/2.2"}
Select-Xml -Path $Path -XPath $Xpath -Namespace $XmlNamespace |
    Select-Object -ExpandProperty Node

From Select-Xml , the description of -Namespace says the following:

When the XML uses the default namespace, which begins with xmlns, use an arbitrary key for the namespace name. You cannot use xmlns. In the XPath statement, prefix each node name with the namespace name and a colon, such as //namespaceName:Node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM