Flash / Flex / ActionScript/XML/namespace
Содержание
- 1 As with element and attribute names, we can use the properties wildcard (*) with namespaces.
- 2 Create a namespace
- 3 Create a reference to the default Namespace using the Namespace constructor:
- 4 Creating Namespace-Qualified Elements and Attributes
- 5 Get prefix and uri from a namespace
- 6 Get xml namespace
- 7 Namespace objects return their URI value by default when the toString() method is used
- 8 Namespace with prefix
- 9 Two Namespace objects are considered equal if, and only if, they have the same namespace name, regardless of their prefix.
- 10 Use the namespaceDelarations() method to return an Array of all the namespaces defined for a given element.
- 11 Using the :: Operator with namespace
- 12 Working with Namespaces in ActionScript
- 13 Working with XML Namespaces
As with element and attribute names, we can use the properties wildcard (*) with namespaces.
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">
<table border="1">
<tr align="center">
<td>Item</td>
<td>Price</td>
</tr>
<tr align="left">
<td><shop:desc>Table</shop:desc></td>
<td><shop:price>9.99</shop:price></td>
</tr>
</table>
</shop:table>
</body>
</html>
trace(catalog..*::table);
}
}
}
Create a namespace
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);
}
}
}
Create a reference to the default Namespace using the Namespace constructor:
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">
<table border="1">
<tr align="center">
<td>Item</td>
<td>Price</td>
</tr>
<tr align="left">
<td><shop:desc>Table</shop:desc></td>
<td><shop:price>9.99</shop:price></td>
</tr>
</table>
</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);
}
}
}
}
Creating Namespace-Qualified Elements and Attributes
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);
}
}
}
Get prefix and uri from a namespace
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/
}
}
}
Get xml namespace
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">
<table border="1">
<tr align="center">
<td>Item</td>
<td>Price</td>
</tr>
<tr align="left">
<td><shop:desc>Table</shop:desc></td>
<td><shop:price>9.99</shop:price></td>
</tr>
</table>
</shop:table>
</body>
</html>
var htmlNS:Namespace = catalog.namespace( );
trace(htmlNS);
}
}
}
Namespace objects return their URI value by default when the toString() method is used
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/
}
}
}
Namespace with prefix
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var fooNamespace:Namespace = new Namespace("foo", "http://www.bar.ru/");
}
}
}
Two Namespace objects are considered equal if, and only if, they have the same namespace name, regardless of their prefix.
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
}
}
}
Use the namespaceDelarations() method to return an Array of all the namespaces defined for a given element.
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]);
}
}
}
Using the :: Operator with namespace
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]);
}
}
}
Working with Namespaces in ActionScript
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var fooNamespace:Namespace = new Namespace("http://www.bar.ru/");
}
}
}
Working with XML Namespaces
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">
<table border="1">
<tr align="center">
<td>Item</td>
<td>Price</td>
</tr>
<tr align="left">
<td><shop:desc>Coffee Table</shop:desc></td>
<td><shop:price>9.99</shop:price></td>
</tr>
</table>
</shop:table>
</body>
</html>
var shopNS:Namespace = catalog.namespace("shop");
trace(shopNS);
}
}
}