How to convert XdmNode to System.Xml.XmlNode
Added by Anonymous over 15 years ago
Legacy ID: #7377803 Legacy Poster: Phanidhar Adusumilli (padusumilli)
I am using saxonb for .Net The intention of the application is to evaluate an xquery expression on the given node. The return value would be a list of atomic values or System.Xml.XmlNode's. for example: the method to call looks like: IList evaluateXqueryExpression(System.Xml.XmlNode node, String expression) { Processor processor = new Processor(); XQueryCompiler compiler = processor.NewXQueryCompiler(); XQueryExecutable executable = compiler.Compile(expression); DocumentBuilder documentBuilder = processor.NewDocumentBuilder(); XQueryEvaluator evaluator = executable.Load(); evaluator.ContextItem = documentBuilder.Build(node); XdmValue queryResult = evaluator.Evaluate(); IList returnValues = new ArrayList(); foreach(XdmItem item in queryResult) { if(item.IsAtomic()) { returnValues.Add(((XdmAtomicValue)item).Value); }else { ??? How do we convert the XdmNode to a System.Xml.XmlNode here ??? } } return returnValues; }
Replies (5)
Please register to reply
RE: How to convert XdmNode to System.Xml.XmlNode - Added by Anonymous over 15 years ago
Legacy ID: #7377859 Legacy Poster: Michael Kay (mhkay)
If the query selects nodes from the input document, then the returned XdmNode will wrap (ultimately) the XmlNode. In this case the property item.Implementation will be a net.sf.saxon.om.VirtualNode, whose getUnderlyingNode() will return the original XmlNode. If the query constructs new nodes, the only way to convert them to an XmlNode is to do another query or transformation that has a DomDestination as its output. However, if you ran the main query differently, using the Run() method instead of Evaluate(), then you could arrange for the query output to be written directly to a DomDestination.
RE: How to convert XdmNode to System.Xml.XmlNode - Added by Anonymous over 15 years ago
Legacy ID: #7377931 Legacy Poster: Phanidhar Adusumilli (padusumilli)
In the api the XdmNode.Implementation is declared to be of type net.sf.saxon.om.NodeInfo. It does not have the method getUnderlyingNode. The method getUnderlyingNode is in the interface net.sf.saxon.om.VirtualNode. The instance returned by item.Implementation is of net.sf.saxon.tinytree.TinyElementImpl. This implements net.sf.saxon.om.NodeInfo interface. So how do I get original XmlNode? Am I missing something?
RE: How to convert XdmNode to System.Xml.XmlNode - Added by Anonymous over 15 years ago
Legacy ID: #7378032 Legacy Poster: Michael Kay (mhkay)
If the returned node is a TinyElementImpl, this suggests that the query constructed the node, in which case you should use the Run() method with a DomDestination to ensure that it is constructed as a DOM node rather than as a Saxon node.
RE: How to convert XdmNode to System.Xml.XmlNode - Added by Anonymous over 15 years ago
Legacy ID: #7378096 Legacy Poster: Phanidhar Adusumilli (padusumilli)
Can you elaborate "query constructed the node"? The expression was simply matching an existing node. for example: <book> <author>...</author> </book> the expression was "//author". Can you explain why evaluation of the expression constructs the node? Is it because of the way Documentbuilder is used. If you have noticed to convert the System.Xml.XmlNode that was passed, we are using DocumentBuilder.Build(XmlNode).
RE: How to convert XdmNode to System.Xml.XmlNode - Added by Anonymous over 15 years ago
Legacy ID: #7378150 Legacy Poster: Michael Kay (mhkay)
Thanks for the extra info. The query //author doesn't construct any nodes, it returns nodes that were present in the input. But when you did DocumentBuilder.Build(XmlNode) you copied the DOM to create a Saxon tree. Try using DocumentBuilder.Wrap() instead. Evaluate should then return XdmNode instances that wrap VirtualNode instances that in turn wrap the original XmlNode objects.
Please register to reply