Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath to get xs:element list 1

Status
Not open for further replies.

chiph

Programmer
Jun 9, 1999
9,878
US
I'm trying to parse a .XSD file that's been loaded into a MSXML 4.0 DOM object. I can't seem to come up with an XPath query that will return me any useful nodes.

The xsd is like:
Code:
<xs:schema xmlns:xs=&quot;[URL unfurl="true"]http://www.w3.org/2001/XMLSchema&quot;[/URL] elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;>
  <xs:element name=&quot;RootElement&quot;>
    <xs:complexType>
      <xs:sequence>
        <xs:element name=&quot;UserElements&quot;>
          <xs:complexType>
            <xs:sequence>
              <xs:element name=&quot;E_Code&quot;>
                 more child nodes are here
              </xs:element>
              <xs:element name=&quot;E_Value&quot;>
                 more child nodes are here
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element name=&quot;RootElement&quot;>
</xs:schema>

And some of what I've tried this far is:

Code:
Set objNodeList = objDOM.documentElement.selectNodes(&quot;/descendent::*&quot;)
Returns all gazillion nodes as expected

Code:
Set objNodeList = objDOM.documentElement.selectNodes(&quot;/namespace::*&quot;)
Returns no nodes, when I'd expect at least one that describes the namespace

Code:
Set objNodeList = objDOM.documentElement.selectNodes(&quot;./child::element&quot;)
Returns no nodes, when I would expect a collection of <xs:element> nodes

Code:
Set objNodeList = objDOM.documentElement.selectNodes(&quot;./child::xs:element&quot;)
Blows up with an error: &quot;Reference to undeclared namespace prefix: 'xs'.&quot;

Code:
Set objNodeList = objDOM.documentElement.selectNodes(&quot;./child::xmlns:element&quot;)
Returns no nodes.

Any ideas?

Plan B would be to parse the XSD file via SAX (messy, but doable).

Chip H.
 
This sounds similar to a problem I ran into with selectSingleNode. Path statements worked just fine without a namespace in the document. But when a namespace is declared, nothing is found. The MSDN site on .NET Framework System.Xml has an article that says you must use a different method with namespaces. This method has a second parameter, an XMLNamespaceManager. See
This explained the problem, but there was no JScript code for instantiating the XMLNamespaceManager so I still don't have a solution for myself.
 
Yeah I saw the .NET solution, but I'm stuck with VB6 on this (drat!). I'm waiting to hear back from the office's XML guru on this, but I suspect I might be breaking new ground here :-0

In the meantime, I'm proceeding with SAX...

Thanks for the reply.
Chip H.
 
OK, found the answer:

If you ever have to run an XPath query against a document that uses a namespace, you'll need to set the SelectionNamespaces property first.

Call objDOM.setProperty(&quot;SelectionLanguage&quot;, &quot;XPath&quot;)
Call objDOM.setProperty(&quot;SelectionNamespaces&quot;, &quot;xmlns:xs='Set objNodeList = objDOM.documentElement.selectNodes(&quot;//xs:element[@name=&quot;&quot;UserElements&quot;&quot;]//xs:element&quot;)


Now I can sleep tonight!!
(after worrying about this for two days)

Chip H.
 
Hello Chip,

Good job!

I tried this with selectSingleNode and found that it also works for that method. A couple of fine points I can add. The setProperty(&quot;SelectionLanguage&quot;, &quot;XPath&quot;)
did not make any difference in my simple example, the selectSingleNode worked with or without setting the SelectionLanguage.

And, this is not good, I could not make it work with a default namespace.

In the xml document I have
Code:
<nutshell xmlns=&quot;urn:htd&quot;>
  <nutmeat>
    <nut-name-common>Hazelnut</nut-name-common>
  </nutmeat>
</nutshell>

Then in the script I set the SelectionNamespace property like so.
Code:
xmlSource.setProperty(&quot;SelectionNamespaces&quot;,&quot;xmlns='urn:htd'&quot;);

var nodeSelected = xmlSource.selectSingleNode(&quot;/nutshell/nutmeat/nut-name-common&quot;);

This does not find a node. But if I put a qualifier everywhere then it does find the node.

Thanks for posting the solution to this problem.

Richard
 
Richard -

That makes sense -- if you set the SelectionNamespaces property, it would only find elements that are using that namespace, and the default namespace elements wouldn't be.

An interesting question would be how to set the selection namespace back to the default namespace when you're done. Pass an empty string as the value, perhaps?

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top