Namespace on XQuery extracted XML fragment
Added by Anonymous over 16 years ago
Legacy ID: #4908164 Legacy Poster: Simon Robinson (quitesimplysi)
I am using XQuery to extract a small part of a larger XML file that I wish to then render for display using XSLT. There is a namespace defined on the root element of the source XML document, however when I extract the fragment and run this through a transformation SAX parse, I am getting the following error: The prefix "tv" for attribute "tv:linktarget" associated with an element type "xref" is not bound. This is due to the namespace from the root of the source XML document not being present on the root of the fragment I am formatting. Is there some way I can force this namespace declaration onto the root of the extracted fragment, either in the XQuery that extracts it, or in a SAX parse run before or after the extraction? I have tried various methods (such as declaring a dummy attribute on the extracted root using the required namespace, calling startPrefixMapping within the post extraction SAX parsers) but none of this seems to change anything! Many thanks for any help.
Replies (6)
Please register to reply
RE: Namespace on XQuery extracted XML fragmen - Added by Anonymous over 16 years ago
Legacy ID: #4908283 Legacy Poster: Michael Kay (mhkay)
When you extract a fragment of a larger document, Saxon should ensure that all prefixes used within the fragment are properly declared within that fragment. Could you please explain a bit more precisely what you did, preferable in the form of code that I can run to demonstrate the problem? Michael Kay http://www.saxonica.com/
RE: Namespace on XQuery extracted XML fragment - Added by Anonymous over 16 years ago
Legacy ID: #4910546 Legacy Poster: Simon Robinson (quitesimplysi)
Okay, I have made the issue occur with a simplified version of the data I am working with. Here is a small example XML file I have been testing with: <?xml version="1.0" encoding="UTF-8"?> <doc xmlns:tv="http://www.tv-namespace.com\"> <front>Test</front> <body> <figure> <graphic id="GRA1"/> <title>A Title</title> <legend id="LEG1"> <content> <xref xrefid="FIG-1" tv:linkdisplay="Fig. 1"/> </content> </legend> </figure> </body> </doc> I am binding this document to an XQuery, and also binding a targetid string used to identify the ID I wish to extract. The following is a simplified version of the XQuery I am running over it: declare variable $xmlfile as document-node(element(*, xs:untyped)) external; declare variable $targetid as xs:string external; $xmlfile//figure/legend The Output of this XQuery is the serialised into a StringWriter, and has the following output: <?xml version="1.0" encoding="ASCII"?> <legend id="LEG1"> <content> <xref xrefid="FIG-1" tv:linkdisplay="Fig. 1"/> </content> </legend> When this is later put through a SAX parse it errors as there is no declaration of the tv namespace.
RE: Namespace on XQuery extracted XML fragment - Added by Anonymous over 16 years ago
Legacy ID: #4910551 Legacy Poster: Simon Robinson (quitesimplysi)
My apologies, ignore the escape characters in the original XML.
RE: Namespace on XQuery extracted XML fragmen - Added by Anonymous over 16 years ago
Legacy ID: #4910698 Legacy Poster: Michael Kay (mhkay)
When I run this from the command line it works OK, producing the output <?xml version="1.0" encoding="UTF-8"?> <legend xmlns:tv="http://www.tv-namespace.com" id="LEG1"> <content> <xref xrefid="FIG-1" tv:linkdisplay="Fig. 1"/> </content> </legend> The command line is this: java -cp e:\saxon-build\9.0.0.4\bj\saxon9.jar net.sf.saxon.Query e:\temp\test.xq +xmlfile=e:\temp\test.xml targetid=LEG1 So I think the problem must have something to do with the way you are running the query, and in particular, the way that you are serializing the result. Please show the code that you use to evaluate the query and output its results. Michael Kay http://www.saxonica.com/
RE: Namespace on XQuery extracted XML fragment - Added by Anonymous over 16 years ago
Legacy ID: #4910873 Legacy Poster: Simon Robinson (quitesimplysi)
On close inspection of my code I realised that I was calling XQResultSequence.getItem() and then XQItem.writeItem() to serialise out the result. Having changed this to output from the sequence directly, i.e. XQResultSequence.writeSequence(), the namespace is now appearing correctly. I am not sure whether the way I was using this before was incorrect, but the results of this modification have effected the result I was after - many thanks!
RE: Namespace on XQuery extracted XML fragmen - Added by Anonymous over 16 years ago
Legacy ID: #4910911 Legacy Poster: Michael Kay (mhkay)
OK, thanks: I've raised a bug entry for this and published a patch. https://sourceforge.net/tracker/index.php?func=detail&aid=1944867&group_id=29872&atid=397617
Please register to reply