创建新节点 <xi:include> [英] create new node &lt;xi:include&gt;

查看:94
本文介绍了创建新节点 <xi:include>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$club = $xml.CreateElement('xi:include')
$club.SetAttribute('href','barracas')
$lookupNode.AppendChild($club) >$null
$xml.Save($config_filename)

在上面的 PowerShell 片段中,$lookupNode 是我添加新创建的节点 $club 的节点.

In the above PowerShell fragment $lookupNode is the node where I am appending a newly created node $club.

我希望在下面添加一行.

What I expect is to add the line below.

<xi:include href="barracas" />

实际上我得到的是下面的一行.

What actually I get is a line below.

<include href="barracas" xmlns="" />

问题是:

  1. 我需要 xi:include 但它以 include 开头.
  2. 我得到了我不需要的 xmlns="".
  1. I need xi:include but it starts with include.
  2. I am getting xmlns="", which I don't need.

推荐答案

XML 元素中以冒号分隔的前缀表示 命名空间.

A colon-separated prefix in XML elements indicates a namespace.

<foo:bar baz='something'>else</foo:bar>
  ^   ^   ^       ^       ^
  |   |   |       |       `- node value/text
  |   |   |       `- attribute value/text
  |   |   `- attribute name
  |   `- node name
  `- namespace name

您需要一个 命名空间管理器用于处理这些:

You need a namespace manager for handling these:

[Xml.XmlNamespaceManager]$nsm = $xml.NameTable
$nsm.AddNamespace('ns', $xml.DocumentElement.NamespaceURI)
$nsm.AddNamespace('xi', 'http://...')

$club = $xml.CreateElement('xi:include', $ns.LookupNamespace('xi'))
$club.SetAttribute('href', 'barracas')
$xml.DocumentElement.AppendChild($club) >$null

另见这个相关问题.

这篇关于创建新节点 <xi:include>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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