Reputation: 421
With the following xml file, how can I extract some of the meta tag's text? For example, I want to get the text "aut" out of the meta tag with the property "role" attribute.
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="pub- id" prefix="cc: http://creativecommons.org/ns#">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title id="title">Moby-Dick</dc:title>
<meta refines="#title" property="title-type">main</meta>
<dc:creator id="creator">Herman Melville</dc:creator>
<meta refines="#creator" property="file-as">MELVILLE, HERMAN</meta>
<meta refines="#creator" property="role" scheme="marc:relators">aut</meta>
<dc:identifier id="pub-id">code.google.com.epub-samples.moby-dick-basic</dc:identifier>
<dc:language>en-US</dc:language>
<meta property="dcterms:modified">2012-01-18T12:47:00Z</meta>
<dc:publisher>Harper & Brothers, Publishers</dc:publisher>
<dc:contributor id="contrib1">Dave Cramer</dc:contributor>
<meta refines="#contrib1" property="role" scheme="marc:relators">mrk</meta>
<dc:rights>This work is shared with the public using the Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.</dc:rights>
<link rel="cc:license" href="http://creativecommons.org/licenses/by-sa/3.0/"/>
<meta property="cc:attributionURL">http://code.google.com/p/epub-samples/</meta>
</metadata>
</package>
I've tried using the selectsinglenode() method as such with no success:
[xml]$doc = get-content myxmlfile.xml
$node = $doc.selectsinglenode("/package/metadata/meta[@property='role']")
//and
$node = $doc.selectsinglenode("//meta[@property='role']")
// $node is null for both cases
I've also tried using Select-XML as such:
[xml]$doc = get-content myxmlfile.xml
Select-Xml -xml $doc -Xpath "/package/metadata/meta[@property='role']"
//and
Select-Xml -xml $doc -Xpath "//meta[@property='role']"
Upvotes: 3
Views: 6862
Reputation: 2189
The tag defined a default namespace. You either need to define that default namespace or use a wildcard to avoid specifying a namespace. The latter is easier:
select-xml -xml $xml -xpath "//*[@property='role']/text()" | % { $_.Node.Value }
Here are a couple of references: http://blog.stevex.net/xpath-and-xml-namespaces/ http://huddledmasses.org/xpath-and-namespaces-in-powershell/
Upvotes: 8
Reputation: 6561
This will give you the text contained in each of the "meta"nodes:
PS> $xml = [xml](gc .\pkg.xml)
PS> $xml.package.metadata.meta | % {write-output $_."#text"}
Output:
main
MELVILLE, HERMAN
aut
2012-01-18T12:47:00Z
mrk
http://code.google.com/p/epub-samples/
Upvotes: 1
Reputation: 72680
Here is a way to find the role nodes :
$xmlFile = [xml](Get-Content C:\temp\meta.xml)
$xmlFile.package.metadata.meta |Where-Object {$_.property -eq "role"}
$xmlFile.package.metadata.meta |Where-Object {$_.property -eq "role"} | foreach {$_.innertext}
Upvotes: 2
Reputation: 37800
There are a few methods here is one:
[xml]$foo = Get-Content PATH\TO\FILE.xml
$foo.package.metadata.meta | ?{$_.property -eq 'role'} | Select '#text'
#text
-----
aut
mrk
That would get both the role nodes. To get just the one with aut:
$foo.package.metadata.meta | ?{($_.property -eq 'role') -and ($_.refines -eq '#creator')} | Select '#text'
Upvotes: 1