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).