Default namespaces in nested elements - java

I'm just curious about default namespaces. I have the follow xml:
<root xmlns="myNamespace">
<someElement xmlns="anotherNamespace">
<data>Info</data>
</someElement>
<anotherElement>
<moreData>Info2</moreData>
</anotherElement>
</root>
My guess is that myNamespace is inherit for <root> and <anotherElement>. And anotherNamespace is a default namespace in <someElement> so in this element and for its child's overrides the other myNamespace.
I know that I can rewrite the above xml code like:
<my:root xmlns:my="myNamespace">
<a:someElement xmlns:a="anotherNamespace">
<a:data>Info</a:data>
</a:someElement>
<my:anotherElement>
<my:moreData>Info2</my:moreData>
</my:anotherElement>
</my:root>
I think that both are totally valid but I have some problems with some xml beans implementation in java which doesn't accept the first one, so I'm curious if there is an xml specification where specifies if first aproach is or not correct.

The first approach is valid. In xml-names specification in section 6.2 Namespace Defaulting it explains:
The scope of a default namespace declaration extends from the
beginning of the start-tag in which it appears to the end of the
corresponding end-tag, excluding the scope of any inner default
namespace declarations.
And also includes this example:
<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<title>Cheaper by the Dozen</title>
<isbn:number>1568491379</isbn:number>
<notes>
<!-- make HTML the default namespace for some commentary -->
<p xmlns='http://www.w3.org/1999/xhtml'>
This is a <i>funny</i> book!
</p>
</notes>
</book>

Related

Java signing XML File - prevent standard XML signature namespace inside signature

I'm signing an XML File where I'm using the Java XML Digital Signature API, available from Java 6 up to now.
Web-Source: http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html
The Signature looks like following:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">[...]</Signature>
Now I want to know, is there any way, to sign the XML File, to prevent the API to determine this xmlns="http://www.w3.org/2000/09/xmldsig#"inside my tag, so that I just have following:
<Signature>...</Signature>
I'm very thankful for any clues.
Thank you #Vadim for your answer. Lets give more details for my problem. I got a XML Structure like:
<?xml version="1.0" encoding="UTF-8" ?>
<tests xmlns="schema1">
<test>
</test>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
</Signature>
</tests>
How can I get this working? Because in a third party system I Need to check it against a Schema, where I define the structure of the signature by myself, so there should be like two xmlns inside my
Per XML standard you have to have namespace defined for <Signature> element, so it can be as you have it or outside on parent element with prefix. As
<rootElemnt xmlns:sig="http://....">
<sig:Signature>....
But, why it bother you? Without it <Signature> tag belongs to default namespace of parent element and not to proper Signature namespace.
UPDATED If you have two namespaces you have to have two xmlns declarations. One can be default second must have prefix. or both must have prefixes.
If your custom elements are in xmlns="schema1", I think you need to look either how to make <sig:Signature xmlns:sig="http://www.w3.org/2000/09/xmldsig#"> or
<sch1:tests xmlns:sch1="schema1">
<sch1:test>
</sch1:test>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
<sch1:customElement>...</sch1:customElement>
</Signature>
</sch1:tests>
It depends on how you build full XML. Sorry, I'do not know how to do that in Java XML Digital Signature API (never used it directly, just through WSDL policy), but all other tools have an ability to handle namespace prefixes
also it can look like:
<tests xmlns="schema1">
<test>
</test>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
<sch1:customElement xmlns:sch1="schema1">...</sch1:customElement>
</Signature>
</tests>
Perhaps it should look like that by defualt, but if not I guess if you marshal customElement into XML separately and then add it into Signature it must be like that.

Parse XML in which tag name is not fixed

It is easy to parse XML in which tags name are fixed. In XStream, we can simply use #XStreamAlias("tagname") annotation. But how to parse XML in which tag name is not fixed. Suppose I have following XML :
<result>
<result1>
<fixed1> ... </fixed1>
<fixed2> ... </fixed2>
</result1>
<result2>
<item>
<America>
<name> America </name>
<language> English </language>
</America>
</item>
<item>
<Spain>
<name> Spain </name>
<language> Spanish </language>
</Spain>
</item>
</result2>
</result>
Tag names America and Spain are not fixed and sometimes I may get other tag names like Germany, India, etc.
How to define pojo for tag result2 in such case? Is there a way to tell XStream to accept anything as alias name if tag name is not known before-hand?
if it is ok for you to get the tag from inside the tag itself (field 'name'), using Xpath, you can do:
//result2/*/name/text()
another option could be to use the whole element, like:
//result2/*
or also:
//result2/*/name()
Some technologies (specifically, data binding approaches) are optimized for handling XML whose structure is known at compile time. Others (like DOM and other DOM-like tree models - JDOM, XOM etc) are designed for handling XML whose structure is not known in advance. Use the tool for the job.
XSLT and XQuery try to blend both. In their schema-aware form, they can take advantage of static structure information when it is available. But more usually they are run in "untyped" mode, where there is no a-priori knowledge of element names or structure, and everything is handled as it comes. The XSLT rule-based processing paradigm is particularly well suited to "semi-structured" XML whose content is unpredictable or variable.

Missing NameSpace Information In XML file using EXIficient

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);

How to remove an specific xml attribute from org.w3c.dom.Document

I have this XML:
<Body xmlns:wsu="http://mynamespace">
<Ticket xmlns="http://othernamespace">
<Customer xlmns="">Robert</Customer>
<Products xmlns="">
<Product>a product</>
</Products>
</Ticket>
<Delivered xmlns="" />
<Payment xlmns="">cash</Payment>
</Body>
I am using Java to read it as a DOM document. I want remove the empty namespace attributes (i.e., xmlns=""). Is there any way to do that?
You need to understand that xmlns is a very special attribute. Basically, the xmlns="" is so that your Customer element is in the "unnamed" namespace, rather than the http://othernamespace namespace (and likewise for other elements which would otherwise inherit a default namespace from their ancestors).
If you want to get rid of the xmlns="", you basically need to put the elements into the appropriate namespace - so it's changing the element name. I don't think the W3C API lets you change the name of an element - you may well need to create a new element with the appropriate namespaced-name, and copy the content. Or if you're responsible for creating the document to start with, just use the right namespace.

Two utterly different JAXB annotated classes, same #XmlElement name. Possible?

We have an existing document tree. We want to wrap some of the elements inside this tree up within a element.
Depending on where in the tree we are, the element will hold very different content.
So I have a DocumentPromptLanguage class and a DocumentRouterLanguage class. They have different parents and different children but it makes sense that in XML they are both called <language>.
Is this possible without adapters or must the XML representation disambiguate by element name?
Sample:
<?xml version="1.0" encoding="utf-8"?>
<doc>
<info>
<language>
<iso639>en</iso639>
<value>This is a sample document</value>
</language>
<language>
<iso639>es</iso639>
<value>Se trata de un documento de muestra</value>
</language>
</info>
<someElement>
<route>
<language>
<iso639>en</iso639>
<possibleValues>Yes|No|Maybe</possibleValues>
<prefix>For</prefix>
</language>
<language>
<iso639>es</iso639>
<possibleValues>sí|not|tal vez</possibleValues>
<prefix>para</prefix>
</language>
<when>Tuesday</when>
<afterTime>17.30</afterTime>
<goto></goto>
</route>
</someElement>
</doc>
Yes the class mapped to the route element can have a property mapped with #XmlElement(name="language"), and so can the class mapped to the info element. This because the mappings are scoped by class.

Categories

Resources