JAXB not parsing default namespace correctly - java

I'm writing some code to parse GPX files. These are produced by a number of different sources, from route planning websites to satnav recordings.
I'm having trouble with JAXB. It seems that it won't load the file if it uses the default name space.
For example when I try to parse the following file only the root gpx element is loaded and the child meta is not loaded and java leaves the field blank.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<time>2015-01-24T18:13:23Z</time>
<bounds minlat="52.058110" minlon="-2.218540" maxlat="52.347110" maxlon="-1.943840" />
</metadata>
<!-- snip -->
</gpx>
However the following file loads just fine. The only difference being that the default namespace has been changed to a named one (ns1).
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ns1:gpx xmlns:ns1="http://www.topografix.com/GPX/1/1" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<time>2015-01-24T18:13:23Z</time>
<bounds minlat="52.058110" minlon="-2.218540" maxlat="52.347110" maxlon="-1.943840" />
</metadata>
<!-- snip -->
</ns1:gpx>
My package-info.java is currently defined with:
/**
* Pojos Representing the GPX xml.
*/
#XmlSchema(
elementFormDefault = XmlNsForm.UNQUALIFIED,
namespace = "http://www.topografix.com/GPX/1/1",
location = "http://www.topografix.com/GPX/1/1/gpx.xsd",
xmlns= {
#XmlNs(namespaceURI = "http://www.topografix.com/GPX/1/1", prefix = "gpx"),
}
)
package couling.gpxTools.pojo;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Is this something anyone else has seen before and are there any suggestions of how to get round it?

In your second XML all the elements should have the ns1 prefix to be equivalent to the first XML document.
To load the first document you need to specify elementFormDefault=XmlNsForm.QUALIFIED on the #XmlSchema annotation. Then all mapped elements within this package will default to having this namespace. Since you specified UNQUALIFIED only top level elements get the namespace qualification.

Related

Transformer removes the namespace incorrectly from xml

I have the following xml file which has namespace on NFe and enviNFe tags:
<?xml version="1.0" encoding="UTF-8"?>
<nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeAutorizacao4">
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<idLote>203364605034338</idLote>
<indSinc>0</indSinc>
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
...
...
But while send the request, exactly on WebServiceTemplate.sendSourceAndReceiveToResult, there is a Transformer that removes the namespace of the NFe tag, as follows:
<?xml version="1.0" encoding="UTF-8"?>
<nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeAutorizacao4">
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<idLote>203364605034338</idLote>
<indSinc>0</indSinc>
<NFe>
...
...
The Webservice doesn't accept the request n this case.
What should I do to avoid this behavior?

How to merge more than one XSD file to one XSD file?

I am not master in XML and XSD.
Just want to know how I can merge more than one XSD file to one XSD file?
Thanks in Advance.
You can use import (different namespace) and include (same namespace) multiple times. redefine can also be used multiple times. It depends on what you mean by "merge."
See also http://www.herongyang.com/XML-Schema/Multiple-XSD-Schema-Document-Include-Redefine-Import.html or http://msdn.microsoft.com/en-us/library/ee254473%28v=bts.10%29.aspx.
Edit: redefine can be used multiple times (similar to include).
Examples (validated in Eclipse) follow. I used different namespace (as the "merging" target namespace) and element names where necessary:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/m"
xmlns:tns="http://www.example.org/m" elementFormDefault="qualified">
<!-- import: different (i.e. not target) namespace -->
<import namespace="http://www.example.org/a" schemaLocation="so20046640a.xsd"/>
<import namespace="http://www.example.org/b" schemaLocation="so20046640b.xsd"/>
<!-- include: same namespace -->
<include schemaLocation="so20046640c.xsd"/>
<include schemaLocation="so20046640d.xsd"/>
<!-- redefine: same namespace -->
<redefine schemaLocation="so20046640e.xsd"/>
<redefine schemaLocation="so20046640f.xsd"/>
</schema>
...a.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/a"
xmlns:tns="http://www.example.org/a" elementFormDefault="qualified">
<element name="a" type="int"/>
</schema>
...b.xsd: Same as ...a.xsd but target namespace .../b
...c.xsd: Same as ...a.xsd but target namespace .../m
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/m"
xmlns:tns="http://www.example.org/m" elementFormDefault="qualified">
<element name="a" type="int"/>
</schema>
...d.xsd: Same as ...c.xsd but element name b.
...e.xsd: Same as ...c.xsd but element name e.
...f.xsd: Same as ...c.xsd but element name f.

Adding Custom META tag to XML file

I created a java application that generates a whole bunch of simple XML files using the DocumentBuilder class. But now I need to go back and modify it so that all of the XML files also include this meta tag inserted before the root 'resultset' element:
<meta name="ZOOMPAGEBOOST" content="5">
So that the resulting files will look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="..\..\..\stylesheet\mysql-result.xsl"?>
<meta name="ZOOMPAGEBOOST" content="5">
<resultset>
<row>
...
</row>
</resultset>
Is this possible using the DocumentBuilder class? I figured this would have been a simple change, but I am having a difficult time finding the solution.
What you want to build is not well formed XML, DocumentBuilder won't let you build it because of that reason. Not to mention that the meta element you use in your example isn't valid XML either because it isn't closed; you have multiple problems here.
You have to have a single root element, the following is valid XML.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="..\..\..\stylesheet\mysql-result.xsl"?>
<resultset>
<meta name="ZOOMPAGEBOOST" content="5"/>
<row>
...
</row>
</resultset>

How to use DOCTYPE in Spring XML file

Most of the time we do not declare DOCTYPE in Spring.
But I want to declare a DOCTYPE in my XML context file so that I can use ENTITY in my xml file.
For example:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"
[<!ENTITY % crmHome SYSTEM "crm-home.dtd"> %crmHome;]
>
This gives many errors like...
- Attribute "xmlns" must be declared for element type "beans".
- Attribute "xmlns:xsi" must be declared for element type "beans".
etc.....
What is the way to achieve this?
This works for me. Using Spring Framework V.4.2.1
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
If you're using schema validation anyway then you could just define the internal DTD subset sufficient to declare the parameter entity and not refer to the http://www.springframework.org/dtd/spring-beans-2.0.dtd:
<!DOCTYPE beans [
<!ENTITY % crmHome SYSTEM "crm-home.dtd">
%crmHome;
]>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

extract inline schema WSDL stylesheet xsom

I want to extract inline schema from a WSDL file. But, I don't know how to perform XSL transformation for that. Any help on creating such a stylesheet would be great.
thanks a lot,
This one is working for me:
<?xml-stylesheet type="text/xsl"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.w3.org/2001/XMLSchema" >
<xsl:output method="xml" />
<xsl:template match='text()' />
<xsl:template match="//s:schema">
<xsl:copy-of select='.'/>
</xsl:template>
</xsl:stylesheet>
It assumes your inline schema uses the http://www.w3.org/2001/XMLSchema namespace.
This is what you can do when the wsdl contains more than one schema-elements:
Use xsd-split.xslt from
https://gist.github.com/ebratb/3819949
to to split the *.wsdl file into several *.xsd files.
You can use the maven plugin to run the 2.0 xslt with Saxon ( see http://www.mojohaus.org/xml-maven-plugin/examples/transform-saxon.html )
Then write a small schema file that just imports those generated *.xsd files plus the official definition of the soap envelope.
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://your.company.com/dummy" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://<target namespace of the first xsd file>" schemaLocation="file:///path/to/first.xsd" />
<xsd:import namespace="http://<target namespace of the second xsd file>" schemaLocation="file:///path/to/second.xsd" />
...
<xsd:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/" />
</xsd:schema>
When using a custom Resource Resolver, the schemaLocation attributes are not needed.

Categories

Resources