0

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?

RoninDev
  • 5,446
  • 3
  • 23
  • 37

1 Answers1

0

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

Ashis Roy
  • 44
  • 4
  • I'm quite sure that XML document can override default namespace in the inner elements. It does not affect the whole document, but only these inner elements – RoninDev Sep 23 '19 at 15:17
  • If your goal is to override the default namespace then what is the problem with the following: – Ashis Roy Sep 23 '19 at 16:56
  • I'm extracting `AttachmentHeader`, not `Content` or `tns:Response`. Attachment header has the namespace `xmlns="urn://message/"`, but I get `xmlns="urn://shadow"` – RoninDev Sep 23 '19 at 17:01
  • My understanding is that elements are iterated in document order. What happens when you change the order, AttachmentHeader before Content? – Ashis Roy Sep 23 '19 at 21:41
  • Result not changed – RoninDev Sep 24 '19 at 07:48
  • I am not sure why the architects of VTD-XML have designed this way. It makes sense to me though that a sibling or child elements should have the same default namespace, I think once a namespace is overridden it should be applied consistently to the siblings and child elements, it appears that in your case you have to explicitly declare the default namespace in AttachmentHeader. There is another method iterate2(), I don't know the behavior of this method. – Ashis Roy Sep 24 '19 at 12:22