Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
3 replies Latest Post - ‏2012-04-16T21:41:41Z by MatthiasNicola
mudshark
mudshark
2 Posts
ACCEPTED ANSWER

Pinned topic How to save a reference to an XML node

‏2012-04-04T14:37:38Z |
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:

PAGES
name varchar
xhtml xml

Once this table is create a subsequent process will examine the XHTML looking for Javascript. The JS is parsed and an AST, in XML format is created and stored in another table called JS.

JS
xmlnode ????
ast xml

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?

Regards, Keith
Updated on 2012-04-16T21:41:41Z at 2012-04-16T21:41:41Z by MatthiasNicola
  • MatthiasNicola
    MatthiasNicola
    321 Posts
    ACCEPTED ANSWER

    Re: How to save a reference to an XML node

    ‏2012-04-09T07:34:53Z  in response to mudshark
    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.

    Thanks,

    Matthias


    Matthias Nicola
    http://www.tinyurl.com/pureXML
    http://nativexmldatabase.com/
    • mudshark
      mudshark
      2 Posts
      ACCEPTED ANSWER

      Re: How to save a reference to an XML node

      ‏2012-04-16T09:02:25Z  in response to MatthiasNicola
      Hi Matthias,

      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@id=27
      2. Find the button via an explicit, index-based, xpath: /this/that[1]/other[2]/button

      Keith
      • MatthiasNicola
        MatthiasNicola
        321 Posts
        ACCEPTED ANSWER

        Re: How to save a reference to an XML node

        ‏2012-04-16T21:41:41Z  in response to mudshark
        Keith,

        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.

        Matthias


        Matthias Nicola
        http://www.tinyurl.com/pureXML
        http://nativexmldatabase.com/