Class XPath (Excel VBA)

The class XPath represents an XPath that has been mapped to a Range or ListColumn object.

Clear

Clears all XPath schema information for the mapped range.

Clear affects the entire range mapped to the XPath object. This method does not clear the data from the cells mapped to the specified XPath. Use the Clear method of the Range object to clear the data from the cells. If the specified XPath is mapped in an XML list, the schema mapping is removed, but the list is not deleted from the worksheet. If the mapped range is a single-cell, the single-cell is removed and the data remains. This method produces an error if any of the following conditions are true:

Map

Returns an XmlMap object that represents the schema map that contains the specified XPath object.

Parent

Returns the parent object for the specified object. Read-only.

Repeating

Returns True if the specified XPath object is mapped to an XML list; returns False if the XPath object is mapped to a single cell.

SetValue

Maps the specified XPath object to a ListColumn object or Range collection. If the XPath object has previously been mapped to the ListColumn object or Range collection, the SetValue method sets the properties of the XPath object.

See the IsExportable property of the XMLMap object for a discussion on XPath support in Excel. If the XPath expression is invalid or if the XPath specified has already been mapped, a run-time error occurs. If Excel cannot resolve the namespace, a run-time error occurs. This method produces an error if any of the following conditions are true:

Syntax : expression.SetValue (Map, XPath, SelectionNamespace, Repeating)

Sub CreateXMLList() 
    Dim mapContact As XmlMap 
    Dim strXPath As String 
    Dim lstContacts As ListObject 
    Dim objNewCol As ListColumn 
 
    ' Specify the schema map to use. 
    Set mapContact = ActiveWorkbook.XmlMaps("Contacts") 
     
    ' Create a new list. 
    Set lstContacts = ActiveSheet.ListObjects.Add 
         
    ' Specify the first element to map. 
    strXPath = "/Root/Person/FirstName" 
    ' Map the element. 
    lstContacts.ListColumns(1).XPath.SetValue mapContact, strXPath 
 
    ' Specify the second element to map. 
    strXPath = "/Root/Person/LastName" 
    ' Add a column to the list. 
    Set objNewCol = lstContacts.ListColumns.Add 
    ' Map the element. 
    objNewCol.XPath.SetValue mapContact, strXPath 
 
    strXPath = "/Root/Person/Address/Zip" 
    Set objNewCol = lstContacts.ListColumns.Add 
    objNewCol.XPath.SetValue mapContact, strXPath 
End Sub

Arguments

The following arguments are required:

Map (XmlMap) - The map info that the mapped range will be associated with.

XPath (String) - A valid XPath expression that tells Excel what XML data should appear in this mapped range. The XPath string can also contain valid filters, in which case, only a subset of the data that the XPath points to will ever appear in this mapped range.

The following arguments are optional

SelectionNamespace (XPath) - Specifies any namespace prefixes used in the XPath argument. This argument can be omitted if the XPath object doesn't contain any prefixes, or if the XPath object uses the Microsoft Excel internal prefixes.

Repeating (XPath) - Specifies whether the XPath object is to be bound to a column in an XML list or mapped to a single cell. Set to True to bind the XPath object to a column in an XML list. False forces a non-repeating cell to be created. If the range is greater than a single cell and False is specified, a run-time error occurs.

Value

Returns a String that represents the XPath for the specified object.