从xml中获取属性值并将其重用于powershell中的另一个xml?

时间:2014-03-10 16:23:21

标签: xml powershell formatting

我有一个问题,我想从一个XML获取属性,并为另一个XML使用相同的获取值。听起来很简单,但有一件事我需要绕过。我的第一个XML在这里看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<ffprobe>
<streams>
    <stream index="0" codec_name="prores" codec_long_name="ProRes" codec_type="video" codec_time_base="1/24" codec_tag_string="apcs" codec_tag="0x73637061" width="1920" height="1080" has_b_frames="0" sample_aspect_ratio="0:1" display_aspect_ratio="0:1" pix_fmt="yuv422p10le" level="-99" r_frame_rate="24/1" avg_frame_rate="24/1" time_base="1/24" start_pts="0" start_time="0.000000" duration_ts="1152" duration="48.000000" bit_rate="13174677" nb_frames="1152" nb_read_frames="1152">
        <disposition default="1" dub="0" original="0" comment="0" lyrics="0" karaoke="0" forced="0" hearing_impaired="0" visual_impaired="0" clean_effects="0" attached_pic="0"/>
        <tag key="creation_time" value="2013-10-10 02:59:14"/>
        <tag key="language" value="eng"/>
        <tag key="handler_name" value="DataHandler"/>
    </stream>
</streams>
</ffprobe>

我抓住nb_frames attribut并将其保存在名为$totalframes的参数中:

$xmlpfad = [xml](Get-Content C:\Users\Administrator\Desktop\input_dcp\$filename+totalframes.xml)
        [string]$totalframes = $xmlpfad.ffprobe.streams.stream | select nb_frames

现在,当我尝试将此值用于我的第二个XML时,我得到了类似的内容:

<NumOfRepetitions>@{nb_frames=1152}</NumOfRepetitions>

但我只需要值1152本身,没有@{nb_frames=}这可能是某种方式吗?

1 个答案:

答案 0 :(得分:1)

您的totalframes变量将是一个具有名为nb_frames的属性的对象。

为了只获取属性的值,您需要使用-ExpandProperty cmdlet的Select参数。所以将最后一行改为:

[string]$totalframes = $xmlpfad.ffprobe.streams.stream | select -ExpandProperty nb_frames