Search code examples
javaxmlparsingxpathvtd-xml

com.ximpleware parse XML - XPath - Syntax error after or around the end of ==>


I use com.ximpleware and I try to parse an XML like that:

<S2SCTScf:SCTScfBlkCredTrf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" xsi:schemaLocation="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf file:///T:/CommonData%201/CBS/CBS%20Payments%20Team/Testing/XSD/SCT/SCTScfBlkCredTrf.xsd">
        <CdtTrfTxInf>
            <PmtId>
                <EndToEndId>171766 12856615</EndToEndId>
                <TxId>6022064LAS99</TxId>
            </PmtId>
        ..............
        </CdtTrfTxInf>
        ..............
</S2SCTScf:SCTScfBlkCredTrf>

and the java code:

        VTDGen vg = new VTDGen();
        if (vg.parseFile("aaa.xml",true)){
                         VTDNav vn = vg.getNav();
                         AutoPilot ap = new AutoPilot(vn);
                         ap.bind(vn);
                         str = "/S2SCTScf:SCTScfBlkCredTrf/CdtTrfTxInf";
                         ap.selectXPath(str);
                         System.out.println(ap.evalXPath());
        }

throws me an error:

 Syntax error after or around the end of ==>
 Exception during navigation com.ximpleware.XPathParseException: No URL found for prefix:S2SCTScf

Any idea what should I do not being able to modify my XML? Thanks!


Solution

  • Not familiar with vtd-xml, but the documentation suggests that you need to register namespace prefix via declareXPathNameSpace() prior to using it in selectXPath() :

    .....
    ap.declareXPathNameSpace("S2SCTScf", "urn:S2SCTScf:xsd:$SCTScfBlkCredTrf");
    str = "/S2SCTScf:SCTScfBlkCredTrf/CdtTrfTxInf";
    ap.selectXPath(str);
    .....