如何防止输出VBA中的空白xmlns属性

时间:2016-07-30 01:57:28

标签: xml vba dom

晚安:

我正在创建一个功能区,当我创建节点根目录和命名空间时,我将其设置为属性。它在毯子

中重新命名命名空间的下一个节点
Code:
Private Sub crearRibbon()
    Dim RibbonXml As DOMDocument60
    Dim objRaizElem As IXMLDOMElement
    Dim objRibbonElem As IXMLDOMElement
    Dim objPestana As IXMLDOMElement
    Dim objPestanas As IXMLDOMElement
    Dim objGrupo As IXMLDOMElement
    Dim objControl As IXMLDOMElement
    Dim objRaizAtt As IXMLDOMAttribute
    Dim objRibbonAtt As IXMLDOMAttribute
    Dim objPestanaAtt As IXMLDOMAttribute
    Dim objGrupoAtt As IXMLDOMAttribute
    Dim objControlAtt As IXMLDOMAttribute
    Dim strRibbon As String
    Dim x As String
    Dim oNode As MSXML2.IXMLDOMElement


    Set RibbonXml = New DOMDocument60

    'Raiz
    Set objRaizElem = RibbonXml.createElement("customUI")
    RibbonXml.appendChild objRaizElem
    Set objRaizAtt = RibbonXml.createAttribute("xmlns")
    objRaizAtt.Text = ("http://schemas.microsoft.com/office/2006/01/customui")
    objRaizElem.setAttributeNode objRaizAtt

    'Ribbon

    Set objRibbonElem = RibbonXml.createElement("ribbon")
    objRaizElem.appendChild objRibbonElem
    Set objRibbonAtt = RibbonXml.createAttribute("startFromScratch")
    objRibbonAtt.Text = ("True")
    objRibbonElem.setAttributeNode objRibbonAtt
    Set oNode = RibbonXml.selectSingleNode("//ribbon")
    'Set oNode = RibbonXml.getElementsByTagName("/customUI/ribbon/[xmlns]")
    'oNode.removeAttribute ("xmlns")
    oNode.Attributes.removeNamedItem "xmlns"


    'Pestaña
    Set objPestana = RibbonXml.createElement("tabs")
    objRibbonElem.appendChild objPestana

    'Pestañas
    Set objPestanas = RibbonXml.createElement("tab")
    objPestana.appendChild objPestanas
    Set objPestanaAtt = RibbonXml.createAttribute("id")
    objPestanaAtt.Text = ("1")
    objPestanas.setAttributeNode objPestanaAtt
    Set objPestanaAtt = RibbonXml.createAttribute("label")
    objPestanaAtt.Text = ("A Custom Tab")
    objPestanas.setAttributeNode objPestanaAtt
    Set objPestanaAtt = RibbonXml.createAttribute("visible")
    objPestanaAtt.Text = ("true")
    objPestanas.setAttributeNode objPestanaAtt

    'Grupos
    Set objGrupo = RibbonXml.createElement("group")
    objPestanas.appendChild objGrupo
    Set objGrupoAtt = RibbonXml.createAttribute("id")
    objGrupoAtt.Text = ("dbCustomGroup")
    objGrupo.setAttributeNode objGrupoAtt
    Set objGrupoAtt = RibbonXml.createAttribute("label")
    objGrupoAtt.Text = ("A Custom Group")
    objGrupo.setAttributeNode objGrupoAtt

    'Control
    Set objControl = RibbonXml.createElement("control")
    objGrupo.appendChild objControl
    Set objControlAtt = RibbonXml.createAttribute("idMso")
    objControlAtt.Text = ("Paste")
    objControl.setAttributeNode objControlAtt
    Set objControlAtt = RibbonXml.createAttribute("label")
    objControlAtt.Text = ("Built-in Paste")
    objControl.setAttributeNode objControlAtt
    Set objControlAtt = RibbonXml.createAttribute("enabled")
    objControlAtt.Text = ("true")
    objControl.setAttributeNode objControlAtt

    RibbonXml.Save ("miRibbon1.xml")

End Sub

我的输出

 <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon xmlns="" startFromScratch="False">
<tabs>
<tab id="1" label="A Custom Tab" visible="true">
<group id="dbCustomGroup" label="A Custom Group">
<control idMso="Paste" label="Built-in Paste" enabled="true"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>

And ribbon xmlns=""

它只会获得功能区

谢谢

1 个答案:

答案 0 :(得分:0)

已经决定消除空间xmlns =&#34;&#34;而不是使用createElement使用CreateNode我设置名称空间。

现在:

preventDefault()

在:

    Set objPestanas = RibbonXml.createNode(1, "tab", "http://schemas.microsoft.com/office/2006/01/customui")

全部:

Set objPestanas = RibbonXml.createElement("tab")

End Sub

感谢所有