VTD-XML shadows namespace from sibling element and add unexpected ns

105 Views Asked by At

I have an issue when trying to extract element via getElementFragmentNs.

Here sample test:

    @Test
    public void shouldNotShadowNamespaceAndAddSiblingNamespaces() throws Exception {
        byte[] bytes = ("<ns2:Response xmlns=\"urn://message\" xmlns:ns2=\"urn://ns2\">\n" +
                "    <ns2:Data>\n" +
                "        <Content>\n" +
                "            <tns:Response\n" +
                "                    xmlns:tns=\"urn://tns\"\n" +
                "                    xmlns=\"urn://shadow\">\n" +
                "                <tns:test/>\n" +
                "            </tns:Response>\n" +
                "        </Content>\n" +
                "        <AttachmentHeaderList>\n" +
                "            <AttachmentHeader/>\n" +
                "        </AttachmentHeaderList>\n" +
                "    </ns2:Data>\n" +
                "</ns2:Response>").getBytes("UTF-8");

        VTDGen vg = new VTDGen();
        vg.setDoc(bytes);
        vg.parse(true);  // set namespace awareness to true

        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectElement("AttachmentHeader");
        ap.iterate();

        ElementFragmentNs efn = vn.getElementFragmentNs();
        byte[] result = efn.toBytes();
        assertThat(new String(result, "UTF-8"), is("<AttachmentHeader xmlns=\"urn://message/\"/>"));
    }

The actual result is:

<AttachmentHeader xmlns:tns="urn://tns" xmlns="urn://shadow" xmlns:ns2="urn://ns2"/>

But I expect:

<AttachmentHeader xmlns="urn://message/"/>

Why it shadows default namespace with default namespace from sibling subelement? And why it adds unnecessary namespaces from it?

1

There are 1 best solutions below

6
Ashis Roy On

I am not sure that a XML document can have 2 default namespaces, one at the root element and one at the child level.