首页 > 解决方案 > Excel VB 和 XML 命名空间命名约定

问题描述

假设 gfx 是以下示例中的根元素,xmlns:xsi 和 xsi:noNameSpaceSchemaLocation 是否被视为根的属性,如果是,如何使用 Excel VB 语法分配它们?使用下面的代码,它们出现的顺序是相反的

Dim doc As New MSXML2.DOMDocument60

    Dim root As IXMLDOMElement
    Dim rootAttrib1 As IXMLDOMAttribute, rootAttrib2 As IXMLDOMAttribute

    ' DECLARE ROOT AND CHILDREN '
    Set root = doc.createElement("gfx")
    doc.appendChild root
    
    Set rootAttrib1 = doc.createAttribute("xmlns:xsi")
    rootAttrib1.Value = "http://www.w3.org/2001/XMLSchema-instance"
    root.setAttributeNode rootAttrib1
    
    Set rootAttrib2 = doc.createAttribute("xsi:noNamespaceSchemaLocation")
    rootAttrib2.Value = "Gfx-ME12.xsd"
    root.setAttributeNode rootAttrib2

期望的输出

<?xml version="1.0" encoding="UTF-8"?>
<gfx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Gfx-ME12.xsd">

实际输出

<?xml version="1.0" encoding="UTF-8"?>
<gfx xsi:noNamespaceSchemaLocation="Gfx-ME12.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

标签: excelxml

解决方案


xml 属性的顺序无关紧要。在 xml 信息集中,没有属性顺序之类的东西。


推荐阅读