I am trying to validate the XML with the schema in C#. I will be having an unknown elements under the row element. I am using xs:any, I am getting the below error
The element 'row' has invalid child element 'Name'.
Schema -
<xs:element name="table">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="row">
<xs:complexType>
<xs:sequence>
<xs:any processContents="lax"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
XML -
<table>
<row>
<ProductID>994</ProductID>
<Name>LL Bottom Bracket</Name>
<ProductModel>LL Bottom Bracket</ProductModel>
<CultureID>en </CultureID>
<Description>Chromoly steel.</Description>
</row>
</table>
You've not specified a maxOccurs on the xs:any, and maxOccurs defaults to 1, which means that the second element, Name, is not allowed, thus the error message,
The element 'row' has invalid child element 'Name'.
Correct by adding maxOccurs="unbounded" to xs:any:
<xs:any processContents="lax" macOccurs="unbounded"/>
Related
I have the following XML:
<Person>
<Name>James</Name>
<Age>18</Age>
</Person>
In the XSD that validates this XML, I want to add another element. I want this element to be optional but with a default value - so if it is missing when the XML is validated, it is populated with the default value. Let's say the new element is "NumberOfSiblings" which defaults to 0. I've done this like so:
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Age" type="xs:integer" />
<xs:element name="NumberOfSiblings" type="xs:integer" minOccurs="0" default="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
So now let's assume I come to parse the XML above in .Net / C#. Is there a way to validate it so it would automatically create the element with the default value, and therefore the parser would not fall over? If not, what's the correct way to obtain this default value from the XSD?
I'm following the solution from this question How to generate .NET 4.0 classes from xsd? to generate C# classes. But somehow it only generates the first element. Is there any way that I can generate all elements at same time?
Xsd doc looks like below:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="advice_file">
</xs:element>
<xs:element name="vendorMerchID" type="xs:string">
</xs:element>
</xs:schema>
The reason you only get 1 element is because to be valid the xml must always have a single outer root node. What is suprising to me is that the second node was just ignored completely and no exception was thrown.
Your xsd represents the following xml
<advice_file/>
To have both elements, you need to write you xsd as follows:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="myRoot">
<xs:complexType>
<xs:sequence>
<xs:element name="advice_file" type="xs:string"/>
<xs:element name="vendorMerchID" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
for the xml structure as:
<myRoot>
<advice_file/>
<vendorMerchID/>
</myRoot>
Alternatively, if your xml is like this:
<advice_file>
<vendorMerchID/>
</advice_file>
Use the xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="advice_file">
<xs:complexType>
<xs:sequence>
<xs:element name="vendorMerchID" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I'm trying to validate XSD aginst XML but getting an error
The element 'Table' has incomplete content. List of possible elements expected: 'IP21TAG'.
XML:
<NewDataSet>
<Table>
<SITE>VMD</SITE>
<TANK>65-12-392</TANK>
<SERVICE>HZLPG</SERVICE>
</Table>
<Table>
<SITE>VMD</SITE>
<TANK>65-12-392</TANK>
<SERVICE>HZLPG</SERVICE>
<IP21TAG>BC-BBH-OS-4LI21392</IP21TAG>
</Table>
</NewDataSet>
XSD:
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="NewDataSet">
<xs:complexType>
<xs:sequence>
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="SITE" type="xs:string" />
<xs:element name="PLANT" type="xs:string" />
<xs:element name="TANK" type="xs:string" />
<xs:element name="SERVICE" type="xs:string" />
<xs:element name="IP21TAG" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Can anyone help me how to solve this?
Thanks in advance.
Obviously, solution is to set minOccurs="0" on elements that are optional.
However, error message in Visual Studio displayes even optional elements in the "possible element name" list, so it is not obvious if you miss minOccurs on an element.
My problem was that ONE element missed the minOccurs="0", and error message listed ALL, approximately 100, elements...
You omit minOccurs in <xs:element> node of your schema, its default is one (see specifications) then if you don't specify that node your XML won't be validated against that schema.
If that node is optional simply change your XSD to reflect that. Here I changed just IP21TAG and PLANT (because they're both not present in your example XML but if others are optional too you should change them accordingly):
<xs:element name="IP21TAG" type="xs:string" minOccurs="0"/>
<xs:element name="PLANT" type="xs:string" minOccurs="0" />
If that element is not optional then to be wrong is your XML, you may - for example - provide an empty string instead of a missing node:
<NewDataSet>
<Table>
<SITE>VMD</SITE>
<TANK>65-12-392</TANK>
<SERVICE>HZLPG</SERVICE>
<IP21TAG></IP21TAG>
<PLANT></PLANT>
</Table>
</NewDataSet>
I need to fix the error message below:
Error occured while validating xmlThe element 'UserFields' has invalid child element 'LastApproverID'. List of possible elements expected: 'FirtApproverID'.
Below is my xsd for validating the xml schema :
<xs:element name="UserFields" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="FirtApproverID" type="xs:string" minOccurs="1" />
<xs:element name="LastApproverID" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
The order of the firstapproverid and lastapproverid does not matter but the tag need to occur atleast once.
i.e it can appear
<UserFields>
<LastApproverID>123</LastApproverID>
<FirtApproverID>456</FirtApproverID>
</UserFields>
OR
<UserFields>
<FirtApproverID>456</FirtApproverID>
<LastApproverID>123</LastApproverID>
</UserFields>
The <xs:all> indicator can solve your problem.
See here for answer
Please, improve your Google skills.
I am trying to write some XML schema code to specify that a particular element 'abc' may have a child element with name 'xyz', and that element may have any attributes, and any child elements.
At the moment I have this:
<xs:element name="abc">
<xs:complexType>
<xs:sequence>
<xs:element name="xyz">
<xs:complexType>
<xs:sequence>
<xs:any/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
But when I validate my XML against the schema, I get validation failures complaining about the child elements of the xyz element.
Your xs:any doesn't have any information attached to it, so it is looking for schema defined elements. If you want to be slack in your interpretation of the sub-elements, try this:
<xs:element name="abc">
<xs:complexType>
<xs:sequence>
<xs:element name="xyz">
<xs:complexType>
<xs:sequence>
<xs:any processContents="lax" />
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
This should get you past validation. If you have any expectation on the well-formedness of xyz's content, you can include a namespace using the namespace attribute for xs:any, and pull in another schema for that information.
Good luck, and I hope this helps!