Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2005, 01:29 PM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default DTD "exclusive or" confusion

According to Beginning XML (3rd edition, p. 106), an element can (allegedly) force selection of a child element from one of a group using the "exclusive or" operator "|". Specifically, in reference to the code fragment

  <!ELEMENT location (GPS | country)>

the good book doth say, "This declaration would allow our <location> element to contain one <country> or one <GPS> element. If our <location> element...contained more than one of these elements, the parser would raise an error."

Makes sense to me; that's how I've always understood an XOR to work. But while trying to decipher some production code at work, I come on an .xml that shows *both* elements as children to an element which had declared them in the .dtd to be in an XOR relationship. It parses and runs just fine.

Now I'm thinking, "Well, maybe there's something else going on in this (not-so-easily-understood) production code that's overriding the XOR. So I cobble together the following simple code

  <?xml version = "1.0"?>
  <!DOCTYPE Script
  [
  <!ELEMENT location (GPS | country)>

  <!ELEMENT GPS (#PCDATA)>
  <!ELEMENT country (#PCDATA)>
  ]>

  <Root>
     <GPS>Some foo</GPS>
     <country>Some bar</country>
  </Root>

And lo and behold! It parses and runs just fine using both the MSXML 4.0 (sp2) parser as well as (the book-recommended) Topologi "Schematron Validator". So I ask, "What up?!?"

And just to make life extra-confusing, I tried an additional little experiment. This code (adding an additional GPS element to the location parent also parses just fine despite the utter lack of any cardinality indicator.

  <?xml version = "1.0"?>
  <!DOCTYPE Script
  [
  <!ELEMENT location (GPS | country)>

  <!ELEMENT GPS (#PCDATA)>
  <!ELEMENT country (#PCDATA)>
  ]>

  <Root>
     <GPS>Some foo</GPS>
     <country>Some bar</country>
     <GPS>More foo</GPS>
  </Root>

This, too, runs counter to the book's pronouncement (p. 113), "...when no cardinality indicator is used...the element must appear once and only once."

Again, I ask, "What the hey...?!?"

Thanks in advance for a clue,

Steve
 
Old June 19th, 2005, 04:49 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Works as expected for me.
Given DtDExclusiveOrGood.xml as
Code:
<?xml version = "1.0"?>
  <!DOCTYPE location 
  [
  <!ELEMENT location (GPS | country)>

  <!ELEMENT GPS (#PCDATA)>
  <!ELEMENT country (#PCDATA)>
  ]>

  <location>
     <GPS>Some foo</GPS>
  </location>
and DtDExclusiveOrBad.xml as
Code:
<?xml version = "1.0"?>
  <!DOCTYPE location 
  [
  <!ELEMENT location (GPS | country)>

  <!ELEMENT GPS (#PCDATA)>
  <!ELEMENT country (#PCDATA)>
  ]>

  <location>
     <GPS>Some foo</GPS>
     <country>Some bar</country>
  </location>
then the following script can be run:
Code:
var XML = "DtDExclusiveOrGood.xml";
//var XML = "DtDExclusiveOrBad.xml";

function getValidatingDomDoc()
{
  var oDom =  new ActiveXObject("Msxml2.DomDocument.4.0");
  oDom.async = false;
  oDom.validateOnParse = true;
  return oDom;
}

function main()
{
  var oDom = getValidatingDomDoc();
  var bLoaded = oDom.load(XML);
  if (bLoaded)
  {
    WScript.echo(oDom.xml);
  }
  else
  {
    WScript.echo("Error: " + oDom.parseError.reason + "\nSource: " + oDom.parseError.srcText);
  }
}

main();

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Automated tool to convert XML from DTD to DTD lsantos2000 XSLT 2 October 17th, 2007 08:21 AM
E-mail reports with exclusive rights Brendan Bartley Access 1 August 2nd, 2005 11:49 PM
Exclusive ADO Connection bsa Pro VB Databases 1 September 6th, 2004 07:16 AM
Exclusive ADO Connection bsa SQL Server 2000 2 August 24th, 2004 12:10 AM
Exclusive selection RobbieGee MySQL 2 July 5th, 2004 04:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.