473,881 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Schema to describe specifc case

Hi,
How can I describe an XML like this:

<X>
<Y/>
<Z/>
<Y/>
<Y/>
</X>

I mean a case where Z appears once and Y appears zero or more times and
the order is not important. If Z might appear more than once I can use:

<xsi:choice minOccurs="0" maxOccurs="unbo unded">

But I do not know what to do when it appears once.
Thanks,
Reuven Nisser
Mar 15 '06 #1
5 1206
Hi,

You need a model like:

Y*,Z,Y*

In XML Schema that will be:

<xs:element name="X">
<xs:complexType >
<xs:sequence>
<xs:element ref="Y" minOccurs="0" maxOccurs="unbo unded"/>
<xs:element ref="Z"/>
<xs:element ref="Y" minOccurs="0" maxOccurs="unbo unded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

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

Mar 15 '06 #2
Hi,
Thanks, I thought it will be the answer but I hoped there is an easier
way. You see Y could be several items (Y1,Y2,Y3...) which means creating
a "choice" before and after the Z element.
Regards, Reuven
Mar 15 '06 #3
Hi,
Thanks, I thought it will be the answer but I hoped there is an easier
way. You see Y could be several items (Y1,Y2,Y3...) which means creating
a "choice" before and after the Z element.

And if Z is also several items (Z1,Z2,Z3,...) which can also appear once
everywhere I have a problem.

The complete script would be:
<X>
<Y1/>
<Z2/>
<Y2/>
<Y1/>
<Z1/>
<Y2/>
<Y3/>
<Z3/>
</X>

Regards, Reuven
Mar 15 '06 #4
Hi,

You cannot do this in XML Schema alone, but you can use a relaxed model
in the XML Schema and embedded Schematron rules to check that Z1, Z2,
Z3 elements appear once. Below is a working XML Schema with embedded
schematron 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="X">
<xs:annotatio n>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" name="Check
number of occurances">
<rule context="X">
<assert test="count(Z1) =1">There should be one occurence of
Z1 in X.</assert>
<assert test="count(Z2) =1">There should be one occurence of
Z1 in X.</assert>
<assert test="count(Z3) =1">There should be one occurence of
Z1 in X.</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType >
<xs:choice maxOccurs="unbo unded">
<xs:element ref="Y1"/>
<xs:element ref="Y2"/>
<xs:element ref="Y3"/>
<xs:element ref="Z1"/>
<xs:element ref="Z2"/>
<xs:element ref="Z3"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="Y1">
<xs:complexTy pe/>
</xs:element>
<xs:element name="Y2">
<xs:complexTy pe/>
</xs:element>
<xs:element name="Y3">
<xs:complexTy pe/>
</xs:element>
<xs:element name="Z1">
<xs:complexTy pe/>
</xs:element>
<xs:element name="Z2">
<xs:complexTy pe/>
</xs:element>
<xs:element name="Z3">
<xs:complexTy pe/>
</xs:element>
</xs:schema>

You can also do this in Relax NG. Below are working schemas both in
compact syntax and in XML syntax.

Compact syntax:

start =
element X {
(element Z1 { text }
& element Z2 { text }
& element Z3 { text })
& (element Y1 { text }
| element Y2 { text }
| element Y3 { text })*
}

XML syntax:

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
datatypeLibrary ="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="X">
<interleave>
<interleave>
<element name="Z1"><text/></element>
<element name="Z2"><text/></element>
<element name="Z3"><text/></element>
</interleave>
<zeroOrMore>
<choice>
<element name="Y1"><text/></element>
<element name="Y2"><text/></element>
<element name="Y3"><text/></element>
</choice>
</zeroOrMore>
</interleave>
</element>
</start>
</grammar>

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

Mar 15 '06 #5
Hi Reuven,

Must the Z element always appear beside between Y blocks? Is the sequence
important? If so, I agree George that it is the only way to write such a
complex schema. If the sequence is not so important, I suggest you use

<xs:element name="X">
<xs:complexType >
<xs:sequence>
<xs:element ref="Z"/>
<xs:element ref="Y" minOccurs="0" maxOccurs="unbo unded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

which puts Z before the Y block. You can also add attribute in the Z
element to identify the sequence of Z.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mar 16 '06 #6

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

Similar topics

1
2729
by: Gregg Williams | last post by:
Hi--I am having a problem designing a schema to fit my XML data, and I'm hoping that someone can help. Essentially, I have a schema in mind and two target vocabularies for it, where one vocabulary is a subset of the other. I will describe one part of the schema to give you an idea of what my problem is. The "big" schema calls for 0 or more selector elements, each containing a (required) name attribute and simple character data, as...
6
2495
by: Pieter | last post by:
I've read a lot of posts on "why relax ng is so very good" and on "why w3c xml schema should be the only schema language". I'm, however, still not clear on why I should prefer one over the other. I've made a small list of some good and bad points of both. These points don't really go into the grammar aspects of these languages, but are more about secondary aspects. The grammar aspects are different, but both are suitable for validating...
2
2503
by: Stanimir Stamenkov | last post by:
I'm trying to find out if it is permissible to include a schema document with absent target namespace to a schema with specified target namespace, and if it is, what are the rules to resolve the target namespace of the components from the included schema document. I'm confused because of the rules I read in the XML Schema spec <http://www.w3.org/TR/xmlschema-1/#element-element>: > If the <element> element information item has <schema>...
7
7387
by: Robert Stearns | last post by:
I ran the following bit of SQL and my PRIMARY KEY wound up in schema SYSIBM called SQL.... not schema is3 called primary. The index registation did wind up there. Obviously there's something I don't understand or see. If you have some insight into this problem that you are willing to share, I'd be grateful. One possibility is the uniqueness required: is the constraint required to be unique with table, schema or database? PS: Ignore the...
12
5321
by: Whoever | last post by:
Hi, I'm trying to return an XmlDocument or XmlNode converted from a typed dataset. public XmlNode whatever() { MyTypedDataSet ds = new MyTypedDataSet(); return new XmlDataDocument(ds); }
4
2513
by: joewhitehair | last post by:
Using a schema, I created classes for my web service using the XSD.exe tool. With the classes I then wrote a web service function, but the WSDL that is generated has different minOccurs and maxOccurs values for my elements. I can seem to figure out why this has changed or how to correct it. I can't find any way to manually modify the classes for minOccurs or maxOccurs either. It can happen whether I explicitly set the minOccurs/maxOccurs...
2
1324
by: Mark Doerbandt | last post by:
Hi, I need to validate a XML document where some of the element names are unnknown. <xs:any processContents="lax"/> or "skip" allows me to do so. But: I want to validate those elements - the only thing unknown is the name of the elements. Especially the attributes are well-known and need to be validated.
3
1833
by: Sathyaish | last post by:
Thingies: - Report Definition Language (RDL) - Resource Definition Framework (RDF) - Web Service Definition Language (WSDL) - C# Application Markup Language (CSAML) - Extensible Application Markup Languaeg (XAML) - Or, HTML itself? Are these not specific vocabularies? OK, HTML may not have very rigid
5
1768
by: Norsoft | last post by:
How can I convert an existing schema to an xml document. I have a series of schema documents that validate xml documents that describe metadata. Normally the metadata is first defined in a database, then schemas generated from the database. In this case I was given the schemas and I am writing an application to create the database entries. I can't use a dataset as we have a very specific database structure. I started to load them into an...
0
9927
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
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11098
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10717
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
10814
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
10401
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.