Forum Discussion

thebick's avatar
thebick
Occasional Contributor
5 years ago

SelectSingleNode not finding XML nodes; trying to setProperty("SelectionNamespaces", ...)

I am trying to use SelectSingleNode() to find any node other than the root for this XML:  

 

<?xml version="1.0?>
<myRootNode xmlns:xsl="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="my:set:of:coloned:stuff">
  <level1Node1>
    <level2Node1>
      <level3Node1>L3N1Value</level3Node1>
    </level2Node1>
    <level2Node2>
      <level3Node1>L3N1Value</level3Node1>
    </level2Node2>
  </level1Node1>
  <level1Node2>
    <level2Node1>
      <level3Node1>L3N1Value</level3Node1>
    </level2Node1>
  </level1Node2>
</myRootNode>

 

 After properly loading the doc and assigning root=doc.documentElement, I am trying

 

L3N1 = root.SelectSingleNode("/level1Node1")

 

L3N1 returns None. The only way anything non-None returns is with "/*" or "//*" -- which return the root.

Maybe it's because the start has "xmlns" stuff in it. So now I'm trying to set a namespace using the value that I find in root.namespaceURI

 

doc.setProperty('SelectionNamespaces', 'xmlns="my:set:of:coloned:stuff"')

 

but that doesn't work either.

What is the correct incantation for this?

  • Finally figured out how the SelectionNamespaces stuff works:  

    doc.setProperty("SelectionNamespaces", "xmlns:<your_abbreviation>=<your_namespace>"

    which can then be used as

    nodeToFind = parentNode.SelectSingleNode("<your_abbreviation>:<tag1>/<your_abbreviation>:<tag2>")

    I need "<your_abbreviation>:" at all levels, because the namespace is at the root. In my case

    doc.setProperty('SelectionNamespaces', 'xmlns:abbrev="my:set:of:coloned:stuff"')
    L3N1 = root.SelectSingleNode("abbrev:level1Node1")

    works, adding "abbrev" and dropping the leading "/"

  • thebick's avatar
    thebick
    Occasional Contributor

    Finally figured out how the SelectionNamespaces stuff works:  

    doc.setProperty("SelectionNamespaces", "xmlns:<your_abbreviation>=<your_namespace>"

    which can then be used as

    nodeToFind = parentNode.SelectSingleNode("<your_abbreviation>:<tag1>/<your_abbreviation>:<tag2>")

    I need "<your_abbreviation>:" at all levels, because the namespace is at the root. In my case

    doc.setProperty('SelectionNamespaces', 'xmlns:abbrev="my:set:of:coloned:stuff"')
    L3N1 = root.SelectSingleNode("abbrev:level1Node1")

    works, adding "abbrev" and dropping the leading "/"

    • thebick's avatar
      thebick
      Occasional Contributor

      Already read that. It doesn't address the problem of the namespace.