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

Categories

$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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...