Search code examples
xmlxpathxsdvtd-xml

Modify XMLSchema Location with vtd-xml and xPath


Is there a way to modify the XML schema location parsed by vtd-xml ?

The xml files look like

<rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xmlSchema.xsd">
.
.
.
 </rootElement>

The best solution would be with xPath. I tried

/rootElement/@*

which works with DOM but not with vtd-xml

Here is my code

String xPath = "/rootElement/@*"
XMLModifier xm = new XMLModifier();
VTDGen vg = new VTDGen();
if (vg.parseFile(fnIn,true)){
       VTDNav vn = vg.getNav();
       xm.bind(vn);
       nodeXpath(xPath,vn);
}

private void nodeXpath(String xPath, VTDNav vn) throws Exception{
    int result;

    AutoPilot ap = new AutoPilot();
    ap.selectXPath(xPath);
    ap.bind(vn);
    while((result = ap.evalXPath())!=-1){
    System.out.println(vn.getAttrVal("xsi:noNamespaceSchemaLocation") + ", " + vn.getText() + ", " + vn.toString(result));
    xm.updateToken(result,"test");
        int p = vn.getText();

        if (p!=-1) {                
            System.out.println(vn.getText() + ", " + vn.toString(p));               
        }
    }
}

The result printed to screen is: 10, -1, xsi:noNamespaceSchemaLocation

and the output xml is

test
<rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" test="xmlSchema.xsd">

The element Name seems to be modified and an additional test is on top? Where is my mistake? How can I change the attribute value?

Thanks for your help.


Solution

  • Ok, this is my modification of your code. Result is the attribute name token index. And "result+1" should be the attribute value token index. Let me know if it works for you or not.

    AutoPilot ap = new AutoPilot();
    ap.selectXPath(xPath);
    ap.bind(vn);
    while((result = ap.evalXPath())!=-1){
    // System.out.println(vn.getAttrVal("xsi:noNamespaceSchemaLocation") + ", " + vn.getText() + ", " + vn.toString(result));
    xm.updateToken(result_+1,"test");