删除一个节点当一个属性存在时

huangapple go评论64阅读模式
英文:

Removing a node when an attribute exists

问题

你想要处理XML文档,只保留那些在"Output"节点下的"Target"子节点中具有"value"属性值为"Html"的子节点,对吗?以下是已翻译的代码部分:

Dim doc As New XmlDocument
Dim nodes As XmlNodeList
doc.Load(Path)

nodes = doc.SelectNodes("/report/feature[@tag='Runtime']/feature[@tag='Output']/feature[@tag='Target']")

For Each node As XmlNode In nodes
    If node.SelectSingleNode("property[@name='type']").Attributes("value").Value <> "Html" Then
        node.RemoveAll()
    End If
Next

doc.Save("c:\temp\test.xml")

这段代码将删除在"Output"节点下,"Target"子节点的"value"属性值不是"Html"的所有子节点。

英文:

I have the following xml document.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;report&gt;
	&lt;feature tag=&quot;Config&quot;/&gt;
	&lt;feature tag=&quot;Runtime&quot;&gt;
		&lt;feature tag=&quot;Output&quot;&gt;
			&lt;feature tag=&quot;Target&quot;&gt;
					&lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Html&quot;/&gt;
					&lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Html.Driver&quot;/&gt;
				&lt;/feature&gt;
			&lt;feature tag=&quot;Target&quot;&gt;
					&lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Word&quot;/&gt;
					&lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Word.Driver&quot;/&gt;
				&lt;/feature&gt;
			&lt;feature tag=&quot;Target&quot;&gt;
					&lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;PDF&quot;/&gt;
					&lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Pdf.Driver&quot;/&gt;
				&lt;/feature&gt;
			&lt;/feature&gt;
		&lt;feature tag=&quot;Log&quot;/&gt;
	&lt;/feature&gt;
&lt;/report&gt;

<!-- end snippet -->

I want to read all three subnodes "Target" below the "Output" node in order to find out if the value="Html" is present.

If I find the value "Html", this subnode will be unremoved, all others below "Output" I want to remove, that below "Output" only exists one subnode of "Target".

I am using the XDocument class, but I have no success.

 Dim doc As New XmlDocument
        Dim nodes As XmlNodeList
        doc.Load(Path)

        nodes = doc.SelectNodes(&quot;/report/feature[@tag=&#39;Runtime&#39;]/feature[@tag=&#39;Output&#39;]/feature[@tag=&#39;Target&#39;]&quot;)

        For Each node As XmlNode In nodes
            If node.ChildNodes(0).Attributes(&quot;value&quot;).Value &lt;&gt; &quot;Word&quot; Then
                node.RemoveAll()
            End If
        Next


        doc.Save(&quot;c:\temp\test.xml&quot;)

The result below "Output" is the following, so the "Feature" tags are still remaining.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;feature tag=&quot;Output&quot;&gt;
      &lt;feature&gt;
      &lt;/feature&gt;
      &lt;feature tag=&quot;Target&quot;&gt;
        &lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Word&quot;&gt;
        &lt;/property&gt;
        &lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Word.Driver&quot;&gt;
        &lt;/property&gt;
        &lt;property name=&quot;path&quot; type=&quot;URI&quot; editable=&quot;true&quot; visible=&quot;true&quot; required=&quot;true&quot; value=&quot;&quot;&gt;
        &lt;/property&gt;
        
      &lt;/feature&gt;
      &lt;feature&gt;
      &lt;/feature&gt;
      &lt;feature&gt;
      &lt;/feature&gt;
      &lt;feature&gt;
      &lt;/feature&gt;
    &lt;/feature&gt;

<!-- end snippet -->

答案1

得分: 0

以下是您提供的代码的中文翻译:

这是我使用的数据

Dim doc As XElement
' doc = XElement.Load("your path here")

' 为了测试,使用文字
doc = <report>
          <feature tag="Config"/>
          <feature tag="Runtime">
              <feature tag="Output">
                  <feature tag="Target">
                      <property name="type" editable="false" visible="true" required="false" value="Html"/>
                      <property name="driver" editable="false" visible="true" required="false" value="Telelogic.Html.Driver"/>
                  </feature>
                  <feature tag="Target">
                      <property name="type" editable="false" visible="true" required="false" value="Word"/>
                      <property name="driver" editable="false" visible="true" required="false" value="Telelogic.Word.Driver"/>
                  </feature>
                  <feature tag="Target">
                      <property name="type" editable="false" visible="true" required="false" value="PDF"/>
                      <property name="driver" editable="false" visible="true" required="false" value="Telelogic.Pdf.Driver"/>
                  </feature>
              </feature>
              <feature tag="Log"/>
          </feature>
      </report>

和代码

Dim ie As IEnumerable(Of XElement)
ie = From el In doc...<feature>
        Where el.@tag = "Output"
         From sel In el...<property>
          Where sel.@value = "Html"
          Select sel.Parent

If ie.Count = 1 Then
    Dim parent As XElement = ie(0).Parent
    Dim hld As XElement = New XElement(ie(0))
    ie(0).Parent.Nodes.Remove()
    parent.Add(hld)
End If

和结果

' 在上述操作后的 doc
'<report>
'  <feature tag="Config" />
'  <feature tag="Runtime">
'    <feature tag="Output">
'      <feature tag="Target">
'        <property name="type" editable="false" visible="true" required="false" value="Html" />
'        <property name="driver" editable="false" visible="true" required="false" value="Telelogic.Html.Driver" />
'      </feature>
'    </feature>
'    <feature tag="Log" />
'  </feature>
'</report>

请注意,这是您提供的代码的直译中文翻译,不包括任何额外信息。如果您需要进一步的解释或翻译其他部分,请随时提出。

英文:

So here is the data I used

    Dim doc As XElement
&#39; doc = XElement.Load(&quot;your path here&quot;)
&#39;for test use a literal
doc = &lt;report&gt;
&lt;feature tag=&quot;Config&quot;/&gt;
&lt;feature tag=&quot;Runtime&quot;&gt;
&lt;feature tag=&quot;Output&quot;&gt;
&lt;feature tag=&quot;Target&quot;&gt;
&lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Html&quot;/&gt;
&lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Html.Driver&quot;/&gt;
&lt;/feature&gt;
&lt;feature tag=&quot;Target&quot;&gt;
&lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Word&quot;/&gt;
&lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Word.Driver&quot;/&gt;
&lt;/feature&gt;
&lt;feature tag=&quot;Target&quot;&gt;
&lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;PDF&quot;/&gt;
&lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Pdf.Driver&quot;/&gt;
&lt;/feature&gt;
&lt;/feature&gt;
&lt;feature tag=&quot;Log&quot;/&gt;
&lt;/feature&gt;
&lt;/report&gt;

And the code

    Dim ie As IEnumerable(Of XElement)
ie = From el In doc...&lt;feature&gt;
Where el.@tag = &quot;Output&quot;
From sel In el...&lt;property&gt;
Where sel.@value = &quot;Html&quot;
Select sel.Parent
If ie.Count = 1 Then
Dim parent As XElement = ie(0).Parent
Dim hld As XElement = New XElement(ie(0))
ie(0).Parent.Nodes.Remove()
parent.Add(hld)
End If

And the results

        &#39;doc after above
&#39;&lt;report&gt;
&#39;  &lt;feature tag=&quot;Config&quot; /&gt;
&#39;  &lt;feature tag=&quot;Runtime&quot;&gt;
&#39;    &lt;feature tag=&quot;Output&quot;&gt;
&#39;      &lt;feature tag=&quot;Target&quot;&gt;
&#39;        &lt;property name=&quot;type&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Html&quot; /&gt;
&#39;        &lt;property name=&quot;driver&quot; editable=&quot;false&quot; visible=&quot;true&quot; required=&quot;false&quot; value=&quot;Telelogic.Html.Driver&quot; /&gt;
&#39;      &lt;/feature&gt;
&#39;    &lt;/feature&gt;
&#39;    &lt;feature tag=&quot;Log&quot; /&gt;
&#39;  &lt;/feature&gt;
&#39;&lt;/report&gt;

huangapple
  • 本文由 发表于 2023年6月15日 15:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定