2

I've got this XML - it's an Excel Ribbon's _rels file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
<Relationship Id="cuID14" Type="http://schemas.microsoft.com/office/2007/relationships/ui/extensibility" Target="customUI/customUI14.xml"/>
</Relationships>

I'm trying to use SelectSingleNode to check if the last Relationship element exists, the one with an ID of "cuID14", by checking its Type attribute. I have code that works if the Relationships element doesn't have the xmlns attribute, i.e., if I change the second line to just Relationships>. I'm trying to do it right though and just can't get the syntax to work. Here's my code with the SelectSingleNode function that works without the xmlns complication:

Sub CheckForAttribute()
Dim oXMLDoc As MSXML2.DOMDocument60
Dim oXMLElement As MSXML2.IXMLDOMElement
Dim XmlRelsNamespace As String

XmlRelsNamespace = "xmlns:rels='http://schemas.openxmlformats.org/package/2006/relationships'"
Set oXMLDoc = New MSXML2.DOMDocument60
oXMLDoc.SetProperty "SelectionNamespaces", XmlRelsNamespace
oXMLDoc.Load "C:\Users\doug\XPATH_TESTER.xml"
'The following line works if the Relationships element to just <Relationships>
Set oXMLElement = oXMLDoc.SelectSingleNode("//Relationship[@Type='http://schemas.microsoft.com/office/2007/relationships/ui/extensibility']")
End Sub

I've found lots of examples but nothing that uses the attributes with a defined namespace. I've tried things like oXMLDoc.SelectSingleNode("//rels:Relationship[@rels:Type='http:...

Community
  • 1
  • 1
Doug Glancy
  • 27,214
  • 6
  • 67
  • 115

1 Answers1

2

You were very close with your second attempt.

Instead of

//rels:Relationship[@rels:Type='http:...`

you should use

//rels:Relationship[@Type='http:...

because attributes do not fall under the default namespace -- just elements.

See also:

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    Thanks! I could swear that's the first thing I tried. It was the obvious choice once I'd navigated the byzantine route to get this far (new to XML/XPATH coding :-)). Yet, now it works and I can get on my whatever I was trying to do before this rabbit hole. Much appreciated! – Doug Glancy Nov 20 '17 at 04:14