Forums » Saxon/C Help and Discussions »
SaxonC XdmNode namespace prefix
Added by Paul Greve 2 days ago
We're using SaxonC to parse xml instance documents with namespaces to get an XdmNode. Is there a way to get the namespace prefix for an element in the XdmNode class? I haven't been able to find one. You can get the namespace URI form the fully qualified element name but I don't see where you can get the namespace prefix for an element in the XdmNode model.
Replies (6)
Please register to reply
RE: SaxonC XdmNode namespace prefix - Added by O'Neil Delpratt 2 days ago
Yes this seems like an oversight because SaxonC does not have a QName class like SaxonJ so there is no way to get the namespace prefix.
I have created a bug issue for this #6591 so hopefully we can get a fix out for this in the next maintenance release.
RE: SaxonC XdmNode namespace prefix - Added by Martin Honnen 2 days ago
With the help of XPath prefix-from-QName(node-name($your-node))
you can get the prefix as a workaround as long as there is no QName
equivalent in the SaxonC API, for instance with Python
from saxonche import *
xml1 = '''<root xmlns="http://example.com">
<pf1:foo xmlns:pf1="http://example.com/ns2">test</pf1:foo>
<pf2:bar xmlns:pf2="http://example.com/ns3">test2</pf2:bar>
</root>'''
def get_prefix(xdm_node : PyXdmNode, xpath_processor : PyXPathProcessor):
xpath_processor.set_context(xdm_item=xdm_node)
return xpath_processor.evaluate_single('prefix-from-QName(node-name())')
with PySaxonProcessor() as saxon_processor:
print(saxon_processor.version)
xdm_node = saxon_processor.parse_xml(xml_text=xml1)
xpath_processor = saxon_processor.new_xpath_processor()
xpath_processor.set_context(xdm_item=xdm_node)
elements = xpath_processor.evaluate('//*')
for element in elements:
print(element.name)
print(f'prefix: {get_prefix(element, xpath_processor)}')
RE: SaxonC XdmNode namespace prefix - Added by Paul Greve 2 days ago
Thanks for the workaround Martin. Unfortunately we don't have python on our target system; so we'll have to wait for a fix.
RE: SaxonC XdmNode namespace prefix - Added by O'Neil Delpratt 2 days ago
An equivalent workaround can be done using the C++ API. Same logic really using XPathProcessor
class
RE: SaxonC XdmNode namespace prefix - Added by Paul Greve 2 days ago
Oh. Ok. Thanks O'Neil. I'll give it a try.
RE: SaxonC XdmNode namespace prefix - Added by Paul Greve 1 day ago
I was able to put together a simple C++ test program (based on Martin's python example) that gets the namespace prefixes for all the elements in a document. Thanks for your help on this guys.
Please register to reply