473,811 Members | 3,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>

Mar 8 '06 #1
2 1896
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"
elementFormDefa ult="qualified" >
<xs:element name="data">
<xs:annotatio n>
<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="unbo unded" ref="b"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="c">
<xs:complexType >
<xs:sequence>
<xs:element maxOccurs="unbo unded" 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\works pace\oXygen\sam ples\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

Mar 9 '06 #2
Thanks for your helpful answer.
I tried this using schematron validation and it works fine.

Thank you very much.

Mar 9 '06 #3

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

Similar topics

2
1561
by: tgtt | last post by:
the schema is: http://www.web3d.org/specifications/x3d-3.0.xsd when use SOM to load as: .... SchemaCache = new ActiveXObject("Msxml2.XMLSchemaCache.4.0"); SchemaCache.add("","x3d-3.0.xsd"); .... pop up a messageBox report that there are some error.but the schema is
1
2999
by: Fred Smith | last post by:
Any suggestions or tips to the questions below I have been wrestling with would be most welcome: I have an example XSD file I have been experimenting with. Suppose a user can select from 1 to 4 checkboxes on a web form: Select your favorite sport(s): (checkbox) baseball (checkbox) football
4
2297
by: bibsoconner | last post by:
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all works with SPY, but when I pick "Validate Schema" from the .NET 2003 menu, it fails with message: "Type XType is not declared." As I hinted at in my subject line, I suspect that it has to do with including another schema multiple times.
1
1504
by: Stefan Reiter | last post by:
Hi, I want to limit a date to a minDate and a maxDate - is that possible, and how? <xs:simpleType> <xs:restriction base="xs:date"> <xs: </xs:restriction> </xs:simpleType> I assume it is only possible with "pattern".
3
368
by: 1-crm | last post by:
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 6 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!
1
1749
by: mrc-1 | last post by:
Hello, I built a XSD File with the tool XML-Spy from Altova. My XML file consists of about 5000 elements. If I use the validate button in XML-Spy, the validation process takes about 8 seconds. But if I use Xerces-J2 to validate my XML File it takes about 90 seconds (xerces-c takes about 85 sec). Does anybody know, why xerces is that slow resp. why XML Spy is that fast?
1
376
by: Chris Lieb | last post by:
I am new to XML Schema and am running into a bit of a snag. I have defined an XML-based scripting language for an updater program that I am working on. I would like to make a schema for this language since malformed XML documents break the updater. (I did not have time to add good error-handling code, so run-time errors can abound if the document is malformed.) A sample doc might look something like this: <manifest>
1
373
by: Nichino | last post by:
Hello, I'm trying to define my first Xml Schema and have a problem. The XML I'd like to define through the Schema is the following one. Can you help me please? I don't know how to insert the 3 final notes. Many thanks and my apologies for the trouble. Best, Nick <language>
5
1732
by: Grant Robertson | last post by:
I am only just learning about schema basics. I am trying to understand some fundamental principles about what can be specified within a schema. If I define an enumerated list of values for an attribute in the root element, is it then possible to limit which child elements may appear depending on the value of that attribute? This enumerated attribute would be used to specify a sub-type for this particular instance of a document. For...
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.