RE: [flexcoders] Need help tranforming some XML

2009-06-22 Thread Tracy Spratt
"... break that long line of E4X into separate components..."
I am with you here.  Do it one step at a time and check each step with a
trace(xml.toXMLString()).  If you really like concise, hard to read code,
you can squash it back together once you get it working.  

Tracy Spratt,
Lariat Services, development services available

-Original Message-
From: flexcoders@yahoogroups.com [mailto:flexcod...@yahoogroups.com] On
Behalf Of Ian Thomas
Sent: Monday, June 22, 2009 1:26 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Need help tranforming some XML

Hi Jason,
  Stepping through it, it works when you break that long line of E4X
into separate components.

(For some reason people seem to want to write really long E4X
statements - I'm not quite sure why...)

Like this, your loop works.

for each (var contentRowItem:XML in _contentXML..row)
{
   var moduleName:String = contentRowItem.Module;
   var topicXML:XML = new XML("");
   topicx...@title = contentRowItem.LinkTitle;
   // This e4x creates an XMLList, so pull off the first entry
   var module:XML=finalXML.module.(@title == moduleName)[0];
   // Likewise
   var
topics:XML=module..subsection.(@title==contentRowItem.Subsection).topics[0];
   topics.appendChild(topicXML);
}

However - I'd include a bit of error checking for those XMLLists...

for each (var contentRowItem:XML in _contentXML..row)
{
   var moduleName:String = contentRowItem.Module;
   var topicXML:XML = new XML("");
   topicx...@title = contentRowItem.LinkTitle;
   var modulesList:XMLList=finalXML.module.(@title == moduleName);
   if (modulesList.length()!=1)
 throw new Error("Can't find single module with title:"+moduleName);

   var topicsList:XMLList=
modulesList[0]..subsection.(@title==contentRowItem.Subsection).topics;
   if (topicsList.length()!=1)
 throw new Error("Can't find single subsection with
title:"+contentRowItem.Subsection);

   topicsList[0].appendChild(topicXML);
}

My general approach to this type of stuff is: break it down. Store and
trace out each part of your e4x statement in a separate variable, bit
by bit. That'll show you where it's going wrong.

HTH,
   Ian

On Mon, Jun 22, 2009 at 5:54 PM, Merrill,
Jason wrote:
>
>
> Been wrestling with a script that transforms some XML from one schema to
> another for a while, and have most of the transformation working, but
having
> a hard time wrapping my head around the last little bit.  Probably easy
for
> some E4X XML gurus out there.  The original XML (built automatically by
the
> sever-side app) is fairly flat and looks like this:
>
> (_contentXML)
>
> 
>
>   
>
>
>
>     
>
>   1
>
> iBuild
>
> Assumptions
>
>   Starting iRequire
>
>   To start iRequire, you will first need to do a few things,
> like create a user name and password.
>
>   Sanders, Larry
>
>   …etc.
>
>     
>
>
>
>     
>
>   2
>
> iDeliver
>
> Technical Constraints
>
>     …etc.
>
> What I need to do, is take it from that “flat” form, and based on the
values
> of some of the nodes (,, and  values), make new
> more hierarchical XML that is based on the Module, Subsection, and Topic
> values in the XML, so that it looks like this:
>
> 
>
> 
>
>         
>
>         
>
>                 
>
>                     
>
>                          iRequire”>
>
>                             
>
>                                 To start
> iRequire, you will first need to do a…
>
>                             
>
> Sanders, Larry
>
>                     
>
>             
>
>             
>
>         
>
>         
>
>             
>
>                 
>
>             ..etc.
>
> I have it assembled this far, where the module nodes are created just
fine,
> and the subsections are appearing under the right module with the right
> title attribute:
>
> (finalXML)
>
> 
>
> 
>
>         
>
>         
>
>                 
>
>                     
>
> 
>
>             
>
>                     
>
> 
>
>             
>
>         
>
>         
>
>             
>
>                 
>
> 
>
> 
>
>             
>
>                   

Re: [flexcoders] Need help tranforming some XML

2009-06-22 Thread Ian Thomas
Hi Jason,
  Stepping through it, it works when you break that long line of E4X
into separate components.

(For some reason people seem to want to write really long E4X
statements - I'm not quite sure why...)

Like this, your loop works.

for each (var contentRowItem:XML in _contentXML..row)
{
   var moduleName:String = contentRowItem.Module;
   var topicXML:XML = new XML("");
   topicx...@title = contentRowItem.LinkTitle;
   // This e4x creates an XMLList, so pull off the first entry
   var module:XML=finalXML.module.(@title == moduleName)[0];
   // Likewise
   var 
topics:XML=module..subsection.(@title==contentRowItem.Subsection).topics[0];
   topics.appendChild(topicXML);
}

However - I'd include a bit of error checking for those XMLLists...

for each (var contentRowItem:XML in _contentXML..row)
{
   var moduleName:String = contentRowItem.Module;
   var topicXML:XML = new XML("");
   topicx...@title = contentRowItem.LinkTitle;
   var modulesList:XMLList=finalXML.module.(@title == moduleName);
   if (modulesList.length()!=1)
 throw new Error("Can't find single module with title:"+moduleName);

   var topicsList:XMLList=
modulesList[0]..subsection.(@title==contentRowItem.Subsection).topics;
   if (topicsList.length()!=1)
 throw new Error("Can't find single subsection with
title:"+contentRowItem.Subsection);

   topicsList[0].appendChild(topicXML);
}

My general approach to this type of stuff is: break it down. Store and
trace out each part of your e4x statement in a separate variable, bit
by bit. That'll show you where it's going wrong.

HTH,
   Ian

On Mon, Jun 22, 2009 at 5:54 PM, Merrill,
Jason wrote:
>
>
> Been wrestling with a script that transforms some XML from one schema to
> another for a while, and have most of the transformation working, but having
> a hard time wrapping my head around the last little bit.  Probably easy for
> some E4X XML gurus out there.  The original XML (built automatically by the
> sever-side app) is fairly flat and looks like this:
>
> (_contentXML)
>
> 
>
>   
>
>
>
>     
>
>   1
>
> iBuild
>
> Assumptions
>
>   Starting iRequire
>
>   To start iRequire, you will first need to do a few things,
> like create a user name and password.
>
>   Sanders, Larry
>
>   …etc.
>
>     
>
>
>
>     
>
>   2
>
> iDeliver
>
> Technical Constraints
>
>     …etc.
>
> What I need to do, is take it from that “flat” form, and based on the values
> of some of the nodes (,, and  values), make new
> more hierarchical XML that is based on the Module, Subsection, and Topic
> values in the XML, so that it looks like this:
>
> 
>
> 
>
>         
>
>         
>
>                 
>
>                     
>
>                          iRequire”>
>
>                             
>
>                                 To start
> iRequire, you will first need to do a…
>
>                             
>
> Sanders, Larry
>
>                     
>
>             
>
>             
>
>         
>
>         
>
>             
>
>                 
>
>             ..etc.
>
> I have it assembled this far, where the module nodes are created just fine,
> and the subsections are appearing under the right module with the right
> title attribute:
>
> (finalXML)
>
> 
>
> 
>
>         
>
>         
>
>                 
>
>                     
>
> 
>
>             
>
>                     
>
> 
>
>             
>
>         
>
>         
>
>             
>
>                 
>
> 
>
> 
>
>             
>
>                     
>
> 
>
> 
>
>                     
>
> 
>
> 
>
> 
>
> 
>
>             
>
>         
>
>         …etc.
>
> But what I can’t seem to get is that last part of how to insert the right
> topic data under each  node (Overview, ID, Author, etc).  I’d be
> happy enough just inserting the entire original  node into the right
>  node.  My current attempt looks like this:
>
> (finalXML is my “new“ XML and _contentXML is the original XML)
>
> for each (var contentRowItem:XML in _contentXML..row)
>
> {
>
>     var moduleName:String = contentRowItem.Module;
>
>     var topicXML:XML = new XML();
>
>     topicx...@title = contentRowItem.LinkTitle;
>
>     finalXML..module.(@title == moduleName)..subsection.(@title ==
> contentRowItem.Subsection).topics.appendChild(topicXML)
>
> }
>
> But with this approach, I get the error, “TypeError: Error #1086: The
> appendChild method only works on lists containing one item.” I’ve also tried
> some forms of theNode.setChildren and so many ways to loop m

Re: [flexcoders] Need help tranforming some XML

2009-06-22 Thread Ian Thomas
Oh *sigh* - mind the line breaks.

Ian

On Mon, Jun 22, 2009 at 6:25 PM, Ian Thomas wrote:
> Hi Jason,
>  Stepping through it, it works when you break that long line of E4X
> into separate components.
>
> (For some reason people seem to want to write really long E4X
> statements - I'm not quite sure why...)
>
> Like this, your loop works.
>
> for each (var contentRowItem:XML in _contentXML..row)
> {
>   var moduleName:String = contentRowItem.Module;
>   var topicXML:XML = new XML("");
>   topicx...@title = contentRowItem.LinkTitle;
>   // This e4x creates an XMLList, so pull off the first entry
>   var module:XML=finalXML.module.(@title == moduleName)[0];
>   // Likewise
>   var 
> topics:XML=module..subsection.(@title==contentRowItem.Subsection).topics[0];
>   topics.appendChild(topicXML);
> }
>
> However - I'd include a bit of error checking for those XMLLists...
>
> for each (var contentRowItem:XML in _contentXML..row)
> {
>   var moduleName:String = contentRowItem.Module;
>   var topicXML:XML = new XML("");
>   topicx...@title = contentRowItem.LinkTitle;
>   var modulesList:XMLList=finalXML.module.(@title == moduleName);
>   if (modulesList.length()!=1)
>     throw new Error("Can't find single module with title:"+moduleName);
>
>   var topicsList:XMLList=
> modulesList[0]..subsection.(@title==contentRowItem.Subsection).topics;
>   if (topicsList.length()!=1)
>     throw new Error("Can't find single subsection with
> title:"+contentRowItem.Subsection);
>
>   topicsList[0].appendChild(topicXML);
> }
>
> My general approach to this type of stuff is: break it down. Store and
> trace out each part of your e4x statement in a separate variable, bit
> by bit. That'll show you where it's going wrong.
>
> HTH,
>   Ian
>
> On Mon, Jun 22, 2009 at 5:54 PM, Merrill,
> Jason wrote:
>>
>>
>> Been wrestling with a script that transforms some XML from one schema to
>> another for a while, and have most of the transformation working, but having
>> a hard time wrapping my head around the last little bit.  Probably easy for
>> some E4X XML gurus out there.  The original XML (built automatically by the
>> sever-side app) is fairly flat and looks like this:
>>
>> (_contentXML)
>>
>> 
>>
>>   
>>
>>
>>
>>     
>>
>>   1
>>
>> iBuild
>>
>> Assumptions
>>
>>   Starting iRequire
>>
>>   To start iRequire, you will first need to do a few things,
>> like create a user name and password.
>>
>>   Sanders, Larry
>>
>>   …etc.
>>
>>     
>>
>>
>>
>>     
>>
>>   2
>>
>> iDeliver
>>
>> Technical Constraints
>>
>>     …etc.
>>
>> What I need to do, is take it from that “flat” form, and based on the values
>> of some of the nodes (,, and  values), make new
>> more hierarchical XML that is based on the Module, Subsection, and Topic
>> values in the XML, so that it looks like this:
>>
>> 
>>
>> 
>>
>>         
>>
>>         
>>
>>                 
>>
>>                     
>>
>>                         > iRequire”>
>>
>>                             
>>
>>                                 To start
>> iRequire, you will first need to do a…
>>
>>                             
>>
>> Sanders, Larry
>>
>>                     
>>
>>             
>>
>>             
>>
>>         
>>
>>         
>>
>>             
>>
>>                 
>>
>>             ..etc.
>>
>> I have it assembled this far, where the module nodes are created just fine,
>> and the subsections are appearing under the right module with the right
>> title attribute:
>>
>> (finalXML)
>>
>> 
>>
>> 
>>
>>         
>>
>>         
>>
>>                 
>>
>>                     
>>
>> 
>>
>>             
>>
>>                     
>>
>> 
>>
>>             
>>
>>         
>>
>>         
>>
>>             
>>
>>                 
>>
>> 
>>
>> 
>>
>>             
>>
>>                     
>>
>> 
>>
>> 
>>
>>                     
>>
>> 
>>
>> 
>>
>> 
>>
>> 
>>
>>             
>>
>>         
>>
>>         …etc.
>>
>> But what I can’t seem to get is that last part of how to insert the right
>> topic data under each  node (Overview, ID, Author, etc).  I’d be
>> happy enough just inserting the entire original  node into the right
>>  node.  My current attempt looks like this:
>>
>> (finalXML is my “new“ XML and _contentXML is the original XML)
>>
>> for each (var contentRowItem:XML in _contentXML..row)
>>
>> {
>>
>>     var moduleName:String = contentRowItem.Module;
>>
>>     var topicXML:XML = new XML();
>>
>>     topicx...@title = contentRowItem.LinkTitle;
>>
>>     final