Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27-03-2009
Member
 
Join Date: Feb 2009
Posts: 105
How to Validate Elements using XML Schema

Is there a way to use an XML Schema to validate if elements or attributes of a given path add up to be a certain total? The only schema element I've seen that uses XPath to view multiple elements or attribues of a scope is <xs:unique>. Seems so close, if only it could take an XSL math function to evaluate. But only a few validators I've seen online support <xs:unique>, so it's difficult to experiment.

Code:
<pie>
	<slice percent="20">A</slice>
	<slice percent="85">B</slice>
	<slice percent="15">C</slice>
</pie>
Reply With Quote
  #2  
Old 27-03-2009
Member
 
Join Date: Mar 2008
Posts: 258
Re: How to Validate Elements using XML Schema

It is necessary only to ensure that the XML document is associated with (at least) one. There are two ways to do that:

With a schema declaration in the XML document
By specifying the schema(s) to use in the application

To specify the schema definition in the document, you create XML like this:

Code:
<documentRoot
  xmlns:xsi="http://www.site_name.com/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation='YourSchemaDefinition.xsd'
>
The first attribute defines the XML namespace (xmlns) prefix, xsi, which stands for "XML Schema instance." The second line specifies the schema to use for elements in the document that do not have a namespace prefix--that is, for the elements you typically define in any simple, uncomplicated XML document. (You'll see how to deal with multiple namespaces in the next section.)

You can also specify the schema file in the application:

static final String schemaSource = "YourSchemaDefinition.xsd";
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";
...
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance()
...
factory.setAttribute(JAXP_SCHEMA_SOURCE,
new File(schemaSource));


Note: When the application specifies the schema(s) to use, it overrides any schema declarations in the document.
Reply With Quote
  #3  
Old 27-03-2009
Member
 
Join Date: Mar 2008
Posts: 227
Re: How to Validate Elements using XML Schema

You can use Schematron as a second layer of validation that will allow you to test arbitrary XPath assertions about your XML document. There's a fairly old article about embedding Schematron in XSD that you might find helpful.
Reply With Quote
  #4  
Old 27-03-2009
Member
 
Join Date: Dec 2008
Posts: 202
Re: How to Validate Elements using XML Schema

You need use the -s option as well. This will turn on schema validation. When you don't supply the parser will attempt to validate against a DTD, hence the error messages you received.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Validate Elements using XML Schema"
Thread Thread Starter Forum Replies Last Post
Schema in SQL2 \"Dritan\" Software Development 3 11-01-2011 08:26 AM
Don't know about XML schema ScarFace 01 Software Development 5 04-02-2010 09:20 AM
Adobe Photoshop Elements 6 & Adobe Premiere Elements 4 Jorge131 Vista Help 8 02-01-2010 05:53 AM
Get ddl of schema from toad Katlin Software Development 2 29-05-2009 06:05 PM
Photoshop Elements and Premiere Elements go to version 7.0 Killen Customize Desktop 2 29-08-2008 01:17 PM


All times are GMT +5.5. The time now is 04:56 PM.