Project

Profile

Help

Easiest/Best way to add namespace prefixes

Added by Anonymous over 15 years ago

Legacy ID: #5676575 Legacy Poster: David Lee (daldei)

Using S9API, XPath and XQuery. What is the recommended easiest way to add the namespace prefixes which are contained in the context so that queries can use those namespaces. Example: suppose I have <t:test xmlns:t="http://test.org/test"> <t:title>Test</t:title> </t:test> And I would like to do a xpath or xquery of "//t:title" I know I'm wrong, but it seems the "obvious thing" would be that it just would work. That the namespace prefixes in the document could be matched with the xpath. But ... of course I'm wrong :) The way I can find from the API is that I have to call declareNamespace("t","http://test.org/test"); prior to compiling the expression (xquery or xpath). Or in XQuery of course I can use the declare namespace syntax. But, IMHO, the typical use case is that I want (the users of my program) to be able to juse use the namespace declarations already in the XML document rather then have to pre-declare them. Is there a way to do this ? The way I can think of off hand, but I'm sure its got holes, is to pre-parse the document, and somehow extract the namespace bindings then pre-declare those. Is there another easier (or more efficient) way like some magic flag to Saxon that says "Just use the namespace bindings in the context by default" I can wish cant I ? -David


Replies (3)

Please register to reply

RE: Easiest/Best way to add namespace prefixes - Added by Anonymous over 15 years ago

Legacy ID: #5676784 Legacy Poster: Michael Kay (mhkay)

One can see that for very simple use cases involving a single source document, with all the namespace declared on the root element of that document, copying the namespace declarations from the document element of that document to the static context of the query might make sense. But the general situation is more complicated. I think it would be confusing if it worked one way when you load the source document from the command line, for example, and a different way if you read it using the doc() function. There's also a general principle that the query should continue to work unchanged if the document author decides to use a different namespace prefix. But if you do want to initialize the namespace context in this way, you can use the XPath expression //namespace:: to retrieve the namespace nodes on the outermost element (note this has to be XPath rather than XQuery because XQuery does not support the namespace axis). Then iterate through the resulting namespace nodes, and for each one do QName name = node.getNodeName(); compiler.declareNamespace((name==null ? "" : name.getLocalName()), node.getStringValue()); Michael Kay http://www.saxonica.com/

RE: Easiest/Best way to add namespace prefixes - Added by Anonymous over 15 years ago

Legacy ID: #5683511 Legacy Poster: David Lee (daldei)

I've decided to manage namespaces explicitly without querying the document (for now, but thanks for the code suggestion ... I still may end up doing that) I've implemented the call to declareNamespace on XPath and XQuery but I cant find one for XSLT. Is there an analogous way to pre declare namespaces for XSLT ? Thanks !

RE: Easiest/Best way to add namespace prefixes - Added by Anonymous over 15 years ago

Legacy ID: #5684875 Legacy Poster: Michael Kay (mhkay)

XSLT does not allow namespaces to be declared externally to the stylesheet - they must be declared in the form of XML namespace declarations in the stylesheet itself. Michael Kay

    (1-3/3)

    Please register to reply