0

Closed the question as it wasn't an error - it just isn't possible

I am trying to return a range that contains all elements in a listobject/xmlmap where a sibling element meets some criteria (a particular elements values are not empty/blank/zero-length).

If I try to return the entire element of the xmlmap, I get the range returned correctly. However as soon as I add any criteria, the range object isn't set and I get an error.

I have removed the namespace refrences throughout, to try and pin down the error, but even without namespace declarations, the error remains. The code below is a much simplified version of what I'm working with, to show the error.

XML data

<?xml version='1.0' encoding='UTF-8'?>
<ROOT>
  <RECORD>
    <ITEMA/>
    <ITEMB>RED</ITEMB>
  </RECORD>
  <RECORD>
    <ITEMA>A</ITEMA>
    <ITEMB>BLUE</ITEMB>
  </RECORD>
  <RECORD>
    <ITEMA>B</ITEMA>
    <ITEMB>GREEN</ITEMB>
  </RECORD>
</ROOT>

XSD used to set up XmlMap in Excel

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="ROOT" type="RootType"/>
<xs:complexType name="RootType">
    <xs:sequence>
        <xs:element name="RECORD" maxOccurs="unbounded">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="ITEMA"/>
                    <xs:element name="ITEMB"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>
</xs:schema>

Working VBA Code (without predicate)

Sub TestSUCCESS()

Dim rngSource As Range
Dim sSourceXPath As String

sSourceXPath = "/ROOT/RECORD/ITEMB"

Set rngSource = Sheet4.XmlDataQuery(sSourceXPath)
Debug.Print rngSource.Address

End Sub

Non-Working VBA Code (with predicate)

Sub TestERROR()

Dim rngSource As Range
Dim sSourceXPath As String

sSourceXPath = "/ROOT/RECORD/ITEMA[text()[normalize-space()]]"

Set rngSource = Sheet4.XmlDataQuery(sSourceXPath)
If Not rngSource Is Nothing Then
    Debug.Print rngSource.Address
Else
    Debug.Print "ERROR: rngSource isn't set"        
End If

End Sub

I have also tried using the following xpath:

"/ROOT/RECORD/ITEMB[../ITEMA[string-length(text()) > 0]]"

As well as variations of the folloving just to get anything to work, but to no avail:

sSourceXPath = "/ROOT/RECORD/ITEMB[../ITEMA[.="A"]]"

All the xPath examples I've given work in Altova XMLSpy 2005.

Can anyone offer any suggestions as to where I've gone wrong ?

4

1 回答 1

0

从 xpaht 的角度来看,您的尝试是可以的。但我更喜欢例如选择 ITEMB 且 TIMEA 不为空。

"/ROOT/RECORD/ITEMB[normalize-space(../ITEMA)]" 

但这一切都不适用于 Worksheet.XmlDataQuery

因为(我最好的猜测):http: //msdn.microsoft.com/en-us/library/office/ff839982.aspx

XmlDataQuery 方法允许您查询特定地图数据的存在。它不能用于查询地图中的一条数据。例如,存在一个映射范围是有效的,该范围的 XPath 是“/root/People[@Age="23"]/FirstName”。对此 XPath 位置路径的 XmlDataQuery 查询将返回正确的范围。但是,希望在上述映射范围内找到“Joe”的“/root/People[FirstName="Joe"]”查询将失败,因为映射范围的 XPath 定义不同。

于 2013-05-09T14:13:19.280 回答