What I'm trying to do is, declare an parent element called "data", which are having 10 sub element of these one element are conditional.
My XSD is:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="data" >
<xs:complexType>
<xs:sequence>
<xs:element name="sub_data" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all >
<xs:element ref="A"/>
<xs:element ref="B" minOccurs="0" maxOccurs="1"/>
<xs:element ref="C"/>
<xs:element ref="D"/>
<xs:element ref="E"/>
<xs:element ref="F"/>
<xs:element ref="G"/>
<xs:element ref="H"/>
<xs:element ref="I"/>
<xs:element ref="J"/>
<xs:element ref="K"/>
<xs:element ref="L"/>
<xs:element ref="M"/>
<xs:element ref="N"/>
<xs:element ref="element_group"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="status"/>
</xs:complexType>
</xs:element>
<xs:element name="O" type="xs:string" substitutionGroup="element_group">
<xs:element name="P" type="xs:string" substitutionGroup="element_group">
<xs:element name="Q" type="xs:string" substitutionGroup="element_group">
</xs:schema>
Requirement is:
All element from A to N are appearing in any order.
Element P,Q and R is also part of data but only one element appear from among 3. Order is also any.
More important I have cover 1st and 2nd point but I want one more restriction is that only and only four element will be become the part of <data> that means element count from <A> to <Q> is exact four,
Combination can any of them from <A> to <Q> but final count is only four, please help me.
Now currently i am unable to set maxOccures in <all> , it not compiling the xsd after setting maxOccures.
The simplest way to handle this is probably to use XSD 1.1 and use an assertion on the parent to specify that there must be exactly (or at most) four children. You will also need to make each child of the all-group optional, since thirteen of them will not appear.
The best way might be to redesign your XML to work better with your schema language instead of fighting it. It's hard to give advice on that, though, since your example is abstract enough to make it unclear why you are imposing the requirements you mention.
Related
I have an Xml file like this:
<Configuration xmlns="http://schemas.benefittech.com/evolution/site">
<SiteSettings>
<ProfileGroup>TBOLoanPmtElection</ProfileGroup>
<AWSGroup>TBOLoanPmtElection_Admin</AWSGroup>
</SiteSettings>
</Configuration>
And I have an XSD file like this:
<?xml version="1.0" encoding="us-ascii" ?>
<xs:schema elementFormDefault="qualified" attributeFormDefault="unqualified" version="5.0"
xmlns="http://schemas.benefittech.com/evolution/site" targetNamespace="http://schemas.benefittech.com/evolution/site"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vs="http://schemas.benefittech.com/Visual-Studio-Intellisense" vs:friendlyname="Evolution Site Configuration">
<xs:element name="Configuration">
<xs:complexType>
<xs:all>
<xs:element name="SiteSettings" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:all>
<xs:element name="AWSGroup" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="ProfileGroup" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="LogActivityGroup" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
I want to enable my Xml to look something like the following to allow for 'environment overrides' (the E attribute on the env:* element):
<Configuration xmlns="http://schemas.benefittech.com/evolution/site" xmlns:env="http://schemas.benefittech.com/evolution/siteenvironment">
<SiteSettings>
<ProfileGroup>TBOLoanPmtElection</ProfileGroup>
<AWSGroup>TBOLoanPmtElection_Admin</AWSGroup>
<env:AWSGroup E="SecondEnvironment">TBOLoanPmtElection2_Admin</env:AWSGroup>
</SiteSettings>
</Configuration>
Is there a way to modify the XSD so that given EVERY element in my configuration, I want to optionally have a env:* version of it. Do I have to make a new schema file like below that is basically a duplicate of original except for the target namespace?
<?xml version="1.0" encoding="us-ascii" ?>
<xs:schema elementFormDefault="qualified" attributeFormDefault="unqualified" version="5.0"
xmlns="http://schemas.benefittech.com/evolution/siteenvironment" targetNamespace="http://schemas.benefittech.com/evolution/siteenvironment"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vs="http://schemas.benefittech.com/Visual-Studio-Intellisense" vs:friendlyname="Evolution Site Configuration">
<xs:element name="Configuration">
<xs:complexType>
<xs:all>
<xs:element name="SiteSettings" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:all>
<xs:element name="AWSGroup" type="xs:string" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="E" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="ProfileGroup" type="xs:string" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="E" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="LogActivityGroup" type="xs:string" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="E" use="required" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
Is there a way to modify the XSD so that given EVERY element in my configuration, I want to optionally have a env:* version of it.
No, there is no elegant way to add, after each element, an optional element having the same name but a different namespace. You would need to add each such env:* element individually.
Do I have to make a new schema file like below that is basically a duplicate of original except for the target namespace?
Yes, but only if you stick to this plan of using a different namespace for the 'extension' elements. In your reply to my comment you said
I thought I'd need separate namespaces. I didn't want to change code everywhere that selects elements. For example, SiteConfig.Elements( "SiteSettings" ).Elements( "AWSGroup" ).FirstOrDefault(); Would continue to work without picking up the env:* elements. Then during the publish process, I would swap out the env:* properties everywhere and code would just work. What's your suggestion?
I've hardly written a line of C#, but according to https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=netcore-3.1 the FirstOrDefault() method returns the first element in the sequence or a default. So I think there is a much simpler approach that will not break the existing code...
On each element that needs to have option extension element(s):
Set maxOccurs="unbounded" (or "2", if you only want to allow one extension element)
Add the attribute "E" and make it optional (so that existing documents which omit it on the first occurrence are still valid).
The name "E" for the attribute is not ideal - I would pick something more descriptive if possible.
I have a XSD with XML elements A, B, and C.
I would like these to appear in a parent element in this way:
A and B can appear any times, C only once, optionally.
The order does not matter, but A and B should ideally be "together",
(so it's rather (A*), C?, (B*) | (B*), C?, (A*)), but the "A and B together" is not necessary).
How can I achieve that with XSD?
Actual XSD what I have tried:
<!-- Hint -->
<xs:element name="hint">
<xs:complexType mixed="true">
<xs:sequence>
<xs:choice>
<xs:element ref="tag" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="link" minOccurs="0" maxOccurs="unbounded" />
</xs:choice>
<xs:sequence>
<xs:element name="message" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:choice>
<xs:element ref="tag" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="link" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
But that gives me
cos-nonambig: <ns>:link and <ns>:link (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.
Basically, the need is to have any mix of A, B, C, where C appears
only once.
XSD 1.1 Solution
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:complexType name="SolutionType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="A"/>
<xs:element name="B"/>
<xs:element name="C"/>
</xs:choice>
<xs:assert test="count(c) <= 1"/>
</xs:complexType>
</xs:schema>
The above xs:assertion assumes that you mean "C appears at most once" but can easily be adjusted to require C to appear exactly once.
A and B can appear any times, C only once, optionally. The order does
not matter, but A and B should ideally be "together"
XSD 1.0 Solution
You'll have to impose an ordering to avoid violating Unique Particle Attribution:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="SolutionType">
<xs:sequence>
<xs:element name="A" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="C" minOccurs="0"/>
<xs:element name="B" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
At least this achieves your preference toward keeping A and B together.
The "Unique Particle Attribution" basically means that every element during validation needs to be deterministically assignable to certain XSD "branch". And that can be checked when validating the XSD.
I found out that the (only?) way to avoid abiguity is to list all combinations of A,B,C in a <xs:choice>, while setting minOccurs="1" as appropriate to break the ambiguity. That gives quite a few choices, but for 3 elements, still doable.
This is for the case where I leave 'C' in the beginning:
<xs:sequence>
<xs:element name="message" minOccurs="0" maxOccurs="1"/>
<xs:choice>
<xs:sequence>
<xs:element ref="link" minOccurs="1" maxOccurs="unbounded" />
<xs:element ref="tag" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:sequence>
<xs:element ref="tag" minOccurs="1" maxOccurs="unbounded"/>
<xs:element ref="link" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:sequence/>
</xs:choice>
</xs:sequence>
Not closing this question, I believe there's a better option even in XSD 1.0.
After importing a complextype in one xsd from another xsd, is it possible to have selected subelements of the complex type in the second xsd
What I mean is :
I have first xsd. -> AddressFile.xsd .In AddressFile.xsd, there is a complex type IndividualAddress, with 5 subelemnts. In the next xsd, Individual.xsd , I am referring to the complext type IndividualAddress in AddressFile.xsd. But in Individual.xsd, I just want DoorNO , StreetNum and State from the complex Type IndividualAddress directly . Is that possible. Is there any restrictions I can use.
AddressFile.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://abc/DP/Address">
<xs:complexType name="IndividualAddress">
<xs:annotation>
<xs:documentation>Address of an individual
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="DoorNO" type="xs:String"
nillable="true" minOccurs="0"/>
<xs:element name="StreetNum" type="xs:String"
nillable="true" minOccurs="0"/>
<xs:element name="State" type="xs:String"
nillable="true" minOccurs="0"/>
<xs:element name="Country" type="xs:String"
nillable="true" minOccurs="0"/>
<xs:element name="Pin" type="xs:String"
nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Individual.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:address="http://abc/DP/Address"
targetNamespace="http://abc/DP/Individual">
<xs:import namespace="http://abc/DP/Address"
schemaLocation="AddressFile.xsd"/>
<xs:complexType name="Individual">
<xs:annotation>
<xs:documentation>Address of an individual
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Name" type="xs:String"
nillable="true" minOccurs="0"/>
<xs:element name="Age" type="xs:int"
nillable="true" minOccurs="0"/>
<xs:element name="DoorNO" type=" address:IndividualAddress "
nillable="true" minOccurs="0"/>
<xs:element name="StreetNum" type=" address:IndividualAddress "
nillable="true" minOccurs="0"/>
<xs:element name="State" type=" address:IndividualAddress "
nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Your best approach depends on what you are trying to accomplish.
If you want XSD-aware tools to know that complex type Individual is derived from complex type IndividualAddress, you can derive the one from the other. You will need to do so in two steps: a restriction step to get rid of the unwanted child elements of the base type and then an extension step to add more elements, at the end. (Not at the beginning.)
If you just want to reuse the elements, the creator of the base schema has done the best job they can to hinder you, by making all the children of IndividualAddress local types, which cannot be referred to from elsewhere. If they wanted to make reuse easy, or even possible, they would (a) make the elements in question top-level elements, and/or possibly (b) use named model groups to package up groups of useful elements to be used together.
This has been driving me mad for hours. I've read every relevant XSD question on SO and the rest of the Internet it seems and still the answer eludes me.
I need an XML schema that requires at least one of list of elements be present, but each element may appear only 0 or 1 times.
This is similar to this question:
XML schema construct for "any one or more of these elements but must be at least one"
but I was unable to constrain the upper limit: I am apparently using maxOccursincorrectly.
Here's where I left off with my schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Selects">
<xs:sequence minOccurs="2" maxOccurs="4">
<xs:choice>
<xs:element name="aaa" minOccurs="1" maxOccurs="1"/>
<xs:element name="bbb" minOccurs="1" maxOccurs="1"/>
<xs:element name="ccc" minOccurs="1" maxOccurs="1"/>
<xs:element name="ddd" minOccurs="1" maxOccurs="1"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:element name="baseElement">
<xs:complexType>
<xs:sequence>
<xs:element name="MyChoice" type="Selects"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I've tried the minOccurs and maxOccurs on the choice and the element with no luck. Here is XML that validates, though I don't want it to:
<?xml version="1.0" encoding="UTF-8"?>
<baseElement xsi:noNamespaceSchemaLocation="myTest.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyChoice>
<ddd/>
<ddd/>
</MyChoice>
</baseElement>
Here's an example of what I would like, if possible:
<?xml version="1.0" encoding="UTF-8"?>
<baseElement xsi:noNamespaceSchemaLocation="myTest.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyChoice>
<ddd/>
<aaa/>
<ccc/>
</MyChoice>
</baseElement>
I would like it to complain about the multiple ddd elements but allow any or all of the others in any order. I get an error if I only have one element under MyChoice so at least something works.
What am I doing wrong? How do I prevent multiple of the same element from validating?
UPDATE
This was my solution (from comments on answer below):
Actually, xs:all did the trick. I swapped the choice for all and added minOccurs="0" maxOccurs="1" to each element. With xs:all, minOccurs must be either 0 or 1 and maxOccurs must be 1. Thanks for your help - I'm good to go now!
Just move the <xs:sequence minOccurs="2" maxOccurs="4"> from around the choice to the point where you want to use it further down. (you can also remove the min/max occurs = 1 as this is what xs:choice does)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Selects">
<xs:choice>
<xs:element name="aaa" />
<xs:element name="bbb" />
<xs:element name="ccc" />
<xs:element name="ddd" />
</xs:choice>
</xs:complexType>
<xs:element name="baseElement">
<xs:complexType>
<xs:sequence minOccurs="2" maxOccurs="4">
<xs:element name="MyChoice" type="Selects" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This validates the following:
<baseElement xsi:noNamespaceSchemaLocation="myTest.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyChoice>
<bbb></bbb>
</MyChoice>
<MyChoice>
<ccc></ccc>
</MyChoice>
</baseElement>
UPDATE
I think you are reaching the limit of what you can achieve with XSD. I can't see any way you can do this other than by defining a "version" of the MyChoice type for every single combination possible (which will then need different names MyChoice1, MyChoice2 etc)
You can also use xs:all
<xs:complexType name="Selects">
<xs:all minOccurs=2 maxOccurs=4>
<xs:element name="aaa" />
<xs:element name="bbb" />
<xs:element name="ccc" />
<xs:element name="ddd" />
</xs:all>
</xs:complexType>
but this will not prevent you from having four <ddd/>'s
Using xsd.exe in a C# class, is there a way to produce a xsd file with Nested Type, instead of Global Types?
I want to use this xsd file, with SSIS - Sql Server Integration Services, and look SSIS is not reading my xsd very well.
I want to generate the xsd like this, with Nested Types :
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Country">
<xs:complexType>
<xs:sequence>
<xs:element name="City">
<xs:complexType>
<xs:sequence>
<xs:element name="CityName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CoutryName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
but xsd.exe produce this , with Global Types, and SSIS don't read it. I need to change this xsd manually to be like above.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Country">
<xs:complexType>
<xs:sequence>
<xs:element name="City" type="City">
</xs:element>
<xs:element name="CoutryName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="City">
<xs:sequence>
<xs:element name="CityName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Any suggestion? Or other tool that I can use.
Thanks a lot.
I'll further assume that not "very well" means you're not seeing CountryName in your XML Source output.
The documentation on MSDN is a good reading, although in my opinion it fails to describe why you would encounter the behavior you see.
I believe that it has to do with the way XML Source outputs are being determined. SSIS infers a data set from the XML structure; the top level entity, that corresponds to the root element, is not mapped to an output, so all the attributes associated with it, in your case CountryName, will not show up.
The easiest way to prove it, is to add another root element that wraps your Country (equivalent to having a dummy "root" class that has a Country-type property).
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
If you add the above schema fragment to your schema, you should get your expected results. At the beginning I thought that it has to do with the issue described here; while you can still use the tool to visualize the data set as described by the MSDN link above, in your case, the authoring style you suggested (basically a russian-doll) can't change the outcome.