How do you specify in XML Schema, that an element may either have an attribute present, or a content, but not both.
Example:
Here the element <sig> can either be expressed as :
<sig href="http://static.domain.tld/1231231.sig"/>
or having it's content placed inline :
<sig>
8374a32f4c2de
12B8374a32f4c
2de12B8374a32
f4c2de12B8374
a32f4c2de12bd
</sig>
Cheers.
EDIT : typos
Due to my knowledge this is one of the places, where XML Schema is inferior to Relax NG, where
you can specify:
<element name="sig">
<choice>
<attribute name="href">
<data type="anyURI"/>
</attribute>
<data type="hexBinary"/>
</choice>
</element>
Related
Issue: Unable to create java classes from cXML.dtd using java xjc
version I am using is 1.2.032
command used : xjc -dtd cXML.dtd
Error :
parsing a schema...
[ERROR] Property "Name" is already defined. Use <jaxb:property> to resolve th
is conflict.
Issue 1 : Line number around 573
issue is with the "name" as its duplicate (element as well as attribute).
issue 2:
ShippingPaymentMethod,TermsOfDeliveryCode,TransportTerms uses "value" which is causing duplicate definations.
Solution as I understand==
I need custom binding.xml .. I tried various ways but unable to create correct binding.xml to solve this issue. once I have correct xml I can use following command to create generated classes.
xjc -b binding.xml -dtd cXML.dtd
What I help I need
please provide correct binding.xml if possible
Is there any alternate way to generate java mappings for this cXML
Is there possibilities to have XSD and then have java mapping from XSD?
Please suggest.
FYI: you can also solve this with an external jax-b binding file that looks like this:
<xml-java-binding-schema xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<element name="ReturnData" type="class">
<attribute name="name" property="nameAttribute"/>
</element>
<element name="ShippingPaymentMethod" type="class">
<attribute name="value" property="valueAttribute"/>
</element>
<element name="TermsOfDeliveryCode" type="class">
<attribute name="value" property="valueAttribute"/>
</element>
<element name="TransportTerms" type="class">
<attribute name="value" property="valueAttribute"/>
</element>
</xml-java-binding-schema>
The CXML spec is VERY annoying to generate JAX-B classes for because of their continued use of DTD over XML schema. This is especially annoying if you want to use the other DTDs (Invoice, Catalog, Fulfill) as they each redefine all the common elements but use their own versions of the cxml.requests, cxml.messages, cxml.responses entities
Issue Resolved by myself. did following steps.. may be useful for others
Renamed "name" element "ReturnData" in Cxml.dtd
Renamed "value" attr from TransportTerms,ShippingPaymentMethod,and TermsOfDeliveryCode in Cxml.dtd
Created java classes using
xjc -dtd cXML.dtd
4.in Generated java classes changed xml annotation back to original.
So method names will be different but it will read and write correct XML.
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>
Intuitively, "extension" means add something to base type, not to modify base type. The following XSD
<complexType name="B">
<attribute name="A1" type="int" use="required" />
<anyAttribute namespace="##other" processContents="strict" />
</complexType>
<complexType name="D">
<complexContent >
<extension base="tns:B">
<!--???-->
<attribute name="A1" type="int" use="optional" />
<anyAttribute namespace="##other" processContents="lax" />
</extension>
</complexContent>
</complexType>
should not compile. But XML Schema compiler(System.Xml.Schema.XmlSchema) do not throw errors. What's the rational of this counter-intuition design?
There is a problem with the Schema:
In D, you are trying to add attribute A1 a second time.
Listing something in the extension adds it to the definition. In this case "A1" is already there. Any extended instance needs to be a valid instance of the base. If A1 were not present it would not be a valid member of the base.
It looks like you need to restrict the B type if you want to make it optional.
Im writing a XML schema for a project. I cannot solve following problem:
A element cannot be nested by itself, ex:
<document>
<text>
<b>
<i>
<a link="http://wikipedia.org">
<b />
</a>
</i>
</b>
</text>
</document>
This example shouldn't be allow because the b is nesting itself. So my question for you is: "Is it possible to disallow a element to nest it self, and if yes whats the procedure to do the trick?"
Thx in advantage!
\Morten Møller
Edit:
Until now I only have made sure that a element can be a child of itself, but not that a element cant have a descendant that is itself.
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://cs.au.dk/dWebTek/WikiXML"
targetNamespace="http://cs.au.dk/dWebTek/WikiXML"
elementFormDefault="qualified">
<element name="wiki">
<complexType>
<choice maxOccurs="unbounded">
<!-- A lot of other element is listed here -->
<element name="bold" type="xs:boldnest"/> <!-- Missing nest function -->
</choice>
<complexType>
</element>
<complexType name="boldnest">
<choice maxOccurs="unbounded">
<element name="bold" minOccurs="0" maxOccurs="0" type="xs:boldnest"/>
<!-- All the other element is copy pasted in here -->
</choice>
</complexType>
What you are trying to do is not possible. In XML Schema, if you are using a type-based approach, you can only control the children of an element through the content model, not all possible descendants.
The only way you could possibly get close to what you are trying to do is to fully define the contents of document down to the last level. But you cannot establish a recursive structure and then put in place the sort of constraint you are thinking of.
You will need to validate this using some other mechanism, after XML schema validation is done.
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.