Example 5-11: Test, extract, and retrieve an XPath expression value

SET SERVEROUTPUT ON
DECLARE
  doc       xmldom.DOMDocument;
  approvers xmldom.DOMNodeList;
  PROCEDURE p(msg VARCHAR2, nl BOOLEAN := TRUE) IS BEGIN
    dbms_output.put_line(msg);IF nl THEN dbms_output.put(CHR(10)); END IF;
  END;
  FUNCTION yn(b BOOLEAN ) RETURN VARCHAR2 IS
  BEGIN IF b THEN RETURN 'Yes'; ELSE RETURN 'No'; END IF; END;
BEGIN
  doc := xml.parse(BFileName('XMLFILES','claim77804.xml'));

  p('What is the value of the Policy number for this claim?');
  p( xpath.valueOf(doc,'/Claim/Policy') );

  p('Does this claim have any settlement payments over $500 approved by JCOX?');
  p(yn(xpath.test(doc,'//Settlements/Payment[. > 500 and @Approver="JCOX"]')));

  -- Demonstrate Saving and Re-getting the XML document
  xmldoc.save('claim77804',doc);
  doc := xmldoc.get('claim77804');

  p('What is XML document fragment contained by the <DamageReport> element?');
  p(xpath.extract(doc,'/Claim/DamageReport'));

  p('Who approved settlement payments for this claim?');
  approvers := xpath.selectNodes(doc,'/Claim/Settlements/Payment');
  FOR j IN 1..xmldom.getLength(approvers) LOOP
    p(xpath.valueOf(xmldom.item(approvers,j-1),'@Approver'),nl=>FALSE);
  END LOOP;
  xml.freeDocument(doc);
END;