首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell:如何将InnerText值连接到单个字符串中?

PowerShell:如何将InnerText值连接到单个字符串中?
EN

Stack Overflow用户
提问于 2020-10-14 20:00:33
回答 2查看 655关注 0票数 1

我正在一个目录中遍历多个XML文件。每个文件具有相同的结构:

代码语言:javascript
复制
<input>
    <filePattern>
        <marketCode>cbf_d</marketCode>
        <format>txt</format>
    </filePattern>
</input>
<input>
    <filePattern>
        <marketCode>lvd_cbf_b</marketCode>
        <format>csv</format>
    </filePattern>
</input>
<input>
    <filePattern>
        <marketCode>cbf_a</marketCode>
        <format>zip</format>
    </filePattern>
</input>

我的目的是循环遍历文件中的每个input节点,从marketCodeformat提取InnerText值,然后将它们连接到一个字符串中:

代码语言:javascript
复制
Get-ChildItem -Path 'C:\test\cbf\*merge*' -Recurse |
ForEach-Object {
    $xml_file = $_ 
    
    $content = [xml](Get-Content $xml_file)
    $nodes = $content.SelectNodes('//input')    

    
    foreach ($node in $nodes) {
        $marketCode = $node.SelectNodes('//input/filePattern/marketCode').InnerText
        $format = $node.SelectNodes('//input/filePattern/format').InnerText
        
                                    
    }
    #Trying to cancatenate InnerText values here:
    $marketCode + '_' + $format
    
        
}

我得到的输出:

代码语言:javascript
复制
cbf_d
lvd_cbf_b
cbf_a
_
txt
csv
zip

我期望的输出:

代码语言:javascript
复制
cbf_d_txt
lvd_cbf_b_csv
cbf_a_zip

如何正确连接InnerText的值

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-14 20:13:27

通过使用//input,您将基本路径重置为“全局”--这意味着忽略上下文节点$node。解决方案是在$node表达式中使用相对路径(相对于SelectNodes )。此外,您只在每个文件(而不是每个<input>元素)连接字符串一次,因此需要将连接操作转移到循环中。

代码语言:javascript
复制
Get-ChildItem -Path 'C:\test\cbf\*merge*' -Recurse |
ForEach-Object {
    $xml_file = $_ 
    
    $content = [xml](Get-Content $xml_file)
    $nodes = $content.SelectNodes('//input')    

    
    foreach ($node in $nodes) {
        $marketCode = $node.SelectNodes('filePattern/marketCode').InnerText
        $format = $node.SelectNodes('filePattern/format').InnerText
        #Trying to concatenate InnerText values here:
        $marketCode + '_' + $format
    }
}

输出结果如所需。

票数 2
EN

Stack Overflow用户

发布于 2020-10-15 02:37:59

为了补充zx485 485的有用答案,它很好地解释了您的尝试中存在的问题,并提供了一个有效的解决方案,并提供了一个使用Select-Xml cmdlet的更简洁的解决方案:

代码语言:javascript
复制
Get-ChildItem C:\test\cbf\*merge* -Recurse |
  Select-Xml '//input/filePattern' | 
    ForEach-Object { $_.Node.marketCode, $_.Node.format -join '_' }
  • Select-Xml可以直接对输入文件执行XPath查询,而不必手动将文件内容解析为XML ([xml] (System.Xml.XmlDocument))。
  • 它输出带有元数据的SelectXmlInfo包装器对象,其.Node属性包含匹配的节点。
代码语言:javascript
复制
- As an aside: Given that the metadata is often not needed, it would be convenient if `Select-Xml` could be instructed to return the nodes only, which is the subject of [GitHub feature request #13669](https://github.com/PowerShell/PowerShell/issues/13669).
  • 由于PowerShell的方便的XML DOM的适应性,您可以访问作为属性返回的节点上的子元素(例如,访问<marketCode>子元素的.marketCode ),如果这些元素只有文本内容(只有子文本节点),则直接返回文本(不需要.InnerText)。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64360690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档