Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 8th, 2006, 09:25 AM
mrc-1
Guest
 
Posts: n/a
Default XML Schema - limit occurrence

Hi,

i'd like to validate my XML file (see below) using XML Schema.

Now my question: is it possible to limit the occurrence of the element
b in the data element.
I mean: data can contain up to 10 b elements, but it doesn't matter how
the b elements are allocated in the elements a and c.

Any help would be appreciated -- thank you!


XML File (shall be valid)
<data>
<a>
<b>...</b>
<b>...</b>
<b>...</b>
<b>...</b>
</a>
<c>
<b>...</b>
<b>...</b>
</c>
</data>


XML File (shall be valid too)
<data>
<a>
<b>...</b>
<b>...</b>
<b>...</b>
</a>
<c>
<b>...</b>
<b>...</b>
<b>...</b>
<b>...</b>
</c>
</data>

  #2  
Old March 9th, 2006, 09:35 AM
George Bina
Guest
 
Posts: n/a
Default Re: XML Schema - limit occurrence

Hi,

No, you cannot do that in XML Schema alone. You can use however XML
Schema with Schematron embedded rules to check that the number of a/b
plus c/b elements inside data is less or equal to 10.
Here it is a working XML Schema with Schematron embedded rules that
does that:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="data">
<xs:annotation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" name="Check
number of occurances">
<rule context="data">
<assert test="count(a/b|c/b)&lt;=10">The number of b
elements should be less than or equal with 10.</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element ref="a"/>
<xs:element ref="c"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="b"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="b"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="b" type="xs:string"/>
</xs:schema>


On a sample file like:

<data>
<a>
<b>...</b>
<b>...</b>
<b>...</b>
<b>...</b>
</a>
<c>
<b>...</b>
<b>...</b>
<b>...</b>
<b>...</b>
<b>...</b>
<b>...</b>
<b>...</b>
</c>
</data>

It will give:

SystemID: C:\george\workspace\oXygen\samples\test.xml
Location: 1:0
Description: The number of b elements should be less than or equal with
10. (count(a/b|c/b)&lt;=10)

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

  #3  
Old March 9th, 2006, 12:45 PM
mrc-1
Guest
 
Posts: n/a
Default Re: XML Schema - limit occurrence

Thanks for your helpful answer.
I tried this using schematron validation and it works fine.

Thank you very much.

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles