This discussion is archived
3 Replies Latest reply: Feb 5, 2013 11:34 AM by jtahlborn RSS

Is there a way to give a Java Document an XML schema for XPath queries

DavidThi808 Newbie
Currently Being Moderated
javax.xml.parsers.DocumentBuilder can build a document from a single stream which is the XML file. However, I can't find any way to also give it a schema file.

Is there a way to do this so that my XPath queries can perform type aware queries and return typed data?

We presently use dom4j/jaxen and it does this fine but I figure we should switch to the standard Java runtime if this has been added.

Thanks dave
  • 1. Re: Is there a way to give a Java Document an XML schema for XPath queries
    jtahlborn Expert
    Currently Being Moderated
    you can provide a schema when parsing an xml document in order to check for schema conformance, but that isn't going to affect your XPath queries at all. DOM content is never anything but strings.
  • 2. Re: Is there a way to give a Java Document an XML schema for XPath queries
    DavidThi808 Newbie
    Currently Being Moderated
    jtahlborn wrote:
    you can provide a schema when parsing an xml document in order to check for schema conformance, but that isn't going to affect your XPath queries at all. DOM content is never anything but strings.
    In the case of both dom4j/jaxen and the .NET runtime the values returned from XPath queries when a schema is provided are typed. So you get back numbers, dates, and booleans as well as strings. And for numbers you get correct comparisons so 20 > 1.
  • 3. Re: Is there a way to give a Java Document an XML schema for XPath queries
    jtahlborn Expert
    Currently Being Moderated
    DavidThi808 wrote:
    jtahlborn wrote:
    you can provide a schema when parsing an xml document in order to check for schema conformance, but that isn't going to affect your XPath queries at all. DOM content is never anything but strings.
    In the case of both dom4j/jaxen and the .NET runtime the values returned from XPath queries when a schema is provided are typed. So you get back numbers, dates, and booleans as well as strings. And for numbers you get correct comparisons so 20 > 1.
    i guess it's possible. i use schemas when parsing xml, and i've used xpath (infrequently), but i'm not sure that i've ever used them together in a way that i would see functionality like that.

    FYI, you use http://docs.oracle.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setSchema%28javax.xml.validation.Schema%29 to specify a schema while parsing with a DocumentBuilder.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points