473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Validate fragment

Hi NG,

I would need to validate an XML fragment against a type defined within
an XSD (which defines many other things). What is the correct way of
achieving this?
QD2004
Jul 20 '05 #1
1 1723


QuantDev wrote:
I would need to validate an XML fragment against a type defined within
an XSD (which defines many other things). What is the correct way of
achieving this?


That depends on the validating XML parser you are using, for instance
MSXML 5, Microsoft COM based XML parser version 5, currently only
available with other MS software like Office 2003, has a method named
validateNode
for its XML DOM document that allows you to validate the node (including
any descendants) against the currently loaded schemas.
Unfortunatly that functionality is not available in MSXML 4, the version
you can download from http://msdn.microsoft.com/.
The documentation for the method of MSXML 5 is here:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmth_validateNode.asp>

If you have a simple schema constraining the contents of <god> elements
to NCNames:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<xs:element name="gods">
<xs:complexType>
<xs:sequence>
<xs:element name="god" type="xs:NCName" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

and an instance like

<?xml version="1.0" encoding="UTF-8"?>
<gods
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2005040701Xsd.x ml">

<god>Kibo</god>

</gods>

then the following client-side JScript example adds an new element with
content requested from the user and then validates the newly introduced
node:

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.5.0');
xmlDocument.async = false;
var valid = xmlDocument.load('test2005040701.xml');

if (valid) {
var newGod = xmlDocument.createElement('god');
var newName = prompt('Who is your GOD?', '');
newGod.appendChild(xmlDocument.createTextNode(newN ame));
xmlDocument.documentElement.appendChild(newGod);
var parseError = xmlDocument.validateNode(newGod);
if (parseError.errorCode != 0) {
alert(parseError.reason);
}
else {
alert(xmlDocument.xml);
}
}

The new validation API in Java 1.5 also allows you to validate a DOM
node against a loaded schema.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stuart Baker | last post by:
I just added session support to a web site and the URL's that contain a #fragment no longer function. Is there a delimiter that I can add following the #fragment that will tell the browser that the...
22
by: Haines Brown | last post by:
I have a document with a set of internal links such as: <a name="Z1"></a>... <a name="Z2"></a>... The W3C validator objects to the first instance of the name attribute: There is no attribute...
0
by: MG | last post by:
The following below is Microsoft sample to validate a XML document. My question is how can I put the validate errors in DataGrid? Using the ValidationEventHandle subroutine and adding ListBox...
2
by: Yuriy | last post by:
Hi, any ideas how to read XML fragment from TextReader? XmlTextReader constructor accepts only Stream or string as source Do I miss something? thanks Yuriy
24
by: Arno R | last post by:
Hi all, I have a client with several shoe-shops. Customers can leave their email-address if they want to be notified when there is a sale. Input is validated with instr() I am checking for @...
3
by: CK | last post by:
I am doing a Doc.Loadxml(myxml) and I have an xsd schema that I want to validate the myxml against. How can I do this. code would help. Thanks in advance.
3
by: Gustaf | last post by:
I'm trying to grasp this little passage from the XBRL spec: "The xlink:href attribute MUST be a URI. The URI MUST point to an XML document or to one or more XML fragments within an XML document....
2
by: Miguel Isidoro | last post by:
Hi all, I am trying to include a xsd:unique element in my schema to make an attribute of an element unique at the whole document level. Consider the following xml fragment: <Form>...
1
by: Andy Dingley | last post by:
On 31 Jul 2008 16:22:51 GMT, "mark4asp" <mark4asp@gmail.comwrote: It's hard to programatically validate HTML. You need to use Jade, because Mere Mortals don't get to go near the code that does...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.