This question already has answers here:
xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?
(2 answers)
How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?
(1 answer)
Closed 5 years ago.
I'm new to xsd and learning about namespaces. Seen maximum all questions, blog etc but no one could help me understand in simple way.
What I have understood so far is that, Namespaces are used to differentiate elements having same names. (Not cleared about targetnamespace)
My understanding to make namespace is below
xlmns:foo()="URI" -->Namespace (Its a unique token I would say which is responsible to differentiate elements with this syntax. And this synatx given a name and the criteria of making that name is Prefix:ElementName-->Prefix.
I have got one example
<foo:tag xmlns:foo="http://me.com/namespaces/foofoo"
xmlns:bar="http://me.com/namespaces/foobar"
>
<foo:head>
<foo:title>An example document</foo:title>
</foo:head>
<bar:body>
<bar:e1>a simple document</bar:e1>
<bar:e2>
Another element
</bar:e2>
</bar:body>
</foo:tag>
If we want to use multiple namespaces in a xsd then we can declare them once like in above example. Where same prefix is being used for multiple namespaces
foo:tag --->xmlns:foo="http://me.com/namespaces/foofoo"
foo:tag --->xmlns:bar="http://me.com/namespaces/foobar
Is it same like in java where in a package we can have multiple classes and each class has its own attributes, in case of xml its elements. Am I correct ? Can anyone help me to understand TargetNamespace ?
The targetNamespace is the namespace that is going to be assigned to the schema you are creating or the namespace that this schema is intended to target, or validate. It is the namespace an instance is going to use to access the types it declares.
For example :
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.bestxml.com/jswbook/po">
...
</schema>
In an XML document instance, you declare the namespaces you are going to be using by means of the xmlns attribute
<purchaseOrder xmlns="http://www.bestxml.com/jswbook/po"
xmlns:addr="http://www.bestxml.com/jwsbook/addr">
<accountName>Shanita</accountName>
<accountNumber>123456</accountNumber>
<addr:street>20 King St</addr:street>
</purchaseOrder>
Related
I have an XSD with an element - it is the XMLRootElement if that makes a difference - like this:
<xsd:element name= "SomeElement">
I need to have the generated Java class have a custom XML element name while keeping the default Java class name, so the generated class needs to look like this:
#XmlRootElement(name = "fo:SomeElement")
public class SomeElement
So that marshal/unmarshalled xml elements will show as
<fo:SomeElement>
Can someone help me out with what I need to change to either the XSD file or the binding file?
First of all, with your question you opened a big can of worms.
Things are more complicated than you thought they would be.
To fully understand the rest of this answer
you will surely need to learn more about the namespace concept in XML,
for example at w3schools.com - XML Namespaces.
Having said that, the following stuff should give a quick entry into that topic.
Note that fo:SomeElement is not directly an XML element name.
The fo: is a so-called namespace-prefix.
The namespace-prefix needs to be mapped to a namespace-URI by xmlns:fo="...",
By convention fo: is the namespace-prefix used for XSL Formatting Objects.
Therefore most probably your XML file will look like this:
<fo:SomeElement xmlns:fo="http://www.w3.org/1999/XSL/Format" ...>
...
</fo:SomeElement>
Note that "http://www.w3.org/1999/XSL/Format" is the namespace-URI
as specified in the XSL Formatting Objects specification.
Note also, that namespace-prefixes (here fo) by themselves are irrelevant
and were only invented to make the XML content easier to read for humans.
So instead of fo you might as well have used bla in all places as the namespace-prefix,
and the XML content still would have the exact same meaning.
The only relevant things are the namespace-URIs (here "http://www.w3.org/1999/XSL/Format").
With JAXB the correct Java root class would look like this.
Note the namespace given in the #XmlRootElement annotation.
#XmlRootElement(name="SomeElement", namespace="http://www.w3.org/1999/XSL/Format")
public class SomeElement {
...
}
You would need to specify this namespace-URI not only in #XmlRootElement,
but also for nested Java properties corresponding to any <fo:something> XML content.
Fur this purpose most JAXB annotations (#XmlElement, #XmlAttribute, ...)
can accept a namespace parameter as well.
The XML schema definition (XSD) consistent with the XML example and
the Java class above would look like this.
Note the targetNamespace given in the <xs:schema> element.
<xs:schema version="1.0" targetNamespace="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="SomeElement">
...
</xs:element>
</xs:schema>
I am trying to find good and most generic solution for soap web services' problem. The problem I need to solve is to replace proxies which Apache Axis generate using given WSDL file with the proxies that Apache CXF genereta using wsdl2java command.
However, when I generate proxies with CXF and opened the classes in IDE I realised that I have:
#XmlAnyElement(lax = true)
protected List<Object> any;
element as a Class field. This was strange. I looked at the pure wsdl and in one of the complextype properties there is xsd:any element. I have been searching for a long time what is the problem and for the best solution to solve it. I found this . It seems that apache CXF runtime cannot determine the actual data type of the element. So I need to parse it manually with DOM parser.
Apache Axis generate the following:
private org.apache.axis.message.MessageElement[] _any;
The tag:
<any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
My question is: What is the best way to solve this problem? Do I really need DOM parser to ? Is there something that i miss ?
Thank you.
<xsd:any> is used to allow in the schema any element. See this link
The element enables us to extend the XML document with elements not specified by the schema.
So mapping of CXF is correct. The JAXB annotation #XmlAnyElement(lax = true) means that for that field if contains an element annotated with #XmlRootElement or #XmlElementDecl then an instance of the corresponding class will be used to populate the field if not the element will be set as an instance of org.w3c.dom.Element.
See an example here
I am trying to write a Java application that consumes a given web service, using JAX-WS and wsimport. The SOAP message that it sends to the service is mostly correct. However, on of the parameters passed to the service function is an array of strings. Although the array itself is given the proper namespace in the SOAP XML, elements ('parm' in the message below) have no namespace. This causes the service to fail.
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:Submit xmlns:ns2="http://www.acme.com/service/wsdl">
<ns2:service>SomeJob</ns2:service>
<ns2:parms>
<parm>someparam</parm>
<parm>anotherparam</parm>
</ns2:parms>
</ns2:Submit>
</S:Body>
</S:Envelope>
The <parm> elements should have read <ns2:parm>, or alternatively the <Submit> tag could have defined a default namespace <Submit xmlns="http://www.acme.com/service/wsdl"> (that is what the service suggests in an example SOAP message).
This question does resemble the one in JAX-WS: why nested elements are in "" namespace?. However there changing the style from RPC/Literal to Document/Wrapped solved the issue, while in my case the service is Document/Wrapped to begin with.
How can I convince the JAX-WS library to generate namespaces on the nested elements in the array?
TIA,
Jeroen
Update: Editing the generated code appears to work. Someone suggested adding a namespace attribute to the #XmlElement line preceeding the definition of 'parm' in the generated array type:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "ArrayOfString", propOrder = {
"parm"
})
public class ArrayOfString {
#XmlElement(nillable = true, namespace="http://www.acme.com/service/wsdl")
protected List<String> parm;
...
}
This causes JAX-WS to add the namespace and will probably solve the issue. As code generation is, in this case, done only once, editing the generated code is acceptable. Still I wonder whether there isn't a better solution.
You can also influence the namespace creation by adding
elementFormDefault="qualified"
either to your Java #XmlSchema annotation - or in your case - to the XSD defining the service.
I'm trying to create an XML document in Java that contains the following Element:
<project xmlns="http://www.imsglobal.org/xsd/ims_qtiasiv1p2"
xmlns:acme="http://www.acme.com/schemas"
color="blue">
I know how to create the project Node. I also know how to set the color attribute using
element.setAttribute("color",
"blue")
Do I set the xmlns and xmlns:acme attributes the same way using setAttribute() or do I do it in some special way since they are namespace attributes?
I believe that you have to use:
element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:acme", "http://www.acme.com/schemas");
I do not think below code will serve the question!
myDocument.createElementNS("http://www.imsglobal.org/xsd/ims_qtiasiv1p2","project");
This will create an element as below (using DOM)
<http://www.imsglobal.org/xsd/ims_qtiasiv1p2:project>
So this will not add an namespace attribute to an element. So using DOM we can do something like
Element request = doc.createElement("project");
Attr attr = doc.createAttribute("xmlns");
attr.setValue("http://www.imsglobal.org/xsd/ims_qtiasiv1p2");
request.setAttributeNode(attr);
So it will set the first attribute like below, you can set multiple attributes in the same way
<project xmlns="http://www.imsglobal.org/xsd/ims_qtiasiv1p2>
The short answer is: you do not create xmlns attributes yourself. The Java XML class library automatically creates those. By default, it will auto-create namespace mappings and will choose prefixes based on some internal algorithm.
If you don't like the default prefixes assigned by the Java XML serializer, you can control them by creating your own namespace resolver, as explained in this article:
https://www.intertech.com/Blog/jaxb-tutorial-customized-namespace-prefixes-example-using-namespaceprefixmapper/
You can simply specify the namespace when you create the elements. For example:
myDocument.createElementNS("http://www.imsglobal.org/xsd/ims_qtiasiv1p2","project");
Then the java DOM libraries will handle your namespace declarations for you.
The only way that worked for me, in 2019, was using the attr() method:
Element element = doc.createElement("project");
element.attr("xmlns","http://www.imsglobal.org/xsd/ims_qtiasiv1p2");
I'm using Enunciate to generate a SOAP endpoint for a Wicket web application I am working on and I have a couple of questions that I haven't figured out the solution to yet.
1 How do I change the name of the xsd files? I've looked through the FAQ and it tells me to do something similar to this:
<xml>
<schema namespace="http://api.example.com/data" file="data.xsd"/>
</xml>
However, I haven't quite figured out how to set the targetNamespace for my data objects. I've done this for my service via #WebService ( targetNamespace="blah" ), but how do I annotate my data objects to let Enunciate know which namespace they should belong to?
2 Enunciate generates my XSDs just fine, but I don't particularily like the element names it uses. I have a ServiceRequest and ServiceResponse object. The ServiceRequest object has a List of User objects. The ServiceResponse has a list of Group objects. Enunciate suggests that every "User" object within the ServiceRequest should be using the tag "<users>". I feel that it would make more sense to use the singular form, "<user>" since the tag does in fact only contain a single user. Is it possible to change this behaviour, and if so, how?
Thanks in advance.
So just to be clear, with the exception of the question about naming your schema files, your questions are really more about JAXB than they are about Enunciate. JAXB is the spec that defines how your Java objects are (de)serialized to/from XML and Enunciate conforms to that spec.
Anyway, the easiest way to apply a namespace to your Java objects is with a package-info.java file in the package of your Java classes. Annotate your package with #XmlSchema and set the namespace to be the value you want.
Customizing how your accessors are serialized to/from XML can be done with the #XmlElement annotation, e.g.:
public class MyClass {
...
#XmlElement (name="user")
List<User> users;
...
}
Here are the JAXB javadocs
https://jaxb.dev.java.net/nonav/2.1.9/docs/api/
Or google for a good JAXB tutorial.