使用PowerShell从xml的元标记中提取文本

时间:2012-12-03 19:52:42

标签: xml powershell epub

使用以下xml文件,如何提取某些元标记的文本?例如,我想使用属性“role”属性从元标记中获取文本“aut”。

<?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 &amp; 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>

我尝试过使用chooseinglenode()方法但没有成功:

[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

我也尝试过使用Select-XML:

[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']"

4 个答案:

答案 0 :(得分:8)

标记定义了默认命名空间。您需要定义该默认命名空间或使用通配符来避免指定命名空间。后者更容易:

select-xml -xml $xml -xpath "//*[@property='role']/text()" | % { $_.Node.Value }

以下是一些参考资料: http://blog.stevex.net/xpath-and-xml-namespaces/ http://huddledmasses.org/xpath-and-namespaces-in-powershell/

答案 1 :(得分:2)

以下是查找角色节点的方法:

$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}

答案 2 :(得分:1)

这里有一些方法是:

[xml]$foo = Get-Content PATH\TO\FILE.xml
$foo.package.metadata.meta | ?{$_.property -eq 'role'} | Select '#text'

#text
-----
aut
mrk

这将获得两个角色节点。要获得一个aut:

$foo.package.metadata.meta | ?{($_.property -eq 'role') -and ($_.refines -eq '#creator')} | Select '#text'

答案 3 :(得分:1)

这将为您提供每个“元”节点中包含的文本:

PS> $xml = [xml](gc .\pkg.xml)
PS> $xml.package.metadata.meta | % {write-output $_."#text"}

输出:

main
MELVILLE, HERMAN
aut
2012-01-18T12:47:00Z
mrk
http://code.google.com/p/epub-samples/