Schema Root
<xs:schema jxb:version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/XMLSchema
http://www.nubean.com/schemas/schema.xsd" >
<xs:element name="UsOrCanadaAddress" >
JAXB Binding XML
<?xml version='1.0' encoding='utf-8' ?>
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<jxb:bindings node="/xs:schema" schemaLocation="address.xsd" >
<jxb:schemaBindings>
<jxb:package name="com.apress.jaxb1.example" ></jxb:package>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
I am beginning with JAXB and these are the two tags I came across in the books.
I have a few basic questions regarding the various parts of the two tags. Here they go:
Question 1:
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
Does this attribute has to have the exact same value ?
Question 2:
xsi:schemaLocation="http://www.w3.org/2001/XMLSchema
http://www.nubean.com/schemas/schema.xsd"
This attribute, in the schema.. in the schema ??? I mean, I can understand that attribute in an XML document pointing to an XML schema but this ? What does it do if not trigger a schema-ception ?
Also, the namespace-location pair. In an XML document it would point to a physical location. Here, does it have to point to a physical location ?
Question 3:
The word binding. In my head I understand that as settings that you can change in mobile or computer apps. They have default values which you can change. In the above binding document, they have changed the package setting. Now, assuming that I do not want to keep the document in no package, I should leave that as it is ?
I will not need to write that binding XML document ?
Question 4:
In the JAXB binding document schemaLocation="address.xsd" points to the schema location. Now that is the physical location. What if my schema was packed with a JAR file ?
Question 1 - Does this attribute has to have the exact same value ?
A JAXB (JSR-222) implementation expects the elements the elements in the binding file to be qualified with the "http://java.sun.com/xml/ns/jaxb" namespace. It does not depend on a particular prefix being used.
Question 2 - This attribute, in the schema.. in the schema ???
Since the XML schema is an XML document I guess doing this is ok, but I have never done this in an XML schema myself.
Question 3 - The word binding.
I have a kind of love/hate relationship with the word "binding". It has come to be assocated with converting objects to/from data formats that aren't necessarily persistent (i.e. XML, JSON, etc).
Question 4 - In the JAXB binding document schemaLocation="address.xsd"
points to the schema location.
I do not believe that the schemaLocation is required in the bindings file.
Since you are just getting started with JAXB you may not want to get hung up on the binding document. It's only needed when you need to customize the classes generated from an XML schema. Below is an example where it is not needed:
http://blog.bdoughan.com/2010/09/processing-atom-feeds-with-jaxb.html
What I find is the more interesting use case is staring from objects. Below is an example you may find useful:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
Related
I have the following XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:x="MY_NAMESPACE"
targetNamespace="MY_NAMESPACE">
<xs:element name="response" type="x:responseType"/>
<xs:complexType name="responseType">
<xs:all>
<xs:element name="param" type="x:responseParam"/>
</xs:all>
</xs:complexType>
<xs:complexType name="responseParam">
<xs:all>
<xs:element name="value" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:schema>
I use it to generate JAXB classes for unmarshalling a payload like the following:
<x:response xmlns:x="MY_NAMESPACE">
<param>
<value>OK</value>
</param>
</x:response>
via getWebServiceTemplate().marshalSendAndReceive in Spring. Problem is, I also want to unmarshal payloads without the namespace prefix, like this:
<response xmlns="MY_NAMESPACE">
<param>
<value>OK</value>
</param>
</response>
In this case, the response tag is parsed correctly, but the object reference representing param is always null. How can I edit my XSD to make things work? I already tried setting elementFormDefault="qualified" in the schema, or even form="qualified" on the param element.
Additional info that comes to mind (I might edit and add more depending on comments):
The unmarshaller is a Jaxb2Marshaller.
The XML documents
I think you probably know this, but removing that namespace prefix affects the entire document (because the 'param' and 'value' tags do not have any prefix and therefore inherit the default namespace binding). Becauses of this, in the first document the root tag 'response' is in namespace 'MY_NAMESPACE' and the other tags do not have any namespace. In the second document, all of the tags are in namespace 'MY_NAMESPACE'.
The XML Schema
The elementFormDefault attribute defaults to 'unqualified' so your schema should match the first document and reject the second. Your experiments confirm this.
If you set elementFormDefault to 'qualified' then it will reject the first document and match the second one.
There is no value of elementFormDefault that will make the XSD match both XML documents. The namespace is an integral part of the identity of the element.
Possible solution
If you are determined to construct an XSD that matches both documents then it could be done as follows:
explicitly set elementFormDefault to 'unqualified' (optional, but you're about to rely on that setting)b
wrap the current (globally-declared) contents of responseType in a choice group
add a second branch in the choice group containing a local declaration of element 'param' and all of its descendants. Because those are locally declared, they will be in noTargetNamespace.
This is not a general solution to the problem of making JAXB ignore namespaces, and I don't think you will find one (although I'm happy to be corrected by somebody who knows more than I do about JAXB).
Having said all of the above...I think you are probably solving the wrong problem. The JAXB standard is based on XML Schema. An XSD is not meant to tolerate the wrong namespaces. The second XML document is therefore invalid, and should be corrected by whoever is generating it.
I am using EXIficient to convert XML data to EXI and back to XML. Here, i use their EXIficientDemo class. Sample Code:
EXIficientDemo sample = new EXIficientDemo();
sample.parseAndProofFileLocations("FilePath");
sample.codeSchemaLess();
Firstly it converted xml file to EXI then back to XML, when it generate XML from previously generated EXI's file, it loses some information about Namespace.
Actual XML File:
<?xml version="1.0" encoding="utf-8"?>
<tt xml:lang="ja" xmlns="http://www.w3.org/ns/ttml"
xmlns:tts="http://www.w3.org/ns/ttml#styling"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<body>
<div>
<p xml:id="s1">
<span tts:origin="somethings">somethings</span>
</p>
</div>
</body>
Generated XML File By EXIficient
<?xml version="1.0" encoding="UTF-8"?>
<ns3:tt xmlns:ns3="http://www.w3.org/ns/ttml"
xml:lang="ja"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns3:body><ns3:div>
<ns3:p xml:id="s1">
<ns3:span xmlns:ns4="http://www.w3.org/ns/ttml#styling"
ns4:origin="somethings">somethings</ns3:span>
</ns3:p>
</ns3:div></ns3:body>
In the generated XML file, it is missing xmlns:tts="http://www.w3.org/ns/ttml#styling"
How to fixed this problem? If you can, please help me.
EXIficient may be suppressing unused namespaces. Your example doesn't show any use of the ttm namespace.
As you can see, it didn't retain the namespace prefix for the ttml namespace either (changed to ns3). The generated XML is perfectly valid if the ttml#metadata namespace is unused.
Update
With the updated question, where namespace ttml#styling is used by the origin attribute of the span element, the namespace is retained in the rebuilt XML, but it has been moved to the span element.
This is still a very valid XML document.
Namespace declarations (xmlns) can appear anywhere in a XML document, and applies to the element on which it appears, and all subelements (unless overridden, which is very unusual).
The same namespace can be declared many times on different elements. For simplicity and/or optimization, it is common to declare all namespaces up front, on the root element, using different prefixes, but it is not required to do so.
I read this question by accident and rather late unfortunately.
Just in case people are still struggling with this and are wondering what they can do.
As it was pointed out EXIficient behaves just fine with regards to namespace handling.
Having said that, the EXI specification allows one to preserve prefixes and namespaces (see Preserve Options).
In EXIficient one can set these options accordingly,
e.g.,
EXIFactory.getFidelityOptions().setFidelity(FidelityOptions.FEATURE_PREFIX, true);
I want to get the text value of a certain element in xml. In the XML below i want to get the value of SUBCHILD when the value of CODE is 'Code1' irrespective of the position of the MP entity when there could be many MP elements. Also i want to be able to do this using JAXB and MOXy with the #XMLPath attribute
The xml i have is this:
<RQ>
<PQ>
<MP>
<INFO>
<CODE>Code1</CODE>
</INFO>
<CHILD>
<SUBCHILD>VALUE for Code1</SUBCHILD>
</CHILD>
</MP>
<MP>
<INFO>
<CODE>Code2</CODE>
</INFO>
<CHILD>
<SUBCHILD>VALUE for Code2</SUBCHILD>
</CHILD>
</MP>
</PQ>
</RQ>
I want 'VALUE for Code1' irrespecive of its position, the MP element containing Code1 could be anywhere. The XPath i would use for this would be:
RQ/PQ/MP[INFO/CODE='Code1']/CHILD/SUBCHILD
but i cant seem to get the value i want from MOXy, is this functionality not supported, I know that you can map based on attributes, but i need it depending on the value of another element
Any help would be appreciated
MOXy currently does not support XPaths of the following form on its #XmlPath annotation.
There is an open bug to have an exception thrown if the XPath specified is not supported.
https://bugs.eclipse.org/397101
Can you open an enhancement request for the behaviour you are looking for?
This is xsd file
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/onetext"
xmlns:tns="http://www.example.org/onetext"
elementFormDefault="qualified">
<element name="edge">
<complexType>
<attribute name="x" type="float"></attribute>
<attribute name="y" type="float"></attribute>
<attribute name="height" type="float"></attribute>
<attribute name="width" type="float"></attribute>
<attribute name="xhref" type="string"></attribute>
<attribute name="id" type="string"></attribute>
<attribute name="isLocked" type="string"></attribute>
<attribute name="rx" type="double"></attribute>
<attribute name="ry" type="float"></attribute>
<attribute name="rotation" type="float"></attribute>
</complexType>
</element>
</schema>
This is xml file ,
<?xml version="1.0" encoding="UTF-8"?>
<edge xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/onetext"
xmlns:web="http://www.example.org/onetext"
xsi:schemaLocation="web_one/WebContent onetext.xsd"
id="WebApp_ID" version="3.0">
<image x="336"
y="52"
height="57.6"
width="57.6"
xhref="/dccp_repository/dam/logo/images/accc_logo.jpg"
id="Image_3"
isLocked="false"
rx="336"
ry="52"
rotation="0" />
</edge>
I faced this kind of problem.
No grammar constraints (DTD or XML schema) detected for the document.
in xml document.
If I changed root node of xml file like given below
<edge xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="web_one/WebContent/onetext.xsd"
xsi:schemaLocation="web_one/WebContent
web_one/WebContent/onetext.xsd"
id="WebApp_ID"
version="3.0">
then I faced this kind of error
cvc-elt.1: Cannot find the declaration of element 'edge'.
I confused with this concept. Please help.
The schema document you show declares an element named edge in the namespace http://www.example.org/onetext (the value of the targetNamespace attribute on the schema element). So far, so good.
In your first XML document instance, the outermost element is named edge, and it is in the namespace http://www.example.org/onetext. That's consistent with the declaration in the schema document. So far, so good. If you invoke an XSD validator telling it to use your schema document to find declarations for elements in the http://www.example.com/onetext namespace, all should be well and the validator should be able to validate your document. When it does, it will tell you that edge elements are declared as having no children, so the edge element in the document instance is not valid, because it has a child named image (in the namespace http://www.example.org/onetext). The validator may also mention that the schema has no declaration for any element named image in that namespace. When you get these error messages, your current problem will be solved and you can get on to the next problem of making your schema and your XML agree on the data format you are defining and using.
But at the moment, that is not happening. Why not? In your first XML instance, your outermost element has xsi:schemaLocation="web_one/WebContent onetext.xsd". Translated into English, this means "Hello, schema validator. If you are looking for schema declarations for elements in the namespace web_one/WebContent, you will find a schema document for that namespace at the location onetext.xsd. Have a nice day!" (Well, the spec doesn't actually say anything about having a nice day; that's just my interpretation.) There are a few things wrong with this:
The string web_one/WebContent is not an absolute URI, so it is not appropriate as a namespace name.
The elements in the document instance both are in namespace http://www.example.org/onetext, and your xsi:schemaLocation attribute tells the schema validator nothing about where to find declarations for that namespace.
If the validator actually reads the schema document onetext.xsd, and if that is the schema document you are showing us, then the validator ought to complain that the namespace name web_one/WebContent does not match the target namespace of the schema document.
The error message you're getting (No grammar constraints ... detected for the document) reflects point 2: your validator is not finding any declarations for the only namespace relevant to your instance document.
The way to correct this is to replace your current xsi:schemaLocation attribute value specification with something like xsi:schemaLocation = "http://www.example.org/onetext onetext.xsd" (assuming that the document onetext.xsd is the schema document you show us, and that it's in the same directory as the document instance being validated -- otherwise, use a correctly formulated relative URI).
Your second XML instance document contains an edge document in namespace http://www.w3.org/2001/XMLSchema. The validator doesn't need a pointer to a schema document for this namespace: it's the XSD namespace, and XSD validators typically have the schema for that namespace hard-coded into them. So its error message is not about not finding any schema; instead, the error message is about not finding a declaration for any element named edge in the XSD namespace.
The change made in the second XML instance can seem plausible only to someone who does not really understand XML namespaces or XML namespace declarations. So it allows me to suggest that what you really need to do is spend an hour or two learning how namespaces work in XML and how a parser understands from your document what (namespace-name, local-name) pair to associate with each element and attribute. Once you do that, many things relating to XSD schemas (in particular, many error messages) will be somewhat less mysterious.
Good luck!
This is how XML look
<element>
<complextype name="edge">
<height>some words here</height>
<width>some words here</width>
</complextype>
</element>
Tips: the <element> is the root and does not need a. name.
<complextype> is the one u can add name to which is the Child.
The <height> does not need much styling except you want to use DTD for your styling
I'm having big problems with Xpath evaluation using Jaxen.
Here's part of XML i'm evaluating on:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2011-05-31T13:04:08+00:00</responseDate>
<request metadataPrefix="oai_dc" verb="ListRecords">http://citeseerx.ist.psu.edu/oai2</request>
<ListRecords>
<record>
<header>
<identifier>oai:CiteSeerXPSU:10.1.1.1.1484</identifier>
<datestamp>2009-05-24</datestamp>
</header>
<metadata>
<oai_dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title>Winner-Take-All..</dc:title>
<dc:relation>10.1.1.134.6077</dc:relation>
<dc:relation>10.1.1.65.2144</dc:relation>
<dc:relation>10.1.1.54.7277</dc:relation>
<dc:relation>10.1.1.48.5282</dc:relation>
</oai_dc:dc>
</metadata>
</record>
<resumptionToken>10.1.1.1.2041-1547151-500-oai_dc</resumptionToken>
</ListRecords>
</OAI-PMH>
I'm using Jaxen because in my use case it's much faster then Apache implementation. I'm using W3C DOM for XML representation.
I need to select all record arguments, and then on selected nodes evaluate other xpaths (it's needed because of my processing architecture).
I'm selecting all record nodes (this works):
/OAI-PMH/ListRecords/record
Then on every selected record node I'm evaluating other xpaths to get needed data:
Select identifier text value (this works):
header/identifier/text()
Select title text value (this does NOT work):
metadata/oai_dc:dc/dc:title/text()
I've registered namespaces prefixes with their URIs (oai_dc and dc). I also tried other xpaths but none of them work:
metadata/dc/title/text()
metadata//dc:title/text()
I've read other stackoverflow questions about xpaths, namespaces and solution to add prefix "oai" with URI "http://www.openarchives.org/OAI/2.0/". I tried adding that "oai:" prefix to nodes without defined prefix but as result I even didn't select record nodes. Any ideas what I'm doing wrong?
Solution:
Problem was about parser (thanks jasso). It wasn't set to be namespace aware - after changing that setting everything works fine, as expected.
I can't see how the XPath expression /OAI-PMH/ListRecords/record can possibly select anything, since your document does not have a {}OAI-PMH element, only a {http://www.openarchives.org/OAI/2.0/}OAI-PMH element. See http://jaxen.codehaus.org/faq.html