【问题标题】:Select module which is the have the comment with Powershell选择带有 Powershell 注释的模块
【发布时间】:2019-05-21 00:50:37
【问题描述】:

我想选择有注释行的模块。

<modules>
    <module>com.umut.web</module><!--auto-->
    <module>com.umut.oaservice</module>
    <module>com.umut.signaturephotoservice</module>
  </modules>

powershell 代码=

[string]$modules=""
foreach ($module in $xmlFile.project.modules.module) {

  Write-Output $module.'#comment'  

}

【问题讨论】:

    标签: xml powershell pom.xml


    【解决方案1】:

    我认为您要问的是如何在 xml 中找到实际上直接在其后面有评论元素的&lt;module&gt;,对吧?

    在这种情况下,可以这样做:

    [xml]$xmlFile = @"
    <project>
        <modules>
            <module>com.umut.web</module>
            <module>com.umut.oaservice</module><!--auto-->
            <module>com.umut.signaturephotoservice</module>
        </modules>
    </project>
    "@
    
    foreach ($node in $xmlFile.project.modules.ChildNodes) {
        if ($node.NodeType -eq 'Comment') {
            # if this comment node following a childnode within 'modules' 
            if ($node.PreviousSibling) {
                [PSCustomObject]@{
                    'module'      = $node.PreviousSibling            # the module element (System.Xml.XmlElement object)
                    'moduleText'  = $node.PreviousSibling.InnerText  # the text inside this module element
                    'comment'     = $node                            # the comment element (System.Xml.XmlComment)
                    'commentText' = $node.InnerText                  # the text inside the comment
                }
                break
            }
        }
    }
    

    如果搜索没有产生任何结果,它会发出一个具有四个属性的 PSCustomObject 或什么都没有。

    module moduleText         comment  commentText
    ------ ----------         -------  -----------
    module com.umut.oaservice #comment auto
    

    【讨论】:

      【解决方案2】:

      我认为你在foreach 中的级别太过分了。

      如果你这样做:

      [string]$modules=""
      foreach ($module in $xmlFile.project.modules) {
      Write-Output $module.'#comment'  
      } 
      

      【讨论】:

      • 是的,我现在看到了自动输出。但我还需要同一行的其他值。对于这个例子,我还需要 com.umut.web 这个值
      • 我看到的问题是#comment 属性未链接到实际评论所在的行。 [皱眉] 我试着把它移到第二行,它仍然显示为唯一的评论而不是第二条评论。这使得您似乎无法使用 XML 内容将注释与它应该寻址的模块连接起来。
      • 是的,我认为我无法为您提供帮助。我建议重组文件。搜索属性和值要简单得多。
      猜你喜欢
      • 1970-01-01
      • 2015-10-05
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多