Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

xml - create new node <xi:include>

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

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="" />

The problems are:

  1. I need xi:include but it starts with include.
  2. I am getting xmlns="", which I don't need.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Also see this related question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...