使用Powershell转换XML-此Sed命令的等效项是什么?

使用Powershell转换XML-此Sed命令的等效项是什么?,powershell,Powershell,我得到了以下XML片段: 但我不知道下一步该怎么做 任何指点都将不胜感激, 约翰我会这样做: $Text = @' <simpleType name="StatusType"> <restriction base="integer"> <enumeration value="1"> <annotation> <documentation>Proposed</documentation>

我得到了以下XML片段:

但我不知道下一步该怎么做

任何指点都将不胜感激,
约翰

我会这样做:

$Text = 
@'
<simpleType name="StatusType">
  <restriction base="integer">
    <enumeration value="1">
      <annotation>
        <documentation>Proposed</documentation>
      </annotation>
    </enumeration>
    <enumeration value="2">
      <annotation>
        <documentation>In Use</documentation>
      </annotation>
    </enumeration>
  </restriction>
</simpleType>
'@

$regex = 
@'
(?ms)<simpleType name="StatusType">
  <restriction base="integer">
    <enumeration value="(\d+)">
      <annotation>
        <documentation>Proposed</documentation>
      </annotation>
    </enumeration>
    <enumeration value="(\d+)">
      <annotation>
        <documentation>In Use</documentation>
      </annotation>
    </enumeration>
  </restriction>
</simpleType>
'@

if ($Text -match $regex)
{
@'
<simpleType name="StatusType">
  <restriction base="integer">
      <enumeration value="{0}" id="Proposed"/>
      <enumeration value="{1}" id="In Use"/>
  </restriction>
</simpleType>
'@ -f $Matches[1,2]
}

<simpleType name="StatusType">
  <restriction base="integer">
      <enumeration value="1" id="Proposed"/>
      <enumeration value="2" id="In Use"/>
  </restriction>
</simpleType>
$Text=
@'
提出
使用中
'@
$regex=
@'
(?ms)
提出
使用中
'@
if($Text-匹配$regex)
{
@'
“@-f$匹配[1,2]
}

它并不十分简洁,但对于下一个继承它的人来说,它直观且易于维护。

我知道您要求使用等效的sed,但有一个用于操作XML的API,我必须相信,在XML上使用regex要安全得多。有一种方法可以做到这一点:

$xml = [xml]@'
<simpleType name="StatusType">
  <restriction base="integer">
    <enumeration value="1">
      <annotation>
        <documentation>Proposed</documentation>
      </annotation>
    </enumeration>
    <enumeration value="2">
      <annotation>
        <documentation>In Use</documentation>
      </annotation>
    </enumeration>
  </restriction>
</simpleType>
'@

foreach ($enum in $xml.simpleType.restriction.enumeration) {
    [void]$enum.SetAttribute('id', $enum.annotation.documentation.Trim())
    [void]$enum.RemoveChild($enum.annotation)
    $enum.IsEmpty = $true
}

$xml | Format-Xml

好吧,既然你似乎对用regex替换没问题,你可以只做
(Get Content.\data.xml-Raw)-replace'(?s)(很好,这对我很有用,谢谢你花时间回答。看起来不错,我可能会用这个方法或下面的方法来避免使用regex。很好的解决方案,这里有很多选项供我考虑。
cat input_file.xml | sed -e '/<enumeration.*[^\/]>/{N;N;N;N;s/\r*\n[ \t]*//g;s/><annotation><documentation>/ id="/;s/<\/documentation><\/annotation><\/enumeration/"\//}'>output_file.xml
Get-Content input_file.xml | %{ $_ -replace "enumeration", "replacement_text" }
$Text = 
@'
<simpleType name="StatusType">
  <restriction base="integer">
    <enumeration value="1">
      <annotation>
        <documentation>Proposed</documentation>
      </annotation>
    </enumeration>
    <enumeration value="2">
      <annotation>
        <documentation>In Use</documentation>
      </annotation>
    </enumeration>
  </restriction>
</simpleType>
'@

$regex = 
@'
(?ms)<simpleType name="StatusType">
  <restriction base="integer">
    <enumeration value="(\d+)">
      <annotation>
        <documentation>Proposed</documentation>
      </annotation>
    </enumeration>
    <enumeration value="(\d+)">
      <annotation>
        <documentation>In Use</documentation>
      </annotation>
    </enumeration>
  </restriction>
</simpleType>
'@

if ($Text -match $regex)
{
@'
<simpleType name="StatusType">
  <restriction base="integer">
      <enumeration value="{0}" id="Proposed"/>
      <enumeration value="{1}" id="In Use"/>
  </restriction>
</simpleType>
'@ -f $Matches[1,2]
}

<simpleType name="StatusType">
  <restriction base="integer">
      <enumeration value="1" id="Proposed"/>
      <enumeration value="2" id="In Use"/>
  </restriction>
</simpleType>
$xml = [xml]@'
<simpleType name="StatusType">
  <restriction base="integer">
    <enumeration value="1">
      <annotation>
        <documentation>Proposed</documentation>
      </annotation>
    </enumeration>
    <enumeration value="2">
      <annotation>
        <documentation>In Use</documentation>
      </annotation>
    </enumeration>
  </restriction>
</simpleType>
'@

foreach ($enum in $xml.simpleType.restriction.enumeration) {
    [void]$enum.SetAttribute('id', $enum.annotation.documentation.Trim())
    [void]$enum.RemoveChild($enum.annotation)
    $enum.IsEmpty = $true
}

$xml | Format-Xml
<simpleType name="StatusType">
  <restriction base="integer">
    <enumeration value="1" id="Proposed" />
    <enumeration value="2" id="In Use" />
  </restriction>
</simpleType>
$xml.Save("C:\foo.xml")