This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Microsoft XML > November 2004 > XML schema validation of one xml block based on values from another xml block





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author XML schema validation of one xml block based on values from another xml block
Andy

2004-11-18, 7:17 pm

Hi!

Is possible to do XML validation of one XML block based on values from
another XML block?

For example, I have an input XML file containing account types.
<Root>
<AccountTypes>
<AccountType></AccountType>
…………………………………..
</AccountTypes>

<Accounts>
<Account>
<AccountType></AccountType>
</Account>
</Accounts>
</Root>

Is there a way I can put restriction in schema that <Account> must
have only such <AccountType> that are listed in <AccountTypes>


The file size is 20-25 MB. (It would not be a good idea to load it
into DOM)
It is preferably to catch it before processing will start

Thank you,
Andy
Andy

2004-11-26, 7:20 pm

Hi Martin!

Thank you!!! It works.

I tried before this samples, but it did not work.
http://msdn.microsoft.com/library/d...fkeyelement.asp


After I moved <xs:keyref... from <xs:element name="A' into <xs:element
name="Root" ...
as you did in your samples it worked.

Thank you.

Martin Honnen <mahotrash@yahoo.de> wrote in message news:<ONyyPwjzEHA.1292@TK2MSFTNGP10.phx.gbl>...
> Andy wrote:
>
>
>
> You can specify key/keyref constraints, see
> http://www.w3.org/TR/xmlschema-0/#s...eysAndtheirRefs
> here is a simple example
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> version="1.0">
>
> <xs:element name="root">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="account-types" />
> <xs:element ref="accounts" />
> </xs:sequence>
> </xs:complexType>
> <xs:key name="accountTypeKey">
> <xs:selector xpath="account-types/account-type" />
> <xs:field xpath="." />
> </xs:key>
> <xs:keyref name="refAccountTypeKey" refer="accountTypeKey">
> <xs:selector xpath="accounts/account/account-type" />
> <xs:field xpath="." />
> </xs:keyref>
> </xs:element>
>
> <xs:element name="account-types">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="account-type" maxOccurs="unbounded"
> type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> <xs:element name="accounts">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="account" maxOccurs="unbounded" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> <xs:element name="account">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="account-type" type="xs:string" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> </xs:schema>
>
> then in the XML instance file
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:noNamespaceSchemaLocation="test2004111901Xsd.xml">
> <account-types>
> <account-type>free</account-type>
> <account-type>normal</account-type>
> </account-types>
> <accounts>
> <account>
> <account-type>normal</account-type>
> </account>
> <account>
> <account-type>super</account-type>
> </account>
> </accounts>
> </root>
>
> the account-type "super" is flagged as an error.

Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews