PowerShell:附加XML元素会在同一节点上重写现有元素吗? (办公室) [英] PowerShell: Appending an XML element re-writes existing elements at the same node? (Office)

查看:73
本文介绍了PowerShell:附加XML元素会在同一节点上重写现有元素吗? (办公室)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这不是特定于我正在使用的XML,但为了以防万一,我使用PowerShell脚本来修改Office 2010功能区的内容。  我使用类似于以下内容的代码段将控件添加到自定义组:

 $ OfficeUI = New-Object XML 
$ UIFile = $ ENV:LOCALAPPDATA + '\Microsoft\Office\olkmailitem.officeui'
$ OfficeUI.Load($ UIFILE)
$ NSM =新物体System.Xml.XmlNamespaceManager $ OfficeUI.NameTable
$ NSM.AddNamespace('mso','http://schemas.microsoft.com/office/2009/07/customui')
$ mso = $ NSM.LookupNamespace('mso')
$ TAB = $ OfficeUI.customUI.ribbon.tabs.tab | ?{$ _.idQ -like'mso:TabInsert'}
$ IncludeGroup = $ TAB.group | ? {$ _ ID样 'MyCustomInsertInclude'}
$按钮= $ OfficeUI.CreateElement( 'MSO:控制',$ MSO)
$ ATTR = $ OfficeUI.CreateAttribute( 'IDQ')
$ attr.value ='mso:CalendarButton'
$ QUIET = $ Button.Attributes.Append($ attr)
$ attr = $ OfficeUI.CreateAttribute('visible')
$ QUIET = $ Button.Attributes.Append($ attr)
$ QUIET = $ IncludeGroup.AppendChild($ Button)

this完全符合预期,并给我这一大块XML(只是相关部分:


  < mso:tabs>

   < ; MSO:标签IDQ = QUOT; MSO:TabInsert">

&NBSP;&NBSP;&NBSP;&NBSP;< MSO:组IDQ = QUOT; MSO:GroupInclude"可见= QUOT假QUOT; / >

&NBSP;&NBSP;&NBSP;&NBSP;< MSO:组ID = QUOT; MyCustomInsertInclude"标签= QUOT;包含" insertBeforeQ = QUOT; MSO:GroupInsertTables" AUTOSCALE = QUOT;真"> ; $
     < mso:control idQ =" mso :CalendarButton"可见= QUOT;真" />

  &NBSP; &NBSP; &NBSP; < / mso:group>

  &NBSP; &NBSP; < / MSO:标签>


但是,我想添加一些额外的控件:

 $ Button2 = $ OfficeUI。 CreateElement('mso:control',$ mso)
$ attr = $ OfficeUI.CreateAttribute('idQ')
$ attr.value ='mso:AttachFile'
$ QUIET = $ Button2 .Attributes.Append($ ATTR)
$ ATTR = $ OfficeUI.CreateAttribute( '可见')
$ QUIET = $ Button2.Attributes.Append($ ATTR)
$ QUIET = $ IncludeGroup .AppendChild($ Button2)

这导致以下XML片段:


  &NBSP; < mso:tabs>

  &NBSP; &NBSP; < mso:tab idQ =" mso:TabInsert">

  &NBSP; &NBSP; &NBSP; < mso:group idQ =" mso:GroupInclude"可见= QUOT假QUOT; />

  &NBSP; &NBSP; &NBSP; < mso:group id =" MyCustomInsertInclude"标签= QUOT;包含" insertBeforeQ = QUOT; MSO:GroupInsertTables" autoScale =" true">

  &NBSP; &NBSP; &NBSP; &NBSP; < mso:control idQ =" mso:AttachFile"可见= QUOT;真" />

  &NBSP; &NBSP; &NBSP; &NBSP; < mso:control idQ =" mso:AttachFile"可见= QUOT;真" />

  &NBSP; &NBSP; &NBSP; < / mso:group>

  &NBSP; &NBSP; < / MSO:标签>


Errrr ....嗯?  为什么会这样?  我添加的控件数量无关紧要,所有控件都添加了最后一个控件的属性。  奇怪的是,如果我编写一个SEPARATE脚本来添加下一个控件也没关系....我添加的组中的所有控件
都被重写为我添加的控件。


这是基本的,我只是关于Powershell,XML或两者兼而有之吗?



解决方案


我看了这个,发现那个MS似乎有一个糟糕的架构。 属性添加错误。 我之前看到过这种情况,当时MS不希望我们编辑这些文件,或者解决问题的应用程序提供了另一个命名空间。 
这可能允许应用程序在基本架构上创建变体。


在Office 2016 OneClick上,它表现得更糟。


我建议在Office Developers论坛中发帖。 他们可能知道这里的诀窍。



如果我运行你的代码,我会得到以下结果:


< pre class ="prettyprint">< mso:customUI xmlns:mso =" http://schemas.microsoft.com/office/2009/07/customui">
< mso:ribbon>
< mso:qat />
< mso:tabs>
< mso:tab idQ =" mso:TabMail">
< mso:tab idQ =" mso:TabInsert">
< mso:group idQ =" mso:GroupInclude"可见= QUOT假QUOT; />
< mso:group id =" MyCustomInsertInclude"标签= QUOT;包含" insertBeforeQ = QUOT; MSO:GroupInsertTables"自动缩放= QUOT;真">
< mso:control idQ =" mso:CalendarButton"可见= QUOT;真" />
< mso:control idQ =" mso:AttachFile"的xmlns:MSO = QUOT; xmlns xml mso" />
< / mso:group>
< / mso:tab>
< mso:group idQ =" mso:GroupOfficeApps"可见= QUOT假QUOT; />
< / mso:tab>
< / mso:tabs>
< / mso:ribbon>
< / mso:customUI>

请注意废话:  < mso:control idQ =" mso:AttachFile"的的xmlns:MSO = QUOT; xmlns xml mso" />


这是一个xml片段,旨在加载到更大的XML片段中,并包含在其他命名空间中。




I don't think that this is specific to the XML I'm working with, but just in case, I'm using PowerShell scripts to modify the contents of Office 2010 ribbons.  I use a snippet similar to the below to add a control to a custom group:

$OfficeUI= New-Object XML
$UIFile=$env:localappdata + '\Microsoft\Office\olkmailitem.officeui'
$OfficeUI.Load($UIFile)
$NSM=New-Object System.Xml.XmlNamespaceManager $OfficeUI.NameTable
$NSM.AddNamespace('mso','http://schemas.microsoft.com/office/2009/07/customui')
$mso=$NSM.LookupNamespace('mso')
$TAB=$OfficeUI.customUI.ribbon.tabs.tab | ?{$_.idQ -like 'mso:TabInsert'}
$IncludeGroup=$TAB.group | ? {$_.id -like 'MyCustomInsertInclude'}
$Button=$OfficeUI.CreateElement('mso:control',$mso)
$attr=$OfficeUI.CreateAttribute('idQ')
$attr.value='mso:CalendarButton'
$QUIET=$Button.Attributes.Append($attr)
$attr=$OfficeUI.CreateAttribute('visible')
$QUIET=$Button.Attributes.Append($attr)
$QUIET=$IncludeGroup.AppendChild($Button)

This works exactly as expected and gives me this chunk of XML (just the relevant section:

    <mso:tabs>
      <mso:tab idQ="mso:TabInsert">
        <mso:group idQ="mso:GroupInclude" visible="false" />
        <mso:group id="MyCustomInsertInclude" label="Include" insertBeforeQ="mso:GroupInsertTables" autoScale="true">
          <mso:control idQ="mso:CalendarButton" visible="true" />
        </mso:group>
      </mso:tab>

However, I want to add some additional controls:

$Button2=$OfficeUI.CreateElement('mso:control',$mso)
$attr=$OfficeUI.CreateAttribute('idQ')
$attr.value='mso:AttachFile'
$QUIET=$Button2.Attributes.Append($attr)
$attr=$OfficeUI.CreateAttribute('visible')
$QUIET=$Button2.Attributes.Append($attr)
$QUIET=$IncludeGroup.AppendChild($Button2)

This results in the following XML snippet:

    <mso:tabs>
      <mso:tab idQ="mso:TabInsert">
        <mso:group idQ="mso:GroupInclude" visible="false" />
        <mso:group id="MyCustomInsertInclude" label="Include" insertBeforeQ="mso:GroupInsertTables" autoScale="true">
          <mso:control idQ="mso:AttachFile" visible="true" />
          <mso:control idQ="mso:AttachFile" visible="true" />
        </mso:group>
      </mso:tab>

Errrr....huh?  Why did THAT happen?  It doesn't matter how many controls I add, all of them have the attributes of the last one added.  Oddly, it also doesn't matter if I write a SEPARATE script to add the next control....all of the controls in the group I add to are re-written as the control I added.

Is  this something basic I just dunno about Powershell, XML, or both?

解决方案

I looked at this and find that MS seems to have a bad schema.  The attributes add incorrectly.  I have seen this before when MS deosn't want us to edit these files or there is another namespace tht is supplied by the application that resolves issues.  This may allow the app to create variations on the base schema.

On Office 2016 OneClick it behaves even worse.

I suggest posting in the Office Developers forum.  They may know what the trick is here.

If I run your code I get the following:

<mso:customUI xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">
  <mso:ribbon>
    <mso:qat />
    <mso:tabs>
      <mso:tab idQ="mso:TabMail">
        <mso:tab idQ="mso:TabInsert">
          <mso:group idQ="mso:GroupInclude" visible="false" />
          <mso:group id="MyCustomInsertInclude" label="Include" insertBeforeQ="mso:GroupInsertTables" autoScale="true">
            <mso:control idQ="mso:CalendarButton" visible="true" />
            <mso:control idQ="mso:AttachFile" xmlns:mso=" xmlns xml mso" />
          </mso:group>
        </mso:tab>
        <mso:group idQ="mso:GroupOfficeApps" visible="false" />
      </mso:tab>
    </mso:tabs>
  </mso:ribbon>
</mso:customUI>

Note the crap:  <mso:control idQ="mso:AttachFile" xmlns:mso=" xmlns xml mso" />

This is an xml fragment that is intended to be loaded into a larger piece of XML and enclosed in other namespaces.


这篇关于PowerShell:附加XML元素会在同一节点上重写现有元素吗? (办公室)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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