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 August 27th, 2008, 02:30 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default Re: Validating one XML value against another

Hello,

I'm wondering if it's possible to validate 1 XML value against another?

For example, I have a list of genres and sub-genres in an XML feed and based on the specified genre in the XML I want to validate it against a list of allowable sub-genre values for that genre.

Code:
<myfeed genre="fruit" subgenre="apples" />
<myfeed genre="animals" subgenre="tiger" />
I want the schema to first see the genre of "fruit" or "animals" and based on which one was selected validate it against different xsd:enumeration blocks.

So, if fruit was selected as the genre, a list of allowable sub-genres would be...

Code:
<xsd:restriction base="xsd:string">
  <xsd:enumeration value="apples" />
  <xsd:enumeration value="oranges" />
  <xsd:enumeration value="bananas" />
</xsd:restriction>
And if animals was selected as the genre, a list of allowable sub-genres would be...

Code:
<xsd:restriction base="xsd:string">
  <xsd:enumeration value="tiger" />
  <xsd:enumeration value="monkey" />
  <xsd:enumeration value="panda" />
</xsd:restriction>

 
Old August 27th, 2008, 03:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can do this with "conditional type assignment" in XML Schema 1.1 (the <xs:alternative> element). It's not widely implemented yet, but you can try it out in Saxon-SA 9.1. Something like this:

<xs:element name="myfeed" type="feedType">
  <xs:alternative test="@genre='string'" type="fruitType"/>
  <xs:alternative test="@genre='animals'" type="animalType"/>
</xs:element>

<xs:complexType name="feedType">
    <xs:sequence/>
    <xs:attribute name="genre" type="..."/>
</xs:complexType>

<xs:complexType name="fruitType">
  <xs:complexContent>
   <xs:extension base="feedType">
     .... etc

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old August 27th, 2008, 09:05 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 You can do this with "conditional type assignment" in XML Schema 1.1 (the <xs:alternative> element). It's not widely implemented yet, but you can try it out in Saxon-SA 9.1. Something like this:
Thanks mhkay. I tried out your suggestion, but Visual Studio didn't want to know about it.

Does anyone know of a way to achieve this in XML Schema 1.0??
 
Old August 27th, 2008, 01:35 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It can't be done in XML Schema 1.0.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
validating xml m_rajib C# 0 May 5th, 2006 12:53 AM
Validating XML against XSD ShaileshShinde General .NET 0 September 20th, 2005 01:22 AM
Validating Xml files with XSD file ShaileshShinde VB.NET 2002/2003 Basics 1 September 12th, 2005 07:00 AM
Validating XML Schema lmadhavi XML 0 November 18th, 2004 06:58 AM
Validating XML sunilyenpure XML 3 July 13th, 2004 12:11 AM





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