Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6372537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:15:07+00:00 2026-05-25T01:15:07+00:00

I currently generate a NodeList of all the Document nodes (in document order) manually.

  • 0

I currently generate a NodeList of all the Document nodes (in document order) manually. The XPath expression to get this NodeList is

//. | //@* | //namespace::*

My first attempt for walking the DOM manually and collecting the nodes (NodeSet is a primitive NodeList implementation delegating to a List):

private static void walkRecursive(Node cur, NodeSet nodes) {
    nodes.add(cur);

    if (cur.hasAttributes()) {
        NamedNodeMap attrs = cur.getAttributes();
        for (int i=0; i < attrs.getLength(); i++) {
            Node child = attrs.item(i);
            walkRecursive(child, nodes);
        }
    }

    int type = cur.getNodeType();
    if (type == Node.ELEMENT_NODE || type == Node.DOCUMENT_NODE) {
        NodeList children = cur.getChildNodes();
        if (children == null)
            return;

        for (int i=0; i < children.getLength(); i++) {
            Node child = children.item(i);
            walkRecursive(child, list);
        }
    }
}

I would start the recursion with calling walkRecursive(doc, nodes) where doc is the org.w3c.Document and nodes a (yet empty) NodeSet.

I tested this using this primitive XML document:

<?xml version="1.0"?>
<myns:root xmlns:myns="http://www.my.ns/#">
  <myns:element/>
</myns:root>

If I for example canonicalize my manually created NodeSet and the NodeList generated by the initially mentioned XPath expression and compare the two byte for byte, then the result is equal and seems to work just fine.

But, if I iterate over the two NodeLists and print debug info (typeString simply generates a string representation)

for (int i=0; i < nodes.getLength(); i++) {
    Node child = nodes.item(i);
    System.out.println("Type: " + typeString(child.getNodeType()) +
                       " Name:" + child.getNodeName() + 
                       " Local name: " + child.getLocalName() +
                       " NS: " + child.getNamespaceURI());
}

then I receive this output for the XPath-generated NodeList:

Type: DocumentNode Name:#document Local name: null NS: null
Type: Element Name:myns:root Local name: root NS: http://www.my.ns/#
Type: Attribute Name:xmlns:myns Local name: myns NS: http://www.w3.org/2000/xmlns/
Type: Attribute Name:xmlns:xml Local name: xml NS: http://www.w3.org/2000/xmlns/
Type: Text Name:#text Local name: null NS: null
Type: Element Name:myns:element Local name: element NS: http://www.my.ns/#
Type: Text Name:#text Local name: null NS: null

and this for the manually generated NodeList:

Type: DocumentNode Name:#document Local name: null NS: null
Type: Element Name:myns:root Local name: root NS: http://www.my.ns/#
Type: Attribute Name:xmlns:myns Local name: myns NS: http://www.w3.org/2000/xmlns/
Type: Text Name:#text Local name: null NS: null
Type: Element Name:myns:element Local name: element NS: http://www.my.ns/#
Type: Text Name:#text Local name: null NS: null

So, as you can see, in the first example the NodeList additionally contains the Node for the XML namespace:

Type: Attribute Name:xmlns:xml Local name: xml NS: http://www.w3.org/2000/xmlns/

Now my questions:

a) If I interpret xml-names11 correctly, then I don’t need the xmlns:xml declaration:

The prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace. It MAY, but need not, be declared, and MUST NOT be undeclared or bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace.

Am I correct? (at least c) hints in that direction)

b) But then, why does the XPath evaluation add it anyway – shouldn’t it just include what was there in the first place instead of automagically adding things?

c) This can cause trouble with XML canonicalization, although it shouldn’t – declarations of the xml namespace should be omitted during canonicalization. Does anyone know of (Java) implementations that get this wrong?


Edit:

Here’s the code I used to evaluate the XPath expression that contained the ‘xml’ namespace node:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
InputStream in = ...;
try {
    Document doc = dbf.newDocumentBuilder().parse(in);
    XPathFactory fac = XPathFactory.newInstance();
    XPath xp = fac.newXPath();
    XPathExpression exp = xp.compile("//. | //@* | //namespace::*");
    NodeList nodes = (NodeList)exp.evaluate(doc, XPathConstants.NODESET);
} finally {
    in.close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:15:07+00:00Added an answer on May 25, 2026 at 1:15 am

    Since you can write

    <myns:root xml:space="preserve" xmlns:myns="http://www.my.ns/#">
      <myns:element/>
    </myns:root>
    

    without declaring the “xml” prefix, then it must be there implicitly. It is therefore correct to include the namespace node for this namespace declaration in the //namespace:* location step

    So,

    a) you are wrong, you need it (well, depending on the purpose of your code)

    b) see above

    c) no, but I’ve seen other namespace corner cases where things went haywire (e.g. Problem with conversion of org.dom4j.Document to org.w3c.dom.Document and XML Signature

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I'm trying to automatically generate a create script for all my SQL jobs
This is how I generate a URL in ASP.NET MVC currently: Url.Action(Index, new {
I currently generate lists using following expression (T and no_jobs are integers): for i
I currently use phpthumb to generate thumbnails for profile pictures. http://phpthumb.gxdlabs.com/ This is my
Currently I am doing something like this in my code: --Generate a list of
Currently we generate classes in App_Code by returning all the sprocs from all of
I am currently using this to generate my yaml: doctrine orm:convert-mapping --force --from-database yml
Currently I'm using the BouncyCastle library to generate a certificate. Something like this: X509V3CertificateGenerator
In order to generate a XML i am using following Code currently. .<?php require
How would one generate this xaml from C# code instead: Currently: <TextBlock> Click <Hyperlink

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.