xpath无效,因为xpath语法不正确或excel不支持 - 以编程方式映射模式

时间:2014-04-02 04:37:35

标签: xml excel-vba xpath mapping xsd

我一直在关注将XML数据转换为模式的教程,方法是将XML数据读入字符串: http://msdn.microsoft.com/en-us/library/gg469857(v=office.14).aspx

然后使用本教程导入XML数据和XML Schema以创建映射列表: http://msdn.microsoft.com/en-us/library/gg469859(v=office.14).aspx

我使用他们提供的XML数据试用了代码并且它可以工作。 他们的XML数据:

<?xml version='1.0'?>
<BookInfo>
   <Book>
      <ISBN>989-0-487-04641-2</ISBN>
      <Title>My World</Title>
      <Author>Nancy Davolio</Author>
      <Quantity>121</Quantity>
   </Book>
   <Book>
      <ISBN>981-0-776-05541-0</ISBN>
      <Title>Get Connected</Title>
      <Author>Janet Leverling</Author>
      <Quantity>435</Quantity>
   </Book>
   <Book>
      <ISBN>999-1-543-02345-2</ISBN>
      <Title>Honesty</Title>
      <Author>Robert Fuller</Author>
      <Quantity>315</Quantity>
   </Book>
</BookInfo>

我的XML数据:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<DEFTABLE>

  <TABLE DATACENTER="XXXX" LAST_UPLOAD="XXXX" TABLE_NAME="XXXX" USED_BY_CODE="XXXX">
</TABLE>

</DEFTABLE>

我的代码是:

Sub CreateXMLList()
   Dim oMyMap As XmlMap
   Dim strXPath As String
   Dim oMyList As ListObject
   Dim oMyNewColumn As ListColumn

   ' Add a schema map.
   ' ThisWorkbook.XmlMaps.Add (ThisWorkbook.Path & "\Myschema.xsd")
   ThisWorkbook.XmlMaps.Add ("G:\Sharepoint\EMSCHEDULES\ALL_PROD_EM_SCHEDULES_140331.xsd")

   ' Identify the target schema map.
   Set oMyMap = ThisWorkbook.XmlMaps("DEFTABLE_Map1")

   ' Create a new list in A1.
   Range("A1").Select
   Set oMyList = ActiveSheet.ListObjects.Add

   ' Find the first element to map.
   strXPath = "/DEFTABLE/TABLE/TABLE_NAME"
   ' Map the element.
   oMyList.ListColumns(1).XPath.SetValue oMyMap, strXPath


   ' Give the columns logical names
   oMyList.ListColumns(1).Name = "Table Name"

我收到以下错误: xpath无效,因为xpath语法不正确或不受excel支持 在线:

oMyNewColumn.XPath.SetValue oMyMap, strXPath

有人可以解释为什么它不起作用吗?

1 个答案:

答案 0 :(得分:1)

(如评论中所述)

似乎XPath被拒绝了,因为它对您尝试查询的数据不正确,并且可能与您提供的XSD不匹配。在这种情况下,正确的XPath将是:

/DEFTABLE/TABLE/@TABLE_NAME