注释 XML 部分(在节点中有前缀)并取消注释其他 [英] Comment XML section (having prefixes in the nodes) and un-comment other

查看:53
本文介绍了注释 XML 部分(在节点中有前缀)并取消注释其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的示例 xml 数据.如果有任何语法错误或一些缺少的 xml 功能,请忽略.任务是评论以上部分并取消评论以下部分.

This is my sample xml data. Please ignore if there is any syntax error or some missing xml features. The task is to comment the above section and un-comment the below section.

<?xml version="1.0" encoding="UTF-8"?>
<providersbeans xmlns="http://www.springframework.org/schema/providers"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:dns="http://www.springframework.org/schema/dns"
  xsi:schemaLocation="http://www.springframework.org/schema/dns/spring-dns.xsd">

<!-- COMMENT THIS SECTION -->

<dns:auth-head alias="authHead">
    <dns:user-generator>    
        <dns:user-data>

            <!-- USER AA -->
            <dns:user name="AA"
                password="AAAAAAAAAAAAAAAA" role="ROLE_AA" />

            <!-- USER BB -->
            <dns:user name="BB"
                password="XXXXXXXXXXXXXXX" role="ROLE_BB" />

        </dns:user-data>            
    </dns:user-generator>
</dns:auth-head>


<!-- UNCOMMENT THIS SECTION-->
<!-- 
<bean id="authHead" class="org.xx.providers"> 
    <property name="providers">
        <list>
            <ref bean="AuthProvider"/>
        </list>
    </property>
</bean>
-->
</providersbeans>

我尝试了一些解决方案,但没有成功,因为它有一个前缀.我为了评论而改编的代码之一是这样的:

I tried some of the solutions but couldn't get success since it has a prefix in it. One of the code that I adapted for just commenting is this:

$getxmlpath='C:\Powershell\securityfile.xml'
$xml=[xml](Get-Content $getxmlpath)
$xml.SelectNodes("//auth-head") | ForEach-Object # used with prefix as well, but didnt work
{ 
  $var= $_;
  $mycomment = $xml.CreateComment($var.OuterXml);
  $var.ParentNode.ReplaceChild($mycomment , $var);
}
$xml.Save($getxmlpath);

推荐答案

这样的事情可能会奏效:

Something like this might work:

[xml]$xml = Get-Content 'C:\Powershell\securityfile.xml'

# create namespace manager
$nsm = New-Object Xml.XmlNamespaceManager($xml.NameTable)
$nsm.AddNamespace('dns', $xml.DocumentElement.dns)

# remove nested comments from <auth-head> node(s)
($xml.SelectNodes('//dns:auth-head//comment()', $nsm)) | % {
  [void]$_.ParentNode.RemoveChild($_)
}
# comment-out node(s)
($xml.SelectNodes('//dns:auth-head', $nsm)) | % {
  $comment = $xml.CreateComment($_.OuterXml)
  [void]$_.ParentNode.ReplaceChild($comment, $_)
}

# uncomment <bean> node(s)
($xml.SelectNodes('//comment()')) | ? {
  $_.InnerText -like '*<bean*'
} | % {
  $newxml = [xml]$_.InnerText
  $node = $xml.ImportNode($newxml.DocumentElement, $true)
  $node.SetAttribute('xmlns', $xml.DocumentElement.NamespaceURI)
  [void]$_.ParentNode.ReplaceChild($node, $_)
}

如果没有获得显式(空)命名空间属性 (xmlns=""),我无法找到重新插入 <bean> 节点的方法,所以我将该属性设置为 XML 文档的默认命名空间.

I was unable to find a way to re-insert the <bean> node without it getting an explict (empty) namespace attribute (xmlns=""), so I set that attribute to the default namespace of the XML document.

请注意,您必须从要注释掉的节点中删除(或以其他方式修改)嵌套的注释.否则,第一个嵌套注释中的结束 --> 会过早地终止注释节点,从而留下无效的 XML 结构:

Note that you must remove (or otherwise modify) the nested comments from the node you wish to comment out. Otherwise the closing --> from the first nested comment would prematurely terminate the comment node, leaving you with an invalid XML structure:

<!--dns:auth-head alias="authHead">
    <dns:user-generator>    
        <dns:user-data>

            <!-- USER AA -->   # XML comment ends here!
            <dns:user name="AA"
                password="AAAAAAAAAAAAAAAA" role="ROLE_AA" />

            <!-- USER BB -->
            <dns:user name="BB"
                password="XXXXXXXXXXXXXXX" role="ROLE_BB" />

        </dns:user-data>       # tags from here on are invalid, because
    </dns:user-generator>      # they're missing their respective opening tag
</dns:auth-head-->

这篇关于注释 XML 部分(在节点中有前缀)并取消注释其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆