<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xml.xsl" type="text/xsl"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>nulog, NULL::something : out of the headphone &gt; 2006 &gt; March &gt; 01 &gt; てきとう E4X</title>
  <link href="http://lowreal.net/logs/2006/03/01/1"/>
  <icon>http://lowreal.net/img/banner.png</icon>
  <link rel="self" type="application/atom+xml" href="http://lowreal.net/logs/2006/03/01/1.atom"/>
  <link rel="alternate" type="application/xhtml+xml" href="http://lowreal.net/logs/2006/03/01/1.xhtml"/>
  <updated>2006-03-01T14:11:49+09:00</updated>
  <author>
    <name>cho45(砂糖)</name>
  </author>
  <id>http://lowreal.net/2006/03/01/1</id>
  <entry>
    <title>てきとう E4X</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2006/03/01/1.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2006/03/01/1.xhtml"/>
    <updated>2006-03-01T14:11:49+09:00</updated>
    <published>2006-03-01T14:11:49+09:00</published>
    <id>http://lowreal.net/2006/03/01/1</id>
    <category term="e4x"/>
    <category term="js"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>どうにも使い道が少ない <abbr title="ECMAScript for XML">E4X</abbr> をちょっとメモる。</p>
        <p>基本的に <abbr title="XML Path language">XPath</abbr> (の省略記法) に似ているので、相違点とかあげつつみたいな。<abbr title="XML Path language">XPath</abbr> は <abbr title="ECMAScript for XML">E4X</abbr> に似せるため省略記法を使います。省略しないと<em>全然違う</em>。</p>
        <pre class="ECMAScript">
var doc = &lt;root&gt;
    &lt;foo&gt;hoge&lt;/foo&gt;
    &lt;bar&gt;huga&lt;/bar&gt;
    &lt;baz&gt;
        &lt;foo name="neko"&gt;
            &lt;bar&gt;pqpq&lt;/bar&gt;
        &lt;/foo&gt;
    &lt;/baz&gt;
&lt;/root&gt;;

</pre>
        <dl title="XPath と比べる">
          <dt>
            <code>doc.baz..foo</code>
          </dt>
          <dd>
            <code>/root/baz//foo</code>
          </dd>
          <dd><abbr title="ECMAScript for XML">E4X</abbr> ではルートノードがない。</dd>
          <dt>
            <code>doc..foo.(@name == "neko")</code>
          </dt>
          <dd>
            <code>//foo[@name = 'neko']</code>
          </dd>
          <dd><abbr title="ECMAScript for XML">E4X</abbr> では括弧の前にドットがいる。括弧の中は ECMAScript の式そのまま。すなわち <code>or</code> は <code>||</code>, <code>and</code> は <code>&amp;&amp;</code>。</dd>
          <dt>
            <code>doc.*</code>
          </dt>
          <dd>
            <code>/root/*</code>
          </dd>
          <dd><code>*</code> はそのままの意味。<code>@*</code> もそのまま書ける。</dd>
          <dt>
            <code>doc..foo[0]</code>
          </dt>
          <dd>
            <code>//foo[1]</code>
          </dd>
          <dd>数値は普通の ECMAScript 配列とかとと同じように 0 基準。</dd>
          <dt>
            <code>doc..foo.length()</code>
          </dt>
          <dd>
            <code>count(//foo)</code>
          </dd>
          <dd><abbr title="XML Path language">XPath</abbr> の例はどうでもいいけど、数を知りたいときは <code>length()</code> を使う。括弧をつけず <code>length</code> と書くと <samp>length</samp> 要素を選択しようとするので注意が必要。</dd>
        </dl>
        <p><code>for each</code> とかいう構文がある (構文？)</p>
        <pre class="ECMAScript">
for each (ele in doc.*) {
    // hoge
}
</pre>
        <p><samp>XMLList</samp> とかいうオブジェクトがある。無名の要素をルートにして作る。ちょっときもい。</p>
        <pre class="ECMAScript">
// XMLList (DOM の DocumentFragment みたいなの)
var xmlList = &lt;&gt;
   &lt;li&gt;1&lt;/li&gt;
   &lt;li&gt;2&lt;/li&gt;
   &lt;li&gt;3&lt;/li&gt;
&lt;/&gt;;

var doc = &lt;root&gt;
   &lt;li&gt;999&lt;/li&gt;
&lt;/root&gt;;

doc.appendChild(xmlList);
doc.toString() //=&gt;
  &lt;root&gt;
     &lt;li&gt;999&lt;/li&gt;
     &lt;li&gt;1&lt;/li&gt;
     &lt;li&gt;2&lt;/li&gt;
     &lt;li&gt;3&lt;/li&gt;
  &lt;/root&gt;
</pre>
        <p>名前空間</p>
        <pre class="ECMAScript">var rssDoc = &lt;rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"&gt;

    &lt;channel rdf:about="http://example.com/"&gt;
        &lt;title&gt;Example&lt;/title&gt;
        &lt;link&gt;http://example.com/&lt;/link&gt;
        &lt;items&gt;

            &lt;rdf:Seq&gt;
                &lt;rdf:li rdf:resource="http://example.com/3"/&gt;
                &lt;rdf:li rdf:resource="http://example.com/2"/&gt;
                &lt;rdf:li rdf:resource="http://example.com/1"/&gt;
            &lt;/rdf:Seq&gt;
        &lt;/items&gt;
    &lt;/channel&gt;

    &lt;item rdf:about="http://example.com/3"&gt;
        &lt;title&gt;ex3&lt;/title&gt;
        &lt;link&gt;http://example.com/3&lt;/link&gt;
        &lt;dc:date&gt;2006-03-01T13:52:32+09:00&lt;/dc:date&gt;
    &lt;/item&gt;

    &lt;item rdf:about="http://example.com/2"&gt;
        &lt;title&gt;ex2&lt;/title&gt;
        &lt;link&gt;http://example.com/2&lt;/link&gt;
        &lt;dc:date&gt;2006-03-01T13:52:30+09:00&lt;/dc:date&gt;
    &lt;/item&gt;

    &lt;item rdf:about="http://example.com/1"&gt;
        &lt;title&gt;ex1&lt;/title&gt;
        &lt;link&gt;http://example.com/1&lt;/link&gt;
        &lt;dc:date&gt;2006-03-01T13:52:24+09:00&lt;/dc:date&gt;
    &lt;/item&gt;
&lt;/rdf:RDF&gt;;

var rdf  = new Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
var rdfs = new Namespace("http://www.w3.org/2000/01/rdf-schema#");
var rss  = new Namespace("http://purl.org/rss/1.0/");
// var dc   = new Namespace("http://purl.org/dc/elements/1.1/");

for each (i in rssDoc.rss::channel.rss::items.rdf::Seq.rdf::li) {
    var item = rssDoc.rss::item.(@rdf::about == i.@rdf::resource);
    alert(item.rss::title);
}</pre>
        <p>見ればわかるように Namespace オブジェクト作って <code>::</code> を解決に使うみたい。使わないなら別に宣言しなくてもいい。あくまで <abbr title="ECMAScript for XML">E4X</abbr> でアクセスするために名前をつける (=変数に代入する) ので、別にどんな名前でもいい。</p>
        <pre>
var h = new Namespace("http://www.w3.org/1999/xhtml");
default xml namespace = h;
var HtmlDoc = &lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;E4X&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
       &lt;h1&gt;for (;;)&lt;/h1&gt;
    &lt;/body&gt;
&lt;/html&gt;;

</pre>
        <p>みたいなこともできるみたい。</p>
        <p><abbr title="XML Path language">XPath</abbr> における <code>following-sibling</code> 軸のようなメソッドはないっぽい？とりあえず眺めてみたらないっぽい。よくわからん。</p>
        <p>しかし、GreaseMonkey で <abbr title="ECMAScript for XML">E4X</abbr> を使うにしても、XMLHttpReqeust のハンドラぐらいでしか使えなくてなんともかんとも。もちろん生成するのは自由なんだけど……</p>
        <ins datetime="2006-03-01T15:10:04+09:00">
          <p><code>@</code> の働きがいまいちわからない。ちゃんと仕様読もう</p>
        </ins>
      </div>
    </content>
  </entry>
</feed>
