What is the best way to save a reference to an XML node in a relational table?
The scenario is that the XHTML of a web page is stored in a PAGES table:
In the JS table I would like to store a reference to the node in the xhtml where the original JS came from.
eg If the JS is the onClick event of a button I need a reference to the button node in the xhtml.
I need this info so that I can do queries on the JS AST and then include information about the parent (or parent of parent or siblings etc) xhtml node in the result set.
Can anyone suggest ways of doing this?
My thoughts are:
1. Determine an xpath for the parent of the JS node and store this as the reference to the JS node.
How would I use a saved xpath expression in xquery?
How could I build an index of these nodes for faster access?
2. Modify the XHTML so that each JS parent node has a new jsnodeid attribute that I would populate. Store the jsnodeid as the reference.
Building an index would be easy, //@jsnodeid.
3. Can I use DB2s internal node id's?
4. Any other suggestions?
Pinned topic How to save a reference to an XML node
Answered question This question has been answered.
Unanswered question This question has not been answered yet.
Updated on 2012-04-16T21:41:41Z at 2012-04-16T21:41:41Z by MatthiasNicola
MatthiasNicola 120000E28R322 Posts
Re: How to save a reference to an XML node2012-04-09T07:34:53ZThis is the accepted answer. This is the accepted answer.Hi Keith,
this sounds like a quite involved use case and I might not be able to provide a complete solution. But, I can offer a few ideas and bits and pieces that may or may not be helpful.
If you need to remember the location of an XML element, you can certainly store the XPath to that node. The stored procedure below shows how you can use an XPath in a query.
The procedure takes two XPath expressions as input, one to extract information from XML documents, the other to filter the XML documents with a predicate. The stored procedure plugs these XPath expressions into an SQL/XML statement, which is then dynamically prepared and executed with a cursor. The procedure leaves the cursor open, which allows the caller, such as your Java application or another stored procedure, to iterate over the result set of the query.
CREATE TABLE customer(info XML)# CREATE PROCEDURE dynXMLquery (IN XPathExtract VARCHAR(1024), IN XPathFilter VARCHAR(1024) ) LANGUAGE SQL BEGIN ATOMIC DECLARE sql VARCHAR(2048); DECLARE c1 CURSOR with return to caller FOR stmt; SET sql= 'SELECT XMLQUERY(' ' $INFO'|| XPathExtract || ' ' ' ) FROM customer WHERE XMLEXISTS( '' $INFO '|| XPathFilter || ' '' ) '; PREPARE stmt FROM sql; OPEN c1 ; END #
The following CALL statement executes the procedure to extract the <name> elements of the customers living in Aurora:
call dynXMLquery( '/customerinfo/name', '/customerinfo/addr[city="Aurora"]' )
The call statement can also grab the XPath (that is used as input parameter) from another table, such as in this example:
call dynXMLquery( (SELECT path FROM js WHERE id = 3), '/customerinfo/addr[city="Aurora"]' )
Certainly, modifying all the original document to insert special attributes as markers is another option, but might be more work. But, I know too little about your scenario to really judge the pros and cons of these two approaches.
DB2's internal node IDs are not externalized and cannot be used for this purpose.
mudshark 110000D4TR2 Posts
Re: How to save a reference to an XML node2012-04-16T09:02:25ZThis is the accepted answer. This is the accepted answer.
- MatthiasNicola 120000E28R
Thanks for your suggestions and the procedure.
I tried the option of modifying the xml to add an id and that does work OK.
When it comes to locating a node do you have any idea which of the following xpath's would, in general, perform better, assuming both paths point to the same node?
1. Find the button by id: //button
2. Find the button via an explicit, index-based, xpath: /this/that/other/button
MatthiasNicola 120000E28R322 Posts
Re: How to save a reference to an XML node2012-04-16T21:41:41ZThis is the accepted answer. This is the accepted answer.
- mudshark 110000D4TR
an XPath that starts with "//" can be more expensive than a fully-specified XPath with explicit navigation steps from the document root to the desired node. How much more expensive depends on the size of the document, and how wide and deep the document is.