I have an method in which I need to pass parameters value some like this in the publish methods.
facebookclient. publish("me/events", FacebookType.class,Parameter.with("name", "Party"));
Instead of passing it as three parameters, I am trying to pass it as object storing these three parameters in the object record.
eg:
facebookclient.publish(record);
so that in publish method , I can get this
void publish(Record record)
{
String event= record.getEvent();
}
I am trying to create an xsd for these parameters. Kindly guide me how to do it. for me/events I can create an element in the xsd as
<element name="events" type="string" />
and refer it in an object
<element name="Record">
<complexType>
<sequence>
<element ref="fb:events" />
// how to specify for FacebookType.class,Parameter.with("name", "Party"))?
</sequence>
</complexType>
</element>
Kindly guide me, how to specify the element FacebookType.class,Parameter.with("name", "Party")) in xsd?
You can use the schemagen application (http://pic.dhe.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=%2Fcom.ibm.websphere.wsfep.multiplatform.doc%2Finfo%2Fae%2Fae%2Ftwbs_jaxbjava2schema.html) to do it via command line or JAXB classes (http://jaxb.java.net/guide/Invoking_schemagen_programatically.html) programmatically... this is all assuming you have you classes annotated with the appropriate annotations.
Related
I've made a simple UI definition language for a project and now want to create a schema, for ease of validation. Unfortunately, my XSD skills are quite rusty, and I find myself trying to so something that I'm not even certain is possible.
The UI is made up of "blocks" which can be positioned in relation to one another. In order to simplify the most common use cases, I'd like the referencing attribute to be able to contain any of the strings parent, previous, or next. In order to be as flexible as possible, I'd also like it to be able to point to any element with an ID.
In other words, I'd like the following to be valid:
<ui>
<block id="foo"/>
<block/>
<block anchor="previous"/>
<block anchor="#foo"/>
</ui>
How can I describe this in XSD?
As it turns out, XSD contains a feature which does exactly this — combines two or more types — and I had simply missed it. A union creates a type whose lexical space covers the lexical spaces of all of its member types (in other words, it can contain a value matching any of its subtypes).
With the caveat that IDREFs cannot contain a leading # (it's a direct reference to an ID, not a fragment identifier for a URL), the following schema will validate the example XML. The interesting bits are AnchorType and TreeReferenceType.
<schema targetNamespace="urn:x-ample:ui" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ui="urn:x-ample:ui">
<element name="ui" type="ui:UIType"/>
<complexType name="UIType">
<sequence>
<element minOccurs="1" maxOccurs="unbounded" name="block" type="ui:BlockType"/>
</sequence>
</complexType>
<complexType name="BlockType">
<attribute use="optional" name="id" type="ID"/>
<attribute name="anchor" type="ui:AnchorType"/>
</complexType>
<simpleType name="AnchorType">
<union memberTypes="ui:TreeReferenceType IDREF"/>
</simpleType>
<simpleType name="TreeReferenceType">
<restriction base="string">
<enumeration value="parent"/>
<enumeration value="previous"/>
<enumeration value="next"/>
</restriction>
</simpleType>
</schema>
For example, let us say we have the following schema (listed in http://www.w3.org/TR/xmlschema-0/#NS)
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:po="http://www.example.com/PO1"
targetNamespace="http://www.example.com/PO1"
elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<element name="purchaseOrder" type="po:PurchaseOrderType"/>
<element name="comment" type="string"/>
<complexType name="PurchaseOrderType">
<sequence>
<element name="shipTo" type="po:USAddress"/>
<element name="billTo" type="po:USAddress"/>
<element ref="po:comment" minOccurs="0"/>
<!-- etc. -->
</sequence>
<!-- etc. -->
</complexType>
<complexType name="USAddress">
<sequence>
<element name="name" type="string"/>
<element name="street" type="string"/>
<!-- etc. -->
</sequence>
</complexType>
<!-- etc. -->
</schema>
Can you explain what the purpose of each of the attributes in the "schema" node mean? I have been trying to wrap my head around it, but I don't get it. Please correct me if I am wrong:
I assume xmlns="http://www.w3.org/2001/XMLSchema" refers to elements & attributes that have no prefix.
xmlns:po="http://www.example.com/PO1" seems like it means that anything prefixed with po, refers to this url (example.com/p01).
I don't understand what targetNamespace is for. I also don't understand what qualified or unqualified means.
This is a bit of a terminology minefield, but essentially, xmlns="http://www.w3.org/2001/XMLSchema" and xmlns:po="http://www.example.com/PO1" are there to declare namespaces for the schema document itself. Remember, an XML Schema is just an XML document, and it needs to declare the namespaces it uses just like any other XML document.
targetNamespace is used to define the namespace of the schema's instance documents, i.e. the documents which will conform to your schema. Such documents would declare their namespace to be http://www.example.com/PO1, with whatever prefix they choose e.g. they could use xmlns="http://www.example.com/PO1" or xmlns:po="http://www.example.com/PO1"
I saw an xml schema ( EPP ) whitch used xsd:choice with an element even if we can use xsd:enumeration instead :
<element name="access" type="epp:dcpAccessType"/>
<complexType name="dcpAccessType">
<choice>
<element name="all"/>
<element name="none"/>
<element name="null"/>
<element name="other"/>
<element name="personal"/>
<element name="personalAndOther"/>
</choice>
</complexType>
to make the question clear , I will use this example instead :
<element name="sport" type="sportType"/>
<!-- using choice-->
<complexType name="sportType">
<choice>
<element name="football"/>
<element name="tennis"/>
</choice>
</complexType>
<!-- Or using enumeration-->
<simpleType name="sportType">
<restriction base="string">
<enumeration value="football"/>
<enumeration value="tennis"/>
</restriction>
</simpleType>
an xml example using that schema :
<!--using choice-->
<sport>
<football/>
</sport>
<!--using enumeration-->
<sport>football</sport>
why they prefer xsd:choice instead of xsd:enumeration in this situation ?
Thanks
Choice is for choice between elements, while enumeration allow choice between a set of values. The values can be string like in your example, but if you wanted to enumerate several element objects, then you would have to use choice.
why they prefer xsd:choice instead of xsd:enumeration in this situation ?
Presumably they want a tag instead of text content in the supported xml.
The decision to use one or the other is pretty much a matter of xml you want to support, as they do quite different things. Which xml form is preferable is quite subjective.
See also this related question.
I have a problem with an xsd schema file.
I have this abstract complex type on my schema:
<complexType name="Action" abstract="true">
<sequence>
<element name="actionType">
<complexType>
<choice>
<element name="ALARMACTION"/>
<element name="REPORTDATAACTION"/>
<element name="ENABLEOBSERVATIONACTION"/>
<element name="DISABLEOBSERVATIONACTION"/>
<element name="SETOBSERVATIONSCHEDULEACTION"/>
<element name="VERIFYOVERTIMEACTION"/>
</choice>
</complexType>
</element>
</sequence>
</complexType>
This is a concrete implementation of Action abstract element:
<complexType name="AlarmAction">
<complexContent>
<extension base="ref:Action">
<sequence>
<element name="alarmCode" type="integer"/>
<element name="report" type="string"/>
</sequence>
</extension>
</complexContent>
</complexType>
This element references the abstract Action element:
<complexType name="Conclusion">
<sequence>
<element minOccurs="0" name="observationSet" type="ref:ObservationSet"/>
<element name="action" type="ref:Action"/>
</sequence>
</complexType>
I got an error with this xml instance:
<Conclusion>
<observationSet>
<observationPhenomenum>HIGH_HEARTBEAT</observationPhenomenum>
</observationSet>
<action>
<actionType>
<ENABLEOBSERVATIONACTION></ENABLEOBSERVATIONACTION>
</actionType>
<observationId>1</observationId>
<observationId>2</observationId>
</action>
</Conclusion>
The error on netbeans is this: cvc-type.2: The type definition cannot be abstract for element action. [104]
Can someone help me?
I assume that the schema is valid; you do have somewhere a definition for a global element with the local name "Conclusion", and a non-abstract, complex type deriving from Action, with repeating observationId elements (e.g. XYZAction).
Your problem then is rezolved if you add xsi:type="XYZAction" as an attribute to your action element. Again, the attribute value must match the name of a non-abstract type, that derives from the abstract Action.
My advice to you is when in doubt, use a tool to generate a sample XML for the scenario you have in mind. I am using QTAssistant, since it allows me to easily build any scenario imaginable using simple drag and drop of XML Schema elements.
You can use an abstract complexType as element type, but the user writing a XML instance document with this schema has to state the type of the element.
For your example this means you have to write it as follows:
<Conclusion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="conclusion.xsd">
<observationSet>
<observationPhenomenum>HIGH_HEARTBEAT</observationPhenomenum>
</observationSet>
<action xsi:type="AlarmAction">
<actionType>
<ENABLEOBSERVATIONACTION></ENABLEOBSERVATIONACTION>
</actionType>
<alarmCode>10</alarmCode>
<report>Whatever</report>
</action>
</Conclusion>
For more information hav a look here: http://pic.dhe.ibm.com/infocenter/wci/v6r0m0/index.jsp?topic=%2Fcom.ibm.websphere.cast_iron.doc%2Fmap_Selecting_a_Substitution_Type.html
While validation of request xml against wsdl you have to include following attributes
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" use this in the root element
on abstract type element
<abstractElement name="XYZ" xsi:type="Name of your instance" > </abstractElement>
Say I have these types defined in my XSD:
<complexType name="NamedEntity">
<attribute name="ix" type="positiveInteger"></attribute>
<attribute name="sName" type="string"></attribute>
<attribute name="txtDesc" type="string"></attribute>
</complexType>
<complexType name="Node">
<complexContent>
<extension base="tns:NamedEntity">
</extension>
</complexContent>
</complexType>
<complexType name="Source">
<complexContent>
<extension base="tns:NamedEntity">
<attribute name="dt" type="dateTime"></attribute>
</extension>
</complexContent>
</complexType>
Now I want to express that a Node element may have zero or more child elements that may be of the type Node or Source.
It would be OK if I had to somehow enumerate the allowed types for the children, but since I have more types that inherit from NamedEntity, it would be neat if I could specify just the base type.
Edit: I'd rather not use xsi:type in the document but have a unambigous relationship between element name and type. Quite a lot XML processing seems to depend on that, and I also find it a lot more readable.
Please don't use xsi:type if you can avoid it. It's evil. Ok, maybe I exaggerate, but it does make it impossible to parse the document without intimate knowledge of the schema, which is bad enough in practice.
What will help you is: substitutionGroup.
In the schema, have the Node element contain zero or more child elements of type NamedEntity. In the actual document, use the xsi:type attribute (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance") to choose the subtype ("Node" or "Source") for each one.
This may be beyond the capabilities of XSD. Have you considered doing extra validation using Schematron?
I think you want a substitution group.