Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...