xml - 为 XML 指定命名空间的本地相对路径
问题描述
我在引用加载的 XML 文件中使用的架构文件时遇到问题。架构文件位于folder="C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\"
并且因为 XML 文件位于多个子文件夹中,所以我不想将架构文件复制到每个子文件夹中。
我找到的所有示例都与http...
位置有关,任何人都可以帮助我参考本地文件夹吗?
<?xml version="1.0"?>
<SEQUENCE Format="2.0" Name="hardware\bbx" xmlns="x-schema:sequence_schema.xml">
<PASSPORT Frozen="No" Type="Production" AccessModifyGroups="All" AccessDisplayGroups="All"
Version="1.0" ReviseTime="28/11/2018 11:36:13.384" Revisor="Wesley" CreateTime="11/01/2012
17:38:14.765" Creator="Wes" Description="">
<CUSTOM>
<CUSTOM_PARAM Name="SendLotEndSignal" Value="0"/>
</CUSTOM>
</PASSPORT>
<STEPS>
<STEP SeqEndCleanRecipe="NO_RECIPE" Recipe="STANDARD\205C" Chambers="B2B35" SectionName="Steps">
<CUSTOM>
<CUSTOM_PARAM Name="Type" Value="PAAP"/>
<CUSTOM_PARAM Name="Block" Value="B2"/>
</CUSTOM>
</STEP>
<STEP SeqEndCleanRecipe="NO_RECIPE" Recipe="STANDARD\110C" Chambers="B2B14" SectionName="Steps">
<CUSTOM>
<CUSTOM_PARAM Name="Type" Value="PAHP"/>
<CUSTOM_PARAM Name="Block" Value="B2"/>
</CUSTOM>
</STEP>
</STEPS>
</SEQUENCE>
这是我正在使用的代码(更新为全新的代码)。当我将架构文件复制到与 XML 相同的文件夹时,代码可以完美运行,它可以正常工作,但我不想这样做,因为我有很多子文件夹:
Option Explicit
'Sub ReadAllXML(Ffolder As String)
Sub ReadAllXML()
Dim Ffolder As String
Dim FilePath, XMLFileName, RecipeID As String
Dim main_folder As String
Dim oXMLFile, Recipe As Object
Dim Col, NumberOfElements, LastRow As Long
Dim n As Object
Dim RecipeName, ChamberName, UnitKind, BlockName As String
Dim strtxt, Fromtxt, FromPos, PathWithoutSequences, FirstFolder As String
Dim wks As Worksheet
'main folder is the folder where the schema file is stored
main_folder = "C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\"
'Ffolder are different folders where all the XML files are stored
Ffolder = "C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\Subfolder1\subfolder2\"
Set oXMLFile = CreateObject("Microsoft.XMLDOM")
Set wks = ThisWorkbook.Worksheets("Sheet1")
XMLFileName = Dir(Ffolder & "*.xml")
If XMLFileName <> "" Then
FilePath = Ffolder & XMLFileName
' Namespace = "xmlns=x-schema:sequence_schema.xml" "xsi:schemaLocation=""file:///C:\Users\zander\Desktop\20211112094529\Recipe\RECIPEs\Data\Sequences\sequence_schema.xml""
oXMLFile.Load FilePath
'oXMLFile.SetProperty "SelectionLanguage", "XPath"
'oXMLFile.SetProperty "SelectionNamespaces", "xmlns=x-schema:sequence_schema.xml"
'oXMLFile.resolveExternals = False
'oXMLFile.validateOnParse = False
Debug.Print oXMLFile.parseError.ErrorCode & " " & oXMLFile.parseError.reason & " " & oXMLFile.parseError.Line
'Set elements = oXMLFile.getElementsByTagName("STEP")
'RecipeName = oXMLFile.SelectSingleNode("//STEP").Attributes.getNamedItem("Recipe").Text
'BlockName = oXMLFile.SelectSingleNode("//STEP/CUSTOM").Attributes.getNamedItem("CUSTOM_PARAM").Text
Col = 10
NumberOfElements = oXMLFile.getElementsByTagName("STEP").Length
With wks
LastRow = wks.Cells(wks.Rows.Count, "C").End(xlUp).Row + 1
strtxt = FilePath
Fromtxt = "Sequences"
'Totxt = "test"
FromPos = InStr(strtxt, Fromtxt)
'ToPos = InStr(strtxt, Totxt)
'extract the text string between the two words
'ExtractStr = Mid(strtxt, FromPos + Len(Fromtxt), ToPos - FromPos - Len(Fromtxt))
PathWithoutSequences = Right(strtxt, Len(strtxt) - FromPos - 9)
FirstFolder = Left(PathWithoutSequences, Len(PathWithoutSequences) - Len(XMLFileName) - 1)
.Cells(LastRow, 3) = FirstFolder
.Cells(LastRow, 8) = XMLFileName
For Each n In oXMLFile.SelectNodes("//STEP")
RecipeName = n.Attributes.getNamedItem("Recipe").Text
ChamberName = n.Attributes.getNamedItem("Chambers").Text
UnitKind = n.ChildNodes(0).ChildNodes(0).Attributes.getNamedItem("Value").Text
BlockName = n.ChildNodes(0).ChildNodes(1).Attributes.getNamedItem("Value").Text
.Cells(LastRow, Col) = RecipeName
.Cells(LastRow, Col + 1) = ChamberName
.Cells(LastRow, Col + 2) = UnitKind
.Cells(LastRow, Col + 3) = BlockName
Col = Col + 4
Next
End With
End If
End Sub
解决方案
这里有几个关键概念:
xmlns="x-schema:sequence_schema.xml"
不是提示 XSD 位置的方法。schemaLocation
请参阅以下 Q/A 中描述的如何使用和支持材料:您的 XPath 必须考虑默认命名空间(目前
xmlns="x-schema:sequence_schema.xml"
):请参阅此 Q/A 中的 VBA 条目,该条目解释了如何在 XPath 表达式及其托管评估库/工具中指定 XML 命名空间:
推荐阅读
- if-statement - 使用 If 语句页面加载错误有效,但页面加载成功无效
- asp.net-mvc - id 为 x 的进程未运行(Visual Studio 2017)
- algorithm - 从源节点生成图形的有效方法
- python - 通过 PySpark pandas_udf 数据限制?
- javascript - 如何缩小纯 CSS 组件以适应父组件?
- angular - 全新 Angular 8/Electron 5 应用程序出现白屏
- git - 是否可以删除问题讨论中的“userX 添加了对 Z 小时前引用此问题的 repoY 的提交”消息?
- serial-port - 主板串口与usb ftdi芯片的通讯
- java - 哪个函数可以用来显示元素,哪个函数可以用来隐藏它们?
- visual-studio - 我该如何解决这个问题?烦了我很久