Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Sapphire » problem having namespace declaration within xml tree
problem having namespace declaration within xml tree [message #901008] Thu, 09 August 2012 11:50 Go to next message
Eyck Jentzsch is currently offline Eyck JentzschFriend
Messages: 21
Registered: August 2012
Junior Member
Hi,
Sapphire is a great approach to create form based xml editors and I'm pleased how fast you get results.
Based on the 0.5.2 version I wrote a model and an UI definition against a schema definition. But I ran into a problem I do not have a clue how to address Confused .
The schema requests to have a name space on a node in the xml tree similar to
<root>
  <equation>
    <math xmlns=".../MathML">
      <apply>
         <times/>
         <ci> World </ci>
         <ci> birth </ci>
      </apply>
    </math>
  </equation>
</root>

Currently Sapphire stops parsing at the node having the xmlns specification. If I remove that in the xml everything works fine. I tried to annotate the IMath interface:
@XmlNamespace(uri = ".../MathML", prefix=" " )
public interface IMath extends IMathBase {
...
}

But this did not help and I hit a bug: if I set the prefix to an empty value Sapphire tries to create an invalid DOM element having the notion like "1:apply".

Does anybody have an idea on how to fix this?

Cheers
-Eyck
Re: problem having namespace declaration within xml tree [message #901119 is a reply to message #901008] Thu, 09 August 2012 21:13 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
Take a look at ContactAddress in the contacts sample for an example of switching namespace in the middle of a document. You need to qualify names in various xml binding annotations if the namespace is different from parent DOM element. In xml binding annotations, no namespace prefix means use the namespace of the parent DOM element.

@GenerateImpl
@XmlNamespace( uri = "http://www.eclipse.org/sapphire/samples/address", prefix = "a" )

public interface ContactAddress extends Address
{
    ModelElementType TYPE = new ModelElementType( ContactAddress.class );
    
    // *** Street ***

    @XmlBinding( path = "a:street" )

    ValueProperty PROP_STREET = new ValueProperty( TYPE, Address.PROP_STREET );
    
    // *** City ***

    @XmlBinding( path = "a:city" )

    ValueProperty PROP_CITY = new ValueProperty( TYPE, Address.PROP_CITY );

    // *** State ***

    @XmlBinding( path = "a:state" )

    ValueProperty PROP_STATE = new ValueProperty( TYPE, Address.PROP_STATE );

    // *** ZipCode ***

    @XmlBinding( path = "a:zip" )

    ValueProperty PROP_ZIP_CODE = new ValueProperty( TYPE, Address.PROP_ZIP_CODE );
}
Re: problem having namespace declaration within xml tree [message #901196 is a reply to message #901119] Fri, 10 August 2012 09:25 Go to previous messageGo to next message
Eyck Jentzsch is currently offline Eyck JentzschFriend
Messages: 21
Registered: August 2012
Junior Member
THX for the clarification. But this seems to be a limitation of Sapphire rather than a requirement from W3C (as it can be found on the w3c website under /TR/REC-xml-names/#scoping-defaulting)

Unfortunately my spec does not require to use a namespace prefix and there are plenty of xml documents already existing I have to cope with. Any idea on how to approach this? Would this be a possible enhancement to Sapphire?

-Eyck
Re: problem having namespace declaration within xml tree [message #901269 is a reply to message #901196] Fri, 10 August 2012 14:50 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
The prefix that is specified in @XmlNamespace is there largely to facilitate namespace resolution within various XML binding annotations. Once you've linked the two, Sapphire will be able to read and update your XML documents regardless of the style of namespace declaration.

Here is roughly how this works...

1. Resolve namespace of element names in binding annotations.

2. When reading, only match based on namespace URI.

3. When creating a new element, look for an existing namespace declaration in element hierarchy. If found, use the namespace as declared (no prefix or prefix as specified in the document).

4. If an existing namespace declaration is not found, Sapphire needs to establish one. Here, there are many legal options, all leading to semantically equivalent documents. To avoid complicating the XML binding annotations, Sapphire implements one of the common options. The namespace declaration is written in the root element using the namespace prefix as specified in @XmlNamespace. If prefix "p" is already in use, Sapphire will look for an available one using "p1", "p2", "p3", etc. system. Declaring namespace at the root of the document is commonly preferred over local declarations as it results in fewer namespace declarations.

Once you get this to work, if you are still unhappy with the behavior in #4, Sapphire provides a way to write a custom binding for particular property. For instance, see @CustomXmlElementBinding and ElementBindingImpl. Of course, I would recommend that you double-check whether achieving a particular behavior when establishing a namespace is worth the extra cost to implement and test a custom binding.

Hope this helps.

- Konstantin
Re: problem having namespace declaration within xml tree [message #935008 is a reply to message #901269] Sat, 06 October 2012 15:15 Go to previous messageGo to next message
Eyck Jentzsch is currently offline Eyck JentzschFriend
Messages: 21
Registered: August 2012
Junior Member
Hi Konstantin,
I dug a little bit deeper into the problem and think I hit a bug. Here is the refined example
<root xmlns="http : //www.it-jw.com/some/name/space">
  <equation>
    <math xmlns="http : //www.w3.org/1998/Math/MathML">
      <apply>
         <times/>
         <ci> World </ci>
         <ci> birth </ci>
      </apply>
    </math>
  </equation>
</root>

The problem is that during Math.refreshProperty() the namespace handling is inconsistent between Sapphire and org.w3c.dom.Element. In VirtualChildXmlResource.getXmlElement() Sapphire looks for the QName "{http : //www.it-jw.com/some/name/space}math" while the respective XmlElement.getNamespace() returns the namespace opened by the math element which is "http : //www.w3.org/1998/Math/MathML". Hence the child element is not found and not shown...

I tested this with the 0.5.3 release of Sapphire.

If you would give me a pointer where to fix I would volunteer to provide a patch
Cheers
-Eyck
Re: problem having namespace declaration within xml tree [message #937047 is a reply to message #935008] Mon, 08 October 2012 15:58 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
It sounds like there is an issue with how your XML bindings are specified. The resolution of namespace in the binding annotations isn't related to resolution in DOM. If you post your model, I can help you straighten it out.

- Konstantin
Re: problem having namespace declaration within xml tree [message #937211 is a reply to message #937047] Mon, 08 October 2012 19:44 Go to previous messageGo to next message
Eyck Jentzsch is currently offline Eyck JentzschFriend
Messages: 21
Registered: August 2012
Junior Member
Hi Konstantin,
THX for the offer, here are the respective classes:
@GenerateImpl
public interface IEquation {
    ModelElementType TYPE = new ModelElementType( IEquation.class );

    @Type( base = IMath.class )
    @XmlBinding( path = "math" )
    @Label( standard = "math" )
    @Required
    ImpliedElementProperty PROP_MATH = new ImpliedElementProperty( TYPE, "math" );
    IMath getMath();

    @Type( base = IParameter.class )
    @XmlListBinding(path="listOfParameters", mappings = { @XmlListBinding.Mapping( element = "reactant", type = IParameter.class ) } )
    @Label( standard = "listOfParameters" )
    ListProperty PROP_LISTOFPARAMETERS = new ListProperty( TYPE, "listOfParameters" );
    ModelElementList<IParameter> getListOfParameters();

}

@GenerateImpl
public interface IMathBase extends IModelElement {
    ModelElementType TYPE = new ModelElementType( IMathBase.class );

    @XmlBinding( path = "@class" )
    @Label( standard = "class" )
    ValueProperty PROP_CLAZZ = new ValueProperty( TYPE, "clazz" );
    Value<String> getClazz();
    void setClazz( String value );

    @XmlBinding( path = "@style" )
    @Label( standard = "style" )
    ValueProperty PROP_STYLE = new ValueProperty( TYPE, "style" );
    Value<String> getStyle();
    void setStyle( String value );

    @XmlBinding( path = "@id" )
    @Label( standard = "id" )
    ValueProperty PROP_ID = new ValueProperty( TYPE, "id" );
    Value<String> getId();
    void setId( String value );

}

@GenerateImpl
@XmlNamespace(uri = "http : //www.w3.org/1998/Math/MathML" )
@Label(standard="Math")
public interface IMath extends IMathBase {
	
    ModelElementType TYPE = new ModelElementType( IMath.class );

    @Type( base = IOperand.class, possible = { IApply.class, ICi.class, ICn.class })
    @XmlElementBinding(
    		path="",
    		mappings = {
    				@XmlElementBinding.Mapping( element = "apply", type = IApply.class ),
    				@XmlElementBinding.Mapping( element = "ci", type = ICi.class ),
    				@XmlElementBinding.Mapping( element = "cn", type = ICn.class )
    		} )
    ElementProperty PROP_OPERAND = new ElementProperty(TYPE, "operand");
    ModelElementHandle<IOperand> getOperand();
	
}


- Eyck
PS: I had to modify the namespace uri as I'm currently not allowed to post links....
Re: problem having namespace declaration within xml tree [message #937245 is a reply to message #937211] Mon, 08 October 2012 20:31 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
You need to modify IEquation as follows and remove @XmlNamespace from IMath.

@XmlNamespace(uri = "http://www.w3.org/1998/Math/MathML", prefix="m")
public interface IEquation {
    @XmlBinding( path = "m:math" )
    ImpliedElementProperty PROP_MATH = new ImpliedElementProperty( TYPE, "math" );
    ...
}


- Konstantin
Re: problem having namespace declaration within xml tree [message #937610 is a reply to message #937245] Tue, 09 October 2012 05:25 Go to previous messageGo to next message
Eyck Jentzsch is currently offline Eyck JentzschFriend
Messages: 21
Registered: August 2012
Junior Member
Hi Konstantin,
this works, although it is not what I would like to have. Semantically it is correct but syntactically I have prefixed tags from the math tag on.
The spec of the xml format states that the namespace declaration should be at the math element and my suspicion is that other implementations do not cope well with this syntax.
Are there any other ways to handle this?
Regards

-Eyck
Re: problem having namespace declaration within xml tree [message #938210 is a reply to message #937610] Tue, 09 October 2012 17:05 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
Quote:
The spec of the xml format states that the namespace declaration should be at the math element


That's not correct. The XML spec allows namespace declaration to be anywhere above the point where it is used. Sapphire uses the strategy of placing the declaration on the root element as it minimizes the number of namespace declarations.

Quote:
and my suspicion is that other implementations do not cope well with this syntax.


Any compliant XML parser will not have any issues with XML produced by Sapphire.

Quote:
Are there any other ways to handle this?


If you must control the style of the namespace declaration, you can implement a custom binding for your IEquation.Math property, which allows you to implement your own logic for creating the XML element. The rest of the bindings in your model can remain declarative. See @CustomXmlElementBinding, ElementBindingImpl, StandardImpliedXmlElementBindingImpl and VirtualChildXmlResource.

- Konstantin
Re: problem having namespace declaration within xml tree [message #938873 is a reply to message #938210] Wed, 10 October 2012 08:24 Go to previous messageGo to next message
Eyck Jentzsch is currently offline Eyck JentzschFriend
Messages: 21
Registered: August 2012
Junior Member
Hi Konstantin,
sorry to beat a dead horse but are there any plans to implement the possibility to change the default namespace according to http : //www.w3.org/TR/REC-xml-names/#defaulting

Best regards

-Eyck
Re: problem having namespace declaration within xml tree [message #939227 is a reply to message #938873] Wed, 10 October 2012 14:46 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
I don't anticipate that we would start supporting this style of XML writing via annotations. Note that Sapphire will read any legal namespace style via annotations. Your best bet is a custom binding if this style is really important to your usecase.

- Konstantin
Re: problem having namespace declaration within xml tree [message #939368 is a reply to message #939227] Wed, 10 October 2012 17:47 Go to previous message
Eyck Jentzsch is currently offline Eyck JentzschFriend
Messages: 21
Registered: August 2012
Junior Member
We will see what happens. Currently I will go with the annotation you described. But who knows what future will bring...
Anyway, many THX for your support

-Eyck

[Updated on: Wed, 10 October 2012 17:47]

Report message to a moderator

Previous Topic:Is it possible to use buried xml tree nodes in diagram
Next Topic:Getting XmlElement of an IModelElement
Goto Forum:
  


Current Time: Wed Apr 24 13:50:57 GMT 2024

Powered by FUDForum. Page generated in 0.04747 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top