2

Suppose I have an XML document like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
     targetNamespace="http://www.sample.com" 
     xmlns:sa="http://www.sample.com" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="name" type="sa:NameType"/>   
    <xs:simpleType name="NameType">
        <xs:restriction base="xs:string">
            <xs:maxLength value="100"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

Suppose that I have the DOM document for this schema, perhaps by doing something like this:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = = documentBuilder.parse(uriToSchema.toString());

How do I get the target namespace (http://www.sample.com) and its associated prefix (sa)?

I thought I could do this:

String namespace = ((Element) document.getDocumentElement()).getNamespaceURI());
String prefix = ((Element) document.getDocumentElement()).getPrefix());

But that gets the XML Schema namespace (http://www.w3.org/2001/XMLSchema) and its associated prefix (xs).

2 Answers 2

2

@Yogi's answer pointed me in the right direction. Here's the solution:

NamedNodeMap map = ((Element) document.getDocumentElement()).getAttributes();
String namespace = map.getNamedItem("targetNamespace").getNodeValue();
String prefix = "";
for (int i = 0; i < map.getLength(); i++)
{
   Attr attr = (Attr) map.item(i);
   if (attr.getValue().equals(namespace) && !attr.getName().equals("targetNamespace"))
   {
      prefix = attr.getName().split(":")[1];
      break;
   }
}
System.out.println("ns: " + namespace);
System.out.println("pr: " + prefix);
Sign up to request clarification or add additional context in comments.

Comments

0

You need iterate all attribute of document element like:

Path path = Paths.get(Typedetection.class.getClassLoader()
                 .getResource("data.xml").toURI());   

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(path.toFile());

        Element documentElement = (Element) document.getDocumentElement();
        String namespace = documentElement.getNamespaceURI();
        String prefix = documentElement.getPrefix();
        System.out.println("Namespace:"+namespace);
        System.out.println("Prefix:"+prefix);

        NamedNodeMap map = documentElement.getAttributes();
        for (int iattr=0; iattr<map.getLength(); iattr++) {
           Attr attr = (Attr)map.item(iattr);
           if (attr.getNamespaceURI() != null) {
              System.out.println("Attr " + attr.getName() + " -  " + attr.getNamespaceURI());
           }
        }
    }

2 Comments

Tested this code, and unfortunately it doesn't work. The namespace for every attribute is "w3.org/2000/xmlns".
You can fix it by changing attr.getNamespaceURI() to attr.getValue(), but this still won't tell me which namespace is the targetNamespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.