Adding namespace to XML document without http: - java

I need to add the following namespaces to my XSD document:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="catalog.enterprise.level3.com" attributeFormDefault="unqualified">
When I try to add the target namespace, it complains of a MalformedURIException, which I assume is because there is no http:// in the namespace URI. I am using XOM for this.
Element rootWriter;
rootWriter.addNamespaceDeclaration("tns","catalog.enterprise.level3.com");
Could someone please suggest to me, how to solve this problem.

The namespace MUST be an URI - for example:
uri:catalog.enterprise.level3.com
would work. If, as I assume, you have to read a wrongly encoded xml, your only solution I am afraid is to pick a "lower quality" xml parser which does not perform that kind of check - or complain with the person who gave you the file!

Related

Partial XML parsing giving an element not bound exception

I'm trying to parse an XML document with DSpace XOAI library. This is the input XML:
<?xml version="1.0" encoding="UTF-8"?>
<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">
...
<ListRecords>
<record>
<header>
...
</header>
<metadata>
<crossref xmlns="http://www.crossref.org/xschema/1.1" xsi:schemaLocation="http://www.crossref.org/xschema/1.1 http://www.crossref.org/schema/unixref1.1.xsd">
...
</crossref>
</metadata>
</record>
</ListRecords>
</OAI-PMH>
From what I could deduce from debugging, each metadata node is parsed individually by XOAI library. And, in that context, I get this error (which makes sense, because the xsi namespace is defined in parent OAI-PMH node):
ERROR: 'The prefix "xsi" for attribute "xsi:schemaLocation" associated with an element type "crossref" is not bound.'
For what I could understand from the library source code, it uses the Oracle java javax.xml.transform.Transformer to make the transformations. We can set any Transformer to be executed.
I'm already using an XSLT file for transforming the input XML in the format expected by the library. However, I couldn't find a way to create a rule in XSLT to ignore the xsi:schemaLocation that is causing the error.
The other option is create a new Transformer in Java. I was looking at Transformer.setOutputProperty, but I couldn't make a working configuration that ignores this error in crossref node.
Do you guys know how can I correctly parse the contents of crossref node in that local context?
Thanks in advance!

How to force xmlspy code to write out qualified namespaces on elements?

I have used XmlSpy 2013 to generate program code in Java from a schema. My application basically reads in xml from a file, modifies the xml, and writes it back out to the file. The generated code provides classes and functions to do the load:
sampleSchema2 doc = sampleSchema2.loadFromFile(filePath);
// Load the file into Java objects...
and to write the file back out:
sampleSchema2 sampleDoc = sampleSchema2.createDocument();
// Populate the doc from the modified Java objects...
sampleDoc.saveToFile(path, true);
The schema I used to generate the code has the following attributes:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:bfrs="http://www.example.com/schema/bfrs" xmlns:cnc="http://www.example.com/schema/cnc" targetNamespace="http://www.example.com/schema/cnc" elementFormDefault="qualified" attributeFormDefault="unqualified" version="2006/05/30" xml:lang="en">
The xml files I read in use qualified namespaces for the elements like so:
<?xml version="1.0" encoding="UTF-8"?>
<cnc:cnc versionNumber="v.2.2.1" versionDate="2012-04-03" xsi:schemaLocation="http://www.example.com/schema/cnc exampleSchema.xsd" xmlns:cnc="http://www.example.com/schema/cnc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cnc:Revisions>
<cnc:Revision>S003</cnc:Revision>
</cnc:Revisions>
...
But after I write the file out again using saveToFile() as above, all of the qualified namespaces are removed from the elements like so:
<?xml version="1.0" encoding="UTF-8"?>
<cnc versionNumber="v.2.2.1" versionDate="2012-04-03" xmlns:cnc="http://www.example.com/schema/cnc">
<Revisions>
<Revision>S003</Revision>
</Revisions>
...
Does anyone know how I can get xmlspy to qualify the namespaces on the documents so they look like how I read them in? Thank you for any help.
As it turns out, this is currently impossible using Altova generated code per my response from Altova technical support:
Thanks for contacting us.
I'm afraid that it's currently not presently possible to control the
namespace prefix in the generated code.
I'll forward your message on to our development team for future
consideration.
Best regards,
Mxxxxxxxx Kxxxxx
Support Engineer
Altova GmbH

if XML file has "xmlns",how can I get Text from XML file using XPath

I get a XML file from website (http://www.abc.com/),
URL is: http://www.abc.com/api/api.xml
content is:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://www.abc.com/">
<name>Hello!</name>
</root>
it has xmlns="http://www.abc.com/" in XML file,
now, I using JDOM XPath to get text Hello!
XPath xpath = XPath.newInstance("/root/name/text()");
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new URL("http://www.abc.com/api/api.xml"));
System.out.println(xpath.valueOf(doc)); //nothing to print...
I test to remove xmlns="http://www.abc.com/" from XML file, it's be work!
how to change my java code to get Hello!, if xmlns="http://www.abc.com/" exist?
(I can't chagne this XML file)
thanks for help :)
You'll need to make the query aware of the xml namespace. This answer here looks like it will do the trick:
Default Xml Namespace JDOM and XPATH
You might also change your query to use local-name to ignore namespaces:
XPath xpath = XPath.newInstance("/*[local-name() = 'root']");
That should return the node named root. That is, if it supports it and I typed it correctly! :) I'm not familiar with java API's for XML + XPATH.
Be aware that xml namespaces exist to distinguish node 'root' from any other node named 'root'. Just like class/package namespaces. Ignoring them could lead to a name collision. Your milage may vary.
HTH,
Zach
I have not done this recently. But a quick search found
http://illegalargumentexception.blogspot.com/2009/05/java-using-xpath-with-namespaces-and.html
which point to the usage of a XPathFactory:
NamespaceContext context = new NamespaceContextMap("http://www.abc.com/" );
Or, you could use Zach's answer and just ignore the given namespace (if i understood him right). This could lead to problems if there are more 'root' nodes at the same hierarchy level..

Error when converting an XSD to its classes with JAXB

This is the first time I've ever tried to use JAXB for anything at all and I'm running into a problem.
I'm trying to use JAXB's XJC tool to convert an XSD file into Java class files when I get this error:
parsing a schema...
[ERROR] The prefix "msdata" for attribute "msdata:ColumnName" associated with an element type "xs:simpleContent" is not bound.
line 10 of file:/home/jeremy/TypeDefs.xsd
Failed to parse a schema.
Line 10 in the schema file reads:
<xs:simpleContent msdata:ColumnName="paramText" msdata:Ordinal="1">
and you can see the whole thing here.
edit: It turns out the XSD file was missing the namespace declaration for msdata. Simply adding xmlns:msdata="http://schemas.microsoft.com/2003/07/msdata.xsd" fixed the problem.
You need to have the msdata namespace prefix mapped. Like
<xs:schema targetNamespace="http://maps.trimet.org/maps/model/xml"
xmlns="http://maps.trimet.org/maps/model/xml"
....
xmlns:msdata="http://yournamespace.com/foo">

how to reference XSD Schema location while parsing XML Doc via SAX Xerces?

how to reference XSD Schema location while parsing XML via SAX Xerces?
< ?xml version="1.0" encoding="ISO-8859-1"?> < com.firma
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
< !-- xsi:noNamespaceSchemaLocation="F:\...\myschema_v2.5.xsd"
Must I reference this element really within the XML Doc??? I hope, not...
-- >
I also set it as follows in Java code, which is not elegant, while schema location is fixed(not appropriate for production)
SaxParser.setProperty(
"http://java.sun.com/xml/jaxp/properties/schemaSource",
"F:...\myschema_v2.5.xsd"
);
include the schema in your jar and load it using getResourceAsStream in the following way
reader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
new InputSource(getClass().getResourceAsStream(xsdLocation)));
I got it.
one must use as follows, giving "/com/firma/project/.../myschema_v2.5.xsd" as parameter.
not forgetting the "/" in the path at the very beginning.

Categories