sample xsd file
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="NODE1">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NODE2">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NODE3">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="8"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NODE4">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="80"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
follwing procedure change NODE1 -> maxlength value 50 to 100
create or replace procedure XMLTEST3 is
--Documents variables
doc DBMS_XMLDOM.DOMDocument;
docelem DBMS_XMLDOM.DOMElement;
--sympleType Nodes
simpleTypeNodeList DBMS_XMLDOM.DOMNodelist;
simpleTypeNode DBMS_XMLDOM.DOMNode;
simpleTypeChildNodeList DBMS_XMLDOM.DOMNodelist;
simpleTypeChildNodeMap DBMS_XMLDOM.DOMNamedNodeMap;
simpleTypeChildNodeAttr DBMS_XMLDOM.DOMAttr;
simpleTypeNodeListLength number;
simpleTypeAttributeNode DBMS_XMLDOM.DOMNode;
--generalTypeCode Nodes
strGeneralTypeCode VARCHAR2(200);
simpleTypeChildNodeListLength number;
generalTypeCodeAttributeNode DBMS_XMLDOM.DOMNode;
strIGeneralTypeCodeAttr VARCHAR2(200);
generalTypeCodeMap DBMS_XMLDOM.DOMNamedNodeMap;
generalTypeCodeAttr DBMS_XMLDOM.DOMAttr;
generalTypeCodeNode DBMS_XMLDOM.DOMNode;
--generalTypeChild Nodes
generalTypeCodeChildNodeList DBMS_XMLDOM.DOMNodelist;
generalTypeCodeCNListLength number;
strGeneralTypeCodeCN VARCHAR2(200);
generalTypeCodeCNMap DBMS_XMLDOM.DOMNamedNodeMap;
generalTypeCodeCNAttr DBMS_XMLDOM.DOMAttr;
generalTypeCodeChildNode DBMS_XMLDOM.DOMNode;
--Other Variables
xmlTypeValue XMLType;
--used for write modified xmltype
xmlTypeTOClob CLOB;
--used for write again modified clob to xmltype
clobToXmlType sys.xmlType;
--rowid used to update modified record
row_id varchar(200);
--file name to check specisfic file
fileName varchar(1000);
BEGIN
xmlTypeValue := XMLType(
'
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="NODE1">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NODE2">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NODE3">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="8"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NODE4">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="80"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
');
-- Create DOMDocument handle
doc := DBMS_XMLDOM.newDOMDocument(xmlTypeValue);
DBMS_LOB.createtemporary (xmlTypeTOClob, TRUE);
DBMS_XMLDOM.WRITETOCLOB(doc, xmlTypeTOClob);
docelem := DBMS_XMLDOM.getDocumentElement(doc);
-- get nodelist
-- Access simpletype elements
simpleTypeNodeList := DBMS_XMLDOM.getElementsByTagName(docelem, 'simpleType');
simpleTypeNodeListLength := dbms_xmldom.getLength(simpleTypeNodeList);
-- loop through simple type elements
FOR i IN 0 .. simpleTypeNodeListLength - 1 LOOP
simpleTypeNode := DBMS_XMLDOM.item(simpleTypeNodeList, i);
-- Get the attributes of this node
simpleTypeChildNodeMap := DBMS_XMLDOM.GETATTRIBUTES(simpleTypeNode);
-- Find the 'index' attribute
simpleTypeAttributeNode := DBMS_XMLDOM.GETNAMEDITEM(simpleTypeChildNodeMap,'name');
simpleTypeChildNodeAttr := DBMS_XMLDOM.MAKEATTR(simpleTypeAttributeNode);
strGeneralTypeCode := DBMS_XMLDOM.GETVALUE(simpleTypeChildNodeAttr);
IF strGeneralTypeCode = 'NODE2' THEN
simpleTypeChildNodeList := DBMS_XMLDOM.GETCHILDNODES(simpleTypeNode);
simpleTypeChildNodeListLength := dbms_xmldom.getLength(simpleTypeChildNodeList);
FOR j IN 0 .. simpleTypeChildNodeListLength - 1 LOOP
-- Get the attributes of this node
generalTypeCodeNode := DBMS_XMLDOM.item(simpleTypeChildNodeList, j);
generalTypeCodeMap := DBMS_XMLDOM.GETATTRIBUTES(generalTypeCodeNode);
generalTypeCodeAttributeNode := DBMS_XMLDOM.GETNAMEDITEM(generalTypeCodeMap,'base');
generalTypeCodeAttr := DBMS_XMLDOM.MAKEATTR(generalTypeCodeAttributeNode);
strIGeneralTypeCodeAttr := DBMS_XMLDOM.GETVALUE(generalTypeCodeAttr);
--final loop
generalTypeCodeChildNodeList := DBMS_XMLDOM.GETCHILDNODES(generalTypeCodeNode);
generalTypeCodeCNListLength := dbms_xmldom.getLength(generalTypeCodeChildNodeList);
FOR k IN 0 .. generalTypeCodeCNListLength - 1 LOOP
-- Get the attributes of this node
generalTypeCodeChildNode := DBMS_XMLDOM.item(generalTypeCodeChildNodeList, k);
generalTypeCodeCNMap := DBMS_XMLDOM.GETATTRIBUTES(generalTypeCodeChildNode);
generalTypeCodeAttributeNode := DBMS_XMLDOM.GETNAMEDITEM(generalTypeCodeCNMap,'value');
generalTypeCodeCNAttr := DBMS_XMLDOM.MAKEATTR(generalTypeCodeAttributeNode);
strGeneralTypeCodeCN := DBMS_XMLDOM.GETVALUE(generalTypeCodeCNAttr);
IF DBMS_XMLDOM.GETNODENAME(generalTypeCodeChildNode) = 'xsd:maxLength' THEN
IF DBMS_XMLDOM.GETVALUE(DBMS_XMLDOM.MAKEATTR(generalTypeCodeAttributeNode)) = '50' THEN
DBMS_OUTPUT.PUT_LINE('-------UPDATING RECORD--------BEGIN ROW ID -'||row_id);
DBMS_OUTPUT.PUT_LINE('-------BEFORE CHANGE = '||strGeneralTypeCodeCN);
DBMS_XMLDOM.setAttribute(DBMS_XMLDOM.makeElement(generalTypeCodeChildNode), 'value', '100');
DBMS_OUTPUT.PUT_LINE('-------AFTER CHANGE = '||DBMS_XMLDOM.GETVALUE(DBMS_XMLDOM.MAKEATTR(generalTypeCodeAttributeNode)));
DBMS_XMLDOM.WRITETOCLOB(doc, xmlTypeTOClob);
clobToXmlType := sys.xmlType.createXML(xmlTypeTOClob);
--update <table_name> set <column_name> = clobToXmlType where ROWID = row_id;
--commit;
DBMS_OUTPUT.PUT_LINE('-------UPDATING RECORD--------END ROW ID -'||row_id);
DBMS_OUTPUT.PUT_LINE('');
END IF;
END IF;
END LOOP; --generalTypeCodeCNListLength for
END LOOP; --simpleTypeChildNodeListLength for
END IF;
END LOOP; --simpleTypeNodeListLength for
DBMS_XMLDOM.freeDocument(doc);
end XMLTEST3;
You can do this from updateXML and xpath also
send me and email (saneera@gmail.com) if there is any question regarding this
Thanks
No comments:
Post a Comment