Yes, that's the right code.
 
But I think it's "Gordian Knot".
 
- Gordon

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Iko Knyphausen
Sent: Wednesday, November 15, 2006 11:08 AM
To: [email protected]
Subject: RE: [flexcoders] Re: Namespace help please



Hey Gordon. First of all, thanks very much for taking the time and
laying it out for me. I really appreciate it. And it helped me solve the
Gordic Knot (no pun intended ;-)  Namespaces can appear a bit abstract
at times. I'll implement in a second, one immediate question that
crossed my mind... if I now wanted to address specific data members from
the dg.selectedItem object, would I be able to use

dg.selectedItem.ns::Name 

? Thanks again...looking forward to becoming a namespace pro ;-)

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Gordon Smith
Sent: Tuesday, November 14, 2006 11:54 PM
To: [email protected]
Subject: RE: [flexcoders] Re: Namespace help please

> If you use an "e4x" xml that has a namespace as dataprovider it will
not populate a datagrid properly.

Yes it can, but you have to use the correct E4X syntax for accessing
tags in namespaces; a complete working example is below.

Keep two things in mind:

1. Every child tag like <Project>, <Name>, and <Company> inherits the
default namespace that <Projects> declared. Therefore you have to always
refer to them as ns::Project, ns::Name, and ns::Company in E4X
expressions, where ns is a Namespace instance representing the
http://schemas.microsoft.com/project
<http://schemas.microsoft.com/project>  namespace.

2. You need to use a labelFunction rather than a dataField on a
DataGridColumn when you need to extract something that's in a namespace.
Why? Because when you specify a dataField String, the data is extracted
from the item as item[dataField]. For example, if you specify
dataField="Name" then it tries to extract item["Name"]. But this isn't
the right E4X expression since the <Name> tag has an implicit namespace.
The correct E4X expression would be item.ns::Name or item.ns::["Name"]
but there is no syntax where the namespace somehow goes inside the
String in the square brackets.

The dataField property dates back to the days before E4X, and it is just
too limited for general use with XML, unless we add a property like
dataNamespace to go along with it. (Which wouldn't be a bad idea, come
to think of it.) I suggest getting used to using labelFunctions; you can
be a namespace pro then.

- Gordon

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> " layout="vertical">
 
 <mx:Script>
 <![CDATA[
   
  private var xml:XML =
   <Projects xmlns="http://schemas.microsoft.com/project
<http://schemas.microsoft.com/project> ">
    <Project>
     <Name>Project1</Name>
     <Company>Company1</Company>
    </Project>
    <Project>
     <Name>Project2</Name>
     <Company>Company2</Company>
    </Project>
   </Projects>;
   
  private var ns:Namespace = new
Namespace("http://schemas.microsoft.com/project
<http://schemas.microsoft.com/project> ");
  
  private function nameLabelFunction(item:Object,
column:DataGridColumn):String
  {
   return item.ns::Name;
  }
  
  private function companyLabelFunction(item:Object,
column:DataGridColumn):String
  {
   return item.ns::Company;
  }
  
 ]]>
 </mx:Script>
 
 <mx:DataGrid id="dg" dataProvider="{xml.ns::Project}">
  <mx:columns>
   <mx:DataGridColumn headerText="Name"
labelFunction="nameLabelFunction"/>
   <mx:DataGridColumn headerText="Company"
labelFunction="companyLabelFunction"/>
  </mx:columns>
 </mx:DataGrid>
  
</mx:Application>

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Iko Knyphausen
Sent: Tuesday, November 14, 2006 9:29 PM
To: [email protected]
Subject: RE: [flexcoders] Re: Namespace help please

Thanks Gordon. I agree namespaces are there for a reason. My reason for
removing this one, is that without doing so I cannot bind to a datagrid.
If you use an "e4x" xml that has a namespace as dataprovider it will not
populate a datagrid properly. Thoughts?

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Gordon Smith
Sent: Tuesday, November 14, 2006 8:52 PM
To: [email protected]
Subject: RE: [flexcoders] Re: Namespace help please

I've never tried removeNamespace(), but I don't understand why you want
to remove one. In general, you can't always remove namespaces without
causing tag conflicts -- the namespaces are there for a reason, which is
to disambiguate tags with the same unqualified tagname. Your particular
example doesn't have this problem, but others you encounter in the
future might.

You should be accessing your tags in the namespace they live in. One way
to do this is

    var ns:Namespace = new
Namespace("http://schemas.microsoft.com/project
<http://schemas.microsoft.com/project> ");
    trace(xml..ns::Name);

- Gordon

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of iko_knyphausen
Sent: Tuesday, November 14, 2006 8:14 PM
To: [email protected]
Subject: [flexcoders] Re: Namespace help please

I could not help it, so I ended up working the XML string. The
"removeNamespace" method simply doesn't work - no exceptions thrown
either. I also tried removing the attribute "xmlns" but I guess that was
naive and desperate. Could add attributes ok - so had nothing to do with
the XML read-only or what have you...

Well if anyone can confirm that this is a bug (removeNamespace method) -
or that my assumptions/intentions for this method are wrong - that would
already help. Thanks.... 

public function killMSProjectRootAndNameSpace() : void
   {
    var sTemp : String = xmlProjectData.lastResult.toString();
    sTemp = sTemp.slice(54,-11);
    xmlProject = XMLList("<root>" + sTemp + "</root>");
   } 


--- In [email protected], "iko_knyphausen" <[EMAIL PROTECTED]> wrote:
>
> 
> Hello everyone,
> 
> I have fiddled for tooooooo long now trying to remove a namespace from
> an XML... here is the data (coming out of a MS Project XML file, being
> read via HTTPService, returnResult = "e4x")
> 
> <Project xmlns="http://schemas.microsoft.com/project
> <http://schemas.microsoft.com/project> ">
> <Name>Project3.xml</Name>
> <Company>....</Company>
> ...etc...
> </Project>
> 
> I need to remove the nameSpace in order to properly populate a
DataGrid.
> Here is the code for how I attempted to remove the namespace (function
> is triggered by the result event of the HTTPService):
> 
> public var xmlProject : XML = null;
> 
> public function killNameSpace() : void
> {
> var xmlTemp:XML = new XML(xmlProjectData.lastResult);
> Alert.show(xmlTemp.namespaceDeclarations().length); // shows 1 which
> is correct
> var nsd:Namespace = xmlTemp.namespace();
> Alert.show(nsd.toString()); // shows the correct content of xmlns
> attribute
> xmlProject = xmlTemp.removeNamespace(nsd);
> }
> 
> Unfortunately, the namespace is stubborn and is not removed.
Subsequent
> calls to
> 
> Alert.show(xmlProject.namespaceDeclarations().length);
> 
> still show 1 as length which is incorrect, and the xmlProject var
shows
> the full XML with the namespace attribute in tact.
> 
> What am I missing here? Did I misunderstand the purpose of
> removeNamspace()?
> 
> Thanks in advance for your help...
>

 

Reply via email to