I have such element
<xsd:element name="Car" type="carType"/>
<xsd:complexType name="carType">
<xsd:complexContent>
<xsd:extension base="basicType">
<xsd:attribute name="motor" type="xsd:IDREF" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
When motor element in the current document, it is work fine.
<Car id="car1" motor="motor1"/>
<Motor id="motor1"/>
But when I would like to import motor element from another file
<beans:bean:import resource="motors.conf.xml"/>
Intellij Idea say Invalid id reference, and when I run program I get an exception
There is no ID/IDREF binding for IDREF
May be I'm doing something wrong? Or may be xsd:IDREF equals ref local, and so I can't use it with import?
I was right, xsd:IDREF equals ref local.
About xsd:IDREF MSDN Creating Valid ID, IDREF...
And you can see why it equals here -
<xsd:element name="ref">
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines a reference to another bean in this factory or an external
factory (parent or included factory).
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:attribute name="bean" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the referenced bean.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
**<xsd:attribute name="local" type="xsd:IDREF">**
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the referenced bean. The value must be a bean ID and thus can
be checked by the XML parser. This is therefore the preferred technique
for referencing beans within the same bean factory XML file.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="parent" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the referenced bean in a parent factory.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
It is a description of element bean element ref. And as we know, we can use <ref local> only for elements in current XML document.
Related
I'm getting the following error when generating sources for my project. I have extracted a few common types to a schema called CommonTypes.xsd and I get the following error:
org.xml.sax.SAXParseException: src-resolve.4.1: Error resolving component 'nonEmptyString'. It was detected that 'nonEmptyString' has no namespace, but components with no target namesp
ace are not referenceable from schema document 'file:/C:/Workspace/CommonTypes.xsd'. If 'nonEmptyString' is
intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'nonEmptyString' has no namespace, then an 'import' without a "namespace" attribute should
be added to 'file:/C:/Workspace/lps-performance-calculation-service/pcs-data/src/main/resources/xsd/calc/lps/CommonTypes.xsd'.
The following simple type is defined in my CommonTypes.xsd schema as below:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:unit="http://www.mywebsite.com/unit"
xmlns:types="http://www.mywebsite.com/types"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://www.mywebsite.com//types">
<!-- import types -->
<xsd:import namespace="http://www.mywebsite.com/unit"/>
<!-- other common types -->
<xsd:simpleType name="nonEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:pattern value=".*[^\s].*"/>
</xsd:restriction>
</xsd:simpleType>
And line 241 which causes the error is below:
<xsd:complexType name="Message">
<xsd:simpleContent>
<xsd:extension base="nonEmptyString">
<xsd:attribute type="xsd:string" name="code" use="required"/>
<xsd:attribute name="category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:token">
<xsd:enumeration value="Error"/>
<xsd:enumeration value="Info"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute type="xsd:string" name="component" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Do you have any idea what may cause the error? I've tried searching through StackOverflow and experimenting with the targetNamespace and xmlns, but with no success.
You are trying to refer to a simple type which has
name 'nonEmptyString'
namespace ""
But the simple type 'nonEmptyString' is defined in this XSD, which has targetNamespace="http://www.mywebsite.com//types". So you should be referring to a simple type which has
name 'nonEmptyString'
namespace "http://www.mywebsite.com//types"
You simply need to change this:
<xsd:extension base="nonEmptyString">
to this:
<xsd:extension base="types:nonEmptyString">
You need to import your nonEmptyString in the corresponding namespace and make this namespace referenceable via the prefix.
To do this, add xmlns:types="http://www.mywebsite.com/types" to xsd:schema of the importing schema.
Also provide the namespace in xsd:import of the importing schema. Should be something like:
<xsd:import
namespace="http://www.mywebsite.com/types"
schemaLocation="calc/lps/CommonTypes.xsd"/>
Then you should be able to reference your nonEmptyString type as types:nonEmptyString.
I'm using cxf-codegen-plugin and try to generate java from wsdl.
In my xsd schema I have following element
<xsd:element name="SomeElement">
<xsd:complexType>
<xsd:attribute name="version" type="xsd:string" use="optional">
</xsd:attribute>
<xsd:attribute name="Version" type="xsd:string" use="optional">
</xsd:attribute>
</xsd:complexType>
</xsd:element>
CXF tries to generate both attributes into field version and throws exception that property already exists.
Is there any way to generate properties as they described in XML? e.g.
protected String version;
protected String Version;
I know, that I can define binding and rename one of the properties, but I have ~100 wsdl, so I don't want write bindings for each file.
I also tried to add -autoNameResolution argument, but it didn't help.
I have an XML schema whose topmost element is Document
<xsd:element name="Document" type="Document"/>
It contains one element of type ZZ_Customer which is restriction of Customer.
Both of these elements contain children of the same name but with slightly different types.
<xsd:complexType name="Document">
<xsd:sequence>
<xsd:element name="CstmrCdtTrfInitn" type="ZZ_Customer"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ZZ_Customer">
<xsd:complexContent>
<xsd:restriction base="Customer">
<xsd:sequence>
<xsd:element name="GrpHdr" type="ZZ_Group"/>
<xsd:element name="PmtInf" type="ZZ_Payment" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Customer">
<xsd:sequence>
<xsd:element name="GrpHdr" type="Group"/>
<xsd:element name="PmtInf" type="Payment" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
When JAXB unmarshalls an XML file will it create an instance of ZZ_Customer or will it create an instance of Customer? Likewise when will it create an instance of Group or ZZ_Group?
I've noticed is that JAXB will instances of ZZ_* for certain parts of the XML but uses their base counter parts for other parts of the XML.
On what basis does it makes its decisions? It does not appear obvious which criteria that JAXB is using.
Unfortunately I have no control over the schema and its design.
Since Document has a property that corresponds to the sub-type.
The sub-type will be created when unmarshalling. This makes sense since in Java when a property is typed to the subclass you can't set an instance of the super class on it.
If the property had corresponded to the super type, by default JAXB would have unmarshalled it to an instance of the super type. The XML can contain the xsi:type attribute to specify that the subtype is being used.
<xsd:element name="loginResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="loginReturn" type="tns:test"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="test">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="tx" type="xsd:int"/>
<xsd:element minOccurs="0" maxOccurs="1" name="result" type="xsd:int"/>
<xsd:element minOccurs="0" maxOccurs="1" name="name_space" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
I just want to ask why is the type tns:test used? How can I get the tx, result, namespace values in complextype name="test", because that's the response should I get based on the api they given to me.
tns is the target namespace prefix, which should be defined at the top of your WSDL or XSD file (which includes test).
You didn't wrote how you do access the values, but I assume that your code is working in a different namespace, so that test cannot be indentified. Most likely there is a method which allows you to get values by element name and namespace. Note that in that case, the namespace isn't tns but rather the URL which is defined at top of source file.
If you're not familiar with namespaces: Each XML element is associated with a namespace, like a class in Java is part of a package. In XML there is no import statement, so you have to name an element by name and namespace. To keep the files readable you can define namespace prefixes (probably as an abbreviation).
After going through this page, I wrote the following code to parse an XSD file. However I only get the root element, and I am lost so as to how to get the nested elements inside it.
Code:
XMLSchemaLoader loader = new XMLSchemaLoader();
XSModel model = loader.loadURI(url.toURI().toString());
XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION); //returns the root component
if(map!=null ){
for (int j=0; j<map.getLength(); j++) {
String name = map.item(j).getName(); //returns 'country' correctly.
}
}
I am not posting the entire xsd, but this is the structure:
<xsd:element name="country">
<xsd:complexType>
<xsd:annotation>
<xsd:appinfo id="substring">No</xsd:appinfo>
</xsd:annotation>
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element name="states" minOccurs="1" maxOccurs="1" >
<xsd:complexType>
<xsd:annotation>
<xsd:appinfo id="substring">No</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="cities" minOccurs="1" maxOccurs="unbounded">
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string"></xsd:attribute>
</xsd:complexType>
</xsd:element>
I am looking to read all the elements, not just the base element, and not sure how to proceed. Thanks for the help.
I think your trying to iterate over the XSNamedMap incorrectly. The reason you are getting only the country element is because it is the root element. You will probably have to descend down into XSNamespaceItem and call getComponents to retrieve another set of XSNamedMap objects.
This will properly parse the XSD but you still have to traverse the tree.