Creating an XSD schema - c#

I have an xml tag:
<ROW field1="value 1" field2="value 2" ... />
fieldi has a string value, and number of attributes fieldi is variable, but not less than 1. Is it possible to create an xsd schema for this tag?
possible xml document
<ROWDATA>
<ROW field1="dfgdf" field2="ddfg"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd"></ROW>
<ROW field1="dfedf" field2="djkfg" field3="cdffd" field4="dfedf" field5="djkfg" field6="cdffd"></ROW>
</ROWDATA>
in this xml document, which I receive from a web server, can be a variable number of attributes field (I noted them as fieldi, where i means the order of a specific attribute field)
So I have, unknown number of ROW elements and unknown number of field attributes in the ROW element
Thanks

If you're using Visual Studio 2008:
Open your Xml file in Visual Studio
Go to the 'Xml' menu option at the top of the screen
Choose 'Create Schema'
This will generate your xsd schema(s)
EDIT
Try this example for details on setting minOccurs (on elements) or required (on attributes) so you can manipulate your derived schema.

If you are not comfortable in writing XSD yourself, use some generator like this.
EDIT: Based on your XML in comments, I can think of below structure of XSD
<xsd:element name="FieldHeader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Fields" type="xsd:string"/> <!--use minOccurs maxOccurs here-->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="fieldi">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="Fields">
<xsd:list itemType="fieldi" />
</xsd:simpleType>

I think I have understood your requirement. Just to avoid misconceptions, let me reveal what I have understood once:
"You have an xml file which contains an element with the name fieldi, comes with set of some unknown attributes. Which means you don't know [or say don't want] the names and values of those attributes. Just want to see, there is at-least 1 attribute appearing",
Well. sorry to say that, this requirement is running out of capability of XML-schema. :-[
You cannot have attributes undeclared in schema. If it appears in xml, it requires to have a proper definition for that. There is something called <anyAttribute/> [click-here] which again requires definition [somewhere, in another linked schema].
1) Defining all the possible attributes making use="optional", doesn't look practically possible. And also your last requirement go skipped.
2) If it is possible then, convert all the attributes to elements [using transformation, or you can either ask the sender to do so, I don't know how complicate it is in your case], And define element <any/>, which sounds somewhat comfortable. but your requirement [at-least one attribute must appear] is still not achieved.
so this is it I can add-up for you. If you can change the requirement or input xml structure then let me know, I will see, whether I can help you in any-other ways ..
regards,
infant-pro

I solved the problem but in other way, by controlling the deserialization of the xml document in the way I need. However, I don't like this solution, because I had wanted to create classes from the xsd scheme and use them in my code.
Anyway, thanks to everybody

Related

Is having a scalar type and element inside a parent element bad design in XML?

We currently have the following structure as part of an API endpoint response.
<elements>
<element arg1="" arg2="">id</element>
</element>
Now, I would like to add a sub-element to the element without changing the existing structure. This is what I had when adding the sub-element.
<elements>
<element arg1="" arg2="">id
<subelement arg1="" arg2="" />
</element>
</element>
It seems like the easiest solution and as far as I read it's valid XML.
But is it bad design? Would the API users be able to handle this well in their languages (mostly C#)?
Your candidate design uses mixed-content for data-oriented XML. You're right to suspect that it's non-ideal. Users will not be able to leverage the benefits of markup to access id separately from subelement; parsing and XPath access would be harder than necessary. If you're determined to use XML for data-oriented content, it would be better to have id wrapped in its own tag (or to use attributes for scalars -- see XML Element vs XML Attribute).
More broadly, realize that using XML for data-oriented content has been out-of-favor for good reasons for more than a decade. New data-oriented APIs should not be based on XML. JSON is a commonly preferred alternative.
XML in general and mixed-content in particular are best used for document-oriented data.
See also
Can an XML element have both text and element content?
Need example of mixed content in XSD
What is a markup language? (XML vs YAML vs JSON)
XML vs JSON to represent database data

Generate C# class from XSD (xhtml.blkstruct.class)

I have an xsd file, simplified as below with an element referencing the xhtml.BlkStruct.class. I have tried xsd.exe and xsd2code trying to generate a C# class from it, but I am always getting errors.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xs:complexType name="test">
<xs:group ref="xhtml.BlkStruct.class"/>
</xs:complexType>
</xs:schema>
This is the error (reference to an undeclared / not declared model group)
Error: Verweis auf eine nicht deklarierte Modellgruppe 'http://www.w3.org/1999/xhtml:xhtml.BlkStruct.class'.
Can anybody help me out what is going wrong here?
Background I am tryint to XmlSerialize a string containing (simple) HTML/XML mixed with sting content, so things like
&ltp&gt, &ltdiv&gt, etc
for example
Hello &ltu&gtunderlined text&lt/u&gt &ltb&gtbold text&lt/b&gt world
Well, you're not supplying the xhtml.BlkStruct.class anywhere in your XSD. You might need to include its definition to succeed in the code generation. Download the file from http://www.w3.org/MarkUp/SCHEMA/ and add a
<xs:include schemaLocation="xhtml-basic11-model-1.xsd">
But do you really need a strongly typed DOM? A simpler solution is to define your test field as string, and include your HTML as CDATA or encoded html. My experience is that mixing XML with HTML is always a bad design choice and you have better luck with treating the HTML part as text.

XML validating with XSD failing due to 'refname' not declared despite refname not allowed for schemas

I am trying to validate an XML via an XSD in c# code. However it keeps throwing an XmlSchemaValidationException, namely "The 'refname' attribute is not declared.".
The code doing the validation:
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
xmlSettings.Schemas.Add(null, #"\\[Network-drive path]\KVSchemaMod.xsd");
xmlSettings.ValidationType = ValidationType.Schema;
xmlSettings.DtdProcessing = DtdProcessing.Parse;
xmlSettings.ValidationFlags = XmlSchemaValidationFlags.AllowXmlAttributes;
xmlSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema;
xmlSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
XmlReader reader = XmlReader.Create(#"\\[Network-drive path]\KV_Article.xml", xmlSettings);
// Parse the file.
while (reader.Read()) ;
The XSD (with repetitous parts removed):
<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xs:annotation>
<xs:appinfo>
<sql:relationship name="ProductIdentifier"
parent="tblKVProduct"
parent-key="record_reference"
child="tblKVProductIdentifier"
child-key="record_reference" />
... (More SQL-mappings)
<xs:element name="ONIXMessage" sql:is-constant="1">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Product" sql:relation="tblKVProduct">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="RecordReference" sql:field="record_reference" >
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
...(More elements under Product)
The XML to be validated (ONIX-standard):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix- international.dtd">
<ONIXMessage>
<Product>
<RecordReference>0786606274</RecordReference>
<NotificationType>03</NotificationType>
<ProductIdentifier>
<ProductIDType>01</ProductIDType>
<IDTypeName>Kustantajan tuotenumero</IDTypeName>
<IDValue>95535</IDValue></ProductIdentifier>
<ProductIdentifier>
<ProductIDType>02</ProductIDType>
<IDValue>0786606274</IDValue></ProductIdentifier>
<ProductIdentifier>
<ProductIDType>03</ProductIDType>
<IDValue>9780786606276</IDValue>
</ProductIdentifier>
...(And more Product-fields)
The XML also has a Header tag as the first child of ONIXMessage (followed by all Product tags that as also childrens of ONIXMessage), but since none of the fields there are referenced in the schema I don't see how they could cause this particular error.
As I googled the error I read up on the XmlSchemaElement.RefName property (MSDN Definition) it even explicitly says that "The value cannot be set if the containing element is the schema element."
Since that is the case for the XSD I don't understand why it throws an error about it not being declared.
I've been trying to find out if this could be related to the lack of namespace, but in my mind that shouldn't be a problem since there is none in neither the XML nor the XSD. Or does one need to force a namespace onto the root-element in order to validate the XML?
The validating code does work with another set of XML/XSD (very different build-up on those though), could that just be a fluke though and the error actually in the way I validate?
Just for completion's sake, the Header tag in the XML:
<Header>
<FromCompany></FromCompany>
<FromPerson></FromPerson>
<FromEmail></FromEmail>
<AddresseeIdentifier>
<AddresseeIDType></AddresseeIDType>
<IDTypeName></IDTypeName>
<IDValue></IDValue>
</AddresseeIdentifier>
<ToCompany></ToCompany>
<ToPerson></ToPerson>
<MessageNumber></MessageNumber>
<SentDate></SentDate>
<MessageNote></MessageNote>
</Header>
It's not possible to reproduce your problem, given that you haven't provided a complete example. If you want answers based on anything other than speculation, you need to cut down your schema and your data to the smallest example that illustrates the problem, so question answerers can reproduce the problem and study it in more detail. It will often happen, of course, that your problem dissolves in the process because you come to see what's going wrong in the process of finding a small example of the problem. But that's just a risk you have to take.
That said, however, I'll go ahead and speculate. The RefName property on the XMLSchemaElement object is almost certainly a red herring. The message makes it sound much more as if the XML you are attempting to validate has an attribute named refname somewhere (in the bit you elided), and your XSD schema has no definition for it. Certainly the XSD fragment you provide has no definition for it.
The vocabulary defined at http://www.editeur.org/onix/2.1/reference/onix-international.dtd (which you point to in your XML), however, does define refname as an attribute on many elements (probably all of them, though I haven't checked each one individually). So it's not at all implausible that an ONIXMessage element might contain a descendant with a refname attribute.
Actually, it's not just not implausible; it's certain.
The DTD pointed to by your XML supplies a default value for the refname attribute on all elements (at least, all the ones I've seen), for which your XSD schema declaration appears not to be prepared. That is, the first start-tag of your document seen by the schema validator is not <ONIXMessage> but <ONIXMessage refname="ONIXMessage" shortname="ONIXmessage">.
If you're not using the XSD available from www.editeur.org, the simplest way forward is probably to reconsider that decision and use the schema from editeur.org instead of rolling your own. It includes declarations for the refname and shortname attributes. If you are using that schema, or rather trying to use it, something has gone badly wrong and your validator is not finding or reading it. In that case, your best way forward is to figure out why your system is not finding the correct schema documents.

Is it valid to set nillable=true and use the default property in an XML document?

Using C# and .Net 4.0
I have a generated schema that looks like this:
<xsd:element name="EstimatedDate" minOccurs="0" nillable="true" default="1900-01-01T00:00:00">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:dateTime">
<xsd:attribute name="origVal" type="xsd:dateTime" use="optional" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
When I serialize the object with a null value I get:
<EstimatedDate xsi:nil="true" />
But I am getting a deserialization: "There must be no fixed value when an attribute is 'xsi:nil' and has a value of 'true'."
When I look at the XML specification I do not see that nillable and default properties are mutually exclusive, but my other dateTime XML types that are nillable but do not have a default property work correctly.
The error message describes a constraint that is present in the spec: Validation Rule: Element Locally Valid (Element) clause 3.3.2 says that when xsi:nil=true, there must be no fixed value. However, there is no ban on a default value, as far as I can see, so it seems your schema processor is over-eager to find fault.
I think the correct behavior for your schema is: if the element is empty and xsi:nil is absent or false, use the default value; if the element is empty and xsi:nil is true, leave it as is.
(you can try getting Microsoft to fix this, or you can try switching to Saxon...)
I think that maybe it is one of those confusing areas in the XML Schema specification (interesting enough, even 1.1 spec is only dissalowing default and fixed combination); if you consider that default values for elements apply when the elements are present and empty, and that nilled elements must not have any content, then it kind of make sense to get confused... which one is it: null or default? In other words, when both conditions are present, which one takes precedence? I guess the deserializer is kind of asking for help there...

c# XSLT transform doesnt work if the XML file has a schema attached?

We have an odd problem, we are tranforming a pretty complicated XML file using several XSLT files, this isnt the issue.
The issue is that IF the XML file is attached to the schema, the transform doesnt work, if we remove the schema declaration it begins to work ok.
Any clues what the problem would be?
Here is the schema declation
<xs:schema id="play"
targetNamespace="highway"
elementFormDefault="qualified"
xmlns="highway"
xmlns:mstns="highway"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
And we are simply using the following code to link it (Visual Studio Intellisense then kicks in)
<helloElement name="hello" xmlns="highway">
I appreciate this isnt much to go on, not sure what to offer in terms of symptoms, let me know if you need any info.
Many thanks!
The problem is not the schema, the problem is the namespace declaration xmlns="highway" which your stylesheet(s) need to take into account with e.g.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:hw="highway"
version="1.0">
<xsl:template match="hw:helloElement">
...
</xsl:template>
</xsl:stylesheet>
and so on, anywhere you match or select an element you need to use a prefix.
When you add the schema declaration, you are adding a default namespace to your XML document (xmlns="highway") which wasn't there before. This will then affect the interpretation of element references and XPATHs in the XSLT, because all your elements are now no longer <someElement>, they are <highway:someElement>. Check out this link for more information.

Categories