« March 2006 | Main | May 2006 »
April 21, 2006
XPath and default namespaces
Using default namespaces in XPath expressions seems to be a little bit complicated. The following code excerpt shows the problem:
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
XPath xPath = XPathFactory.newInstance().newXPath();
Document doc = documentBuilder.parse(new File("C:/test.xml"));
Node node =(Node) xPath.evaluate("/address", doc, XPathConstants.NODE);
System.out.println(node);
}
The corresponding XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<address xmlns="http://wsiftypes.addressbook/" />
On the first view the XPath expression "/address" should match the root element "address" of the XML document. But this does not work. The resulting node is "null".
The element "address" is assigned to the namespace "http://wsiftypes.addressbook/" and must be used in the XPath expression. Because it is not possible to map this namespace to an empty prefix it must be mapped to a named prefix.
The above code is changed to use an Implementation of the interface NamespaceContext that maps namespaces to prefixes and the other way round. So using the default namespace works like this:
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
XPath xPath = XPathFactory.newInstance().newXPath();
Document doc = documentBuilder.parse(new File("C:/test.xml"));
NamespaceContextImpl nci = new NamespaceContextImpl();
nci.setNamespaceURI("typens","http://wsiftypes.addressbook/");
xPath.setNamespaceContext(nci);
Node node =(Node) xPath.evaluate("/typens:address", doc, XPathConstants.NODE);
System.out.println(node);
}
Posted by Dominik Marks at 10:35 AM | Comments (0)
April 11, 2006
Generating XML files from XML Schema
There are some existing tools that are able to create XML instances from XML Schema definitions. These are (incomplete list):
- Sun XML Instance Generator (Supports DTD, RELAX, TREX, Part of XML Schema. Can generate invalid documents, too)
- XML-XIG (Early stage SourceForge project)
- Microsoft .NET XmlSampleGenerator (limitations: xs:key, xs:keyref, xs:unique, xs:ENTITY, xs:ENTITIES and xs:NOTATION not supported, incomplete support for xs:pattern and xs:base64Binary)
- Altova XMLSpy (Commercial product)
- My own implementation so far (xs:pattern, xs:substitutionGroup, xs:any, xs:anyAttribute, xs:key, xs:keyRef, xs:unique, xs:notation, xs:annotation not supported, yet)
Posted by Dominik Marks at 05:29 PM | Comments (0)
April 04, 2006
First ideas about validating XPath expressions againt XML Schema
Using XPath expressions to access certain nodes in a document-oriented SOAP message is a good idea.
A problem may be that you don't know if the XPath expressions really match a node in the SOAP message, especially if interfaces and message structure are changed (see Changing interfaces with RPC and document-oriented style).
You don't know that an XPath expression is invalid until run-time. One concept should be to be able to do validation of XPath expressions at compile time (i.e. more loosely coupled, early binding). Here are some ideas how this can be gained:
- Creating an empty XML document from the XPath expression. Then use an XML validator to validate this against an XML Schema. (Problem: That XML document is not a complete document but only contains the nodes from the XPath expression)
- Creating an empty XML document from the XML Schema definition. Then use the XPath expression to get a certain node and see if it exists. (Problem: How complicate is it to create an empty XML document from the Schema? How many Schema elements and constructs need to be supported? E.g. specific data types are irrelevant for XPath expressions, are they?)
- Write a program that parses the XPath expression and walk along the Schema to see if the expression could be valid (Sounds very complicated)
Posted by Dominik Marks at 04:33 PM | Comments (3)