Flash / Flex / ActionScript/XML/namespace

Материал из Web эксперт
Перейти к: навигация, поиск

As with element and attribute names, we can use the properties wildcard (*) with namespaces.

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var catalog:XML = <html xmlns:shop="http://www.example.ru/furniture"
             xmlns="http://www.w3.org/1999/xhtml">
         <head>
           <title>Catalog</title>
         </head>
         <body>
           <shop:table shop:id="4875">
Item Price
<shop:desc>Table</shop:desc> <shop:price>9.99</shop:price>
           </shop:table>
         </body>
       </html>
       
       
       trace(catalog..*::table);
   }
 }

}

       </source>
   
  


Create a namespace

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var shopNS:Namespace = new Namespace("http://www.example.ru/furniture");
       
       trace(shopNS);
   }
 }

}

       </source>
   
  


Create a reference to the default Namespace using the Namespace constructor:

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var catalog:XML = <html xmlns:shop="http://www.example.ru/furniture"
             xmlns="http://www.w3.org/1999/xhtml">
         <head>
           <title>Catalog</title>
         </head>
         <body>
           <shop:table shop:id="4875">
Item Price
<shop:desc>Table</shop:desc> <shop:price>9.99</shop:price>
           </shop:table>
         </body>
       </html>
       
       var shopNS:Namespace = catalog.namespace("shop");
       
       //default xml namespace = shopNS;
       for each (var table:XML in catalog..table) {
         trace(table..desc + ": " + table..price);
       }
       
   }
 }

}

       </source>
   
  


Creating Namespace-Qualified Elements and Attributes

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var htmlNS:Namespace = new Namespace("html","http://www.w3.org/1999/xhtml");
       var shopNS:Namespace = new Namespace("shop","http://www.example.ru/furniture");
       // Set the default namespace
       default xml namespace = htmlNS;
       // Create the root element
       var catalog:XML = <html/>;
       
       catalog.addNamespace(shopNS);
       catalog.head.title = "Catalog";
       catalog.body.shopNS::table = "";
       catalog.body.shopNS::table.@shopNS::id = "4875";
       catalog.body.shopNS::table.table = "";
       catalog.body.shopNS::table.table.@border = "1";
       catalog.body.shopNS::table.table.tr.td = "Item";
       catalog.body.shopNS::table.table.tr.td[1] = "Price";
       catalog.body.shopNS::table.table.tr.@align = "center";
       catalog.body.shopNS::table.table.tr[1] = "";
       catalog.body.shopNS::table.table.tr[1].@align = "left";
       catalog.body.shopNS::table.table.tr[1].td.shopNS::desc ="Table";
       catalog.body.shopNS::table.table.tr[1].td[1] = "";
       catalog.body.shopNS::table.table.tr[1].td[1].shopNS::price = "9.99";
       trace(catalog);
   }
 }

}

       </source>
   
  


Get prefix and uri from a namespace

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var example:XML = <example xmlns:test="http://foo.ru/">
                            <test:element>Lorem Ipsum</test:element>
                         </example>;
       var test:Namespace = example.namespace("test");
       trace(test.prefix); // Displays: test
       trace(test.uri); // Displays: http://foo.ru/
   }
 }

}

       </source>
   
  


Get xml namespace

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var catalog:XML = <html xmlns:shop="http://www.example.ru/furniture"
             xmlns="http://www.w3.org/1999/xhtml">
         <head>
           <title>Catalog</title>
         </head>
         <body>
           <shop:table shop:id="4875">
Item Price
<shop:desc>Table</shop:desc> <shop:price>9.99</shop:price>
           </shop:table>
         </body>
       </html>
       
       
       var htmlNS:Namespace = catalog.namespace(  );
       
       trace(htmlNS);
   }
 }

}

       </source>
   
  


Namespace objects return their URI value by default when the toString() method is used

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var fooNamespace:Namespace = new Namespace("http://www.bar.ru/");
       
       trace(fooNamespace.toString()); // Displays: http://www.bar.ru/
   }
 }

}

       </source>
   
  


Namespace with prefix

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var fooNamespace:Namespace = new Namespace("foo", "http://www.bar.ru/");
   }
 }

}

       </source>
   
  


Two Namespace objects are considered equal if, and only if, they have the same namespace name, regardless of their prefix.

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var product:XML = <someCorp:PRODUCT
                            xmlns:someCorp="http://www.example.ru/someCorp">
                            <someCorp:PRICE>99.99</someCorp:PRICE>
                          </someCorp:PRODUCT>;
       var ns1:Namespace = product.namespace("someCorp");
       var ns2:Namespace = new Namespace("sc", "http://www.example.ru/someCorp");
       trace(ns1 == ns2);  // Displays: true
   }
 }

}

       </source>
   
  


Use the namespaceDelarations() method to return an Array of all the namespaces defined for a given element.

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var example:XML = <example xmlns:test="http://foo.ru/"
                                  xmlns:mims="http://A.ru/"
                                  xmlns:roger="http://B.ru/"
       />;
       trace(example.namespaceDeclarations()[1]);
   }
 }

}

       </source>
   
  


Using the :: Operator with namespace

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var example:XML = <example xmlns:test="http://foo.ru/">
                            <test:element>L I</test:element>
                            <test:element test:attribute="D S A" />
                         </example>;
       var test:Namespace = example.namespace("test");
       trace(example.test::element[0]);
   }
 }

}

       </source>
   
  


Working with Namespaces in ActionScript

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var fooNamespace:Namespace = new Namespace("http://www.bar.ru/");
   }
 }

}

       </source>
   
  


Working with XML Namespaces

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var catalog:XML = <html xmlns:shop="http://www.example.ru/furniture"
             xmlns="http://www.w3.org/1999/xhtml">
         <head>
           <title>Catalog</title>
         </head>
         <body>
           <shop:table shop:id="4875">
Item Price
<shop:desc>Coffee Table</shop:desc> <shop:price>9.99</shop:price>
           </shop:table>
         </body>
       </html>
       
       var shopNS:Namespace = catalog.namespace("shop");
       trace(shopNS);
   }
 }

}

       </source>