XML を処理するための関数

XML を操作する ECMAScript を記述するときには、その作業を容易にするように設計されたいくつかのメンバー関数を利用できます。このトピックでは、WebLogic Workshop に付属の拡張 ECMAScript で利用可能な関数を説明します。

要素名がこのトピックで示されている関数の名前と衝突する場合もあります。その場合には、次の例で示されているような構文を使用してください。この例では、衝突する要素名の先頭に ::(二重コロン)が付けられています。

/*
 * Return the first <parent> element.
 * This avoids a conflict with the parent function.
 */
var firstParent = xmlFamilies.kid.::parent[0];

XML 用の ECMAScript 関数

関数

説明

xmlElement.appendChild(newChild)

XML 値の既存の子の後に新しい子ノードを挿入する

xmlElement.attribute(attributeName)

指定された属性の値を返す

xmlElement.attributes()

指定された要素の属性リストを返す

xmlElement.child(childIndex)

childIndex で指定された 0 から始める順位で XML を返す

xmlElement.childIndex()

その親の中での XML 値の 0 から始まる順位を返す

xmlElement.children()

要素の子のリストを返す

xmlElement.copy()

指定された要素のコピーを返す

xmlElement.domNode()

xmlElement の org.w3c.dom.Node 表現を返す

xmlElement.innerXML(newContent)

XML 値の全内容を新しい内容で置換する

xmlElement.length

XML 要素のリストの長さを返す

xmlElement.namespaceURI()

xmlElement と関連付けられたネームスペース URI を表す文字列を返す

xmlElement.parent()

要素の親を返す

xmlElement.prependChild(newChild)

XML 値の既存の子の前に新しい子ノードを挿入する

xmlElement.tagName()

要素タグの名前を返す

xmlElement.thisXML

式から返された XML などの現在の XML を指定する

xmlElement.text()

文字列型である xmlElement のすべての XML プロパティの値が含まれた文字列を返す

xmlElement.toString()

要素とその内容を文字列として返す

xmlElement.toXMLString()

xmlElement の XML でコード化された文字列表現を返す

xmlElement.xpath(xPathExpression)

XML 値をコンテキスト ノードとして使用して XPath 式を評価する

このトピックで示された各関数は、次の XML 変数に基づく例を使用して説明されます。

/* Declare an XML variable with a literal XML value. */
var xmlEmployees = <employees>
    <employee id="111111111">
        <firstname>John</firstname>
        <lastname>Walton</lastname>
        <age>25</age>
    </employee>
    <employee id="222222222">
        <firstname>Sue</firstname>
        <lastname>Day</lastname>
        <age>32</age>
    </employee>
</employees>;

xmlElement.appendChild(newChild)

xmlElement の既存の子の後に(newChild で指定された)新しい子要素を挿入します。

次の例では、xmlSue に割り当てられた要素の最後に <hobby> 要素が追加されます。

var xmlSue = xmlEmployees..employee.(firstname == "Sue");
xmlSue.appendChild(<hobby>snorkeling</hobby>);

xmlElement.attribute(attributeName)

attributeName の値を返します。

次の例では、Sue の ID 番号で構成された文字列が返されます。

var xmlEmployeeID = xmlEmployees.employees.employee.(firstname == "Sue").attribute("id");

attribute() 関数は、.@ 演算子の代替機能です。リテラル値ではなく変数を渡すことができるので、attribute() 関数は問題の属性が実行時まで不明の場合に便利です。

.@ 演算子の詳細については、 .@ 演算子で属性にアクセスするを参照してください。

xmlElement.attributes()

xmlElement の属性のリストを返します。

次の例では、<name> 値が「Sue」の <employee> 要素の属性が返されます(戻り値は「id」)。

var suesAttributes = xmlEmployees..employee.(firstname == "Sue").attributes();

xmlElement.child(childIndex)

childIndex で指定された 0 から始める順位の XML を返します。

次の例の 2 番目の行では、Sue に関する情報の含まれた XML 型が返されます。

var secondEmployeeIndex = xmlEmployees..employee.(firstname == "Sue").childIndex();
var secondEmployee = xmlEmployees.employees.child(secondEmployeeIndex);

xmlElement.childIndex()

xmlElement の(その親の中での)順位を返します。

次の例では、<name> 要素値が「John」である <employee> 要素のインデックスが返されます。「0」が返されます。

var johnIndex = xmlEmployees..employee.(firstname == "John").childIndex();

次の例のように、childIndex の左側の式で複数の項目が返される場合は childIndex で -1 が返されます。

var nameIndex = xmlEmployees..employee.name.childIndex();

xmlElement.children()

xmlElement のすべての子が含まれた XMLList を返します。

次の例では、xmlEmployees の最初の employee 要素の子要素が返されます。戻り値は「John, Walton, 25」です。

var xmlEmps = xmlEmployees..employee[0].children();

xmlElement.copy()

xmlElement のディープ コピーを返します。

次の例では、Gladys(新しい従業員)についてのデータが、John と Sue がすでに含まれているリストに追加されます。

結果: newEmployee に保持された XML が従業員のリストに追加されます。

/* Create a new variable from data from the first employee in the list. */
var newEmployee = xmlEmployees..employee[0].copy();
/* Assigned new values to the copy. */
newEmployee.@id = "555555555";
newEmployee.firstname = "Gladys";
newEmployee.lastname = "Cravits";
newEmployee.age = "43";
/* Append the copy to the list of employees. */
xmlEmployees..employees += newEmployee;

xmlElement.domNode()

xmlElement の org.w3c.dom.Node 表現を返します。

ノードは、W3C Document Object Model(DOM)の主要なデータ型です。DOM は、XML(および HTML)ドキュメントにアクセスするための論理構造を定義します。DOM では、ノードがドキュメント ツリーの単一ノードを表します。Xerces は、Apache プロジェクトを通じて利用可能な DOM の Java 実装です。

WebLogic Workshop で提供される ECMAScript の拡張機能は DOM を介して XML にアクセスする代替手段ですが、domNode 関数は DOM ノードを作成する便利な手段として提供されます。これは、たとえばスクリプトで DOM ノードが返される場合に便利です。

Node インタフェースの詳細については、Apache Xalan のマニュアルの Node インタフェースのトピックを参照してください。

xmlElement.innerXML(newContent)

xmlElement の全内容を newContent で置換します。

これは、変数の再割り当てと同じではありません。下の例では、変数(<employee>)で表されたルート要素はそのままで、その内容が置換されます。

次の例では、xmlSue の全内容が指定された要素で置換されます。これにより、Sue の ID 番号を維持したまま Sue が Gladys で効果的に置換されます。

var xmlSue = xmlEmployees..employee.(firstname == "Sue");
xmlSue.innerXML(<><firstname>Gladys</firstname><lastname>Cravits</lastname><age>43</age></>);

xmlList.length

xmlList で表されたリストの項目数を返します。

XMLList データ型の詳細については、XML 変数を作成および使用するを参照してください。

次の例では、xmlEmployees<employee> 要素の数が返されます。戻り値は 2 です。

var numberOfEmployees = xmlEmployees.length;

xmlElement.namespaceURI()

xmlElement と関連付けられたネームスペース URI を表す文字列を返します。

次の例では、SOAP メッセージと関連付けられたネームスペース URI が返されます。戻り値は、「http://schemas.xmlsoap.org/soap/envelope/」を表す文字列です。

var xmlStockMessage = <Envelope xmlns ="http://schemas.xmlsoap.org/soap/envelope/"
    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <Body>
        <m:GetLastTradePrice xmlns:m="http://mycompany.com/stocks">
            <symbol>DIS</symbol>
        </m:GetLastTradePrice>
    </Body>
</Envelope>;
var ns = xmlStockMessage.Envelope.Body.namespaceURI();

xmlElement.parent()

xmlElement の親を返します。

次の例では、年が 30 以下の従業員が検索され、parent 関数を使用してその従業員のデータが含まれている要素全体が返されます。戻り値は、John の情報が含まれている XML 型の要素です。

var ns = xmlEmployees..employee.(age <= "30").age;
var emp = ns.parent();

xmlElement.prependChild(newChild)

xmlElement の既存の子の前に newChild を挿入します。

次の例では、xmlSue に割り当てられた要素の先頭に <prefix> 要素が追加されます。このコードでは、John についての情報が含まれた <employee> ノードの最初の子として <prefix>Mr.</prefix> が追加されます。

var xmlJohn = xmlEmployees..employee.(firstname == "John");


xmlJohn.prependChild(<prefix>Mr.</prefix>);

xmlElement.tagName()

xmlElement のタグの名前を返します。

次の例では、最初の <employee> 要素の最初の子要素の名前が返されます。戻り値は、「firstname」という文字列です。

var childTags = xmlEmployees..employee[0].children();


var firstChildTag = childTags[0].tagName();

xmlElement.thisXML

式から返された XML などの現在の XML を指定します。

詳細については、 thisXML キーワードで現在の XML を指定するを参照してください。

xmlElement.text()

文字列型である xmlElement のすべての XML プロパティの値が含まれた文字列を返します。

次の例では、2 番目の <employee> 要素の 2 番目の子が検索され、その内容が返されます。戻り値は、「Sue」という文字列です。

var ageXML = xmlEmployees..employee[1][1].text();

xmlElement.toString()

XML とその内容を文字列として返します。

次の例では、最初の <employee> 要素の文字列表現が返されます。

var johnText = xmlEmployees..employee[0].toString();

xmlElement.toXMLString()

xmlElement の XML でコード化された文字列表現を返します。

次の例では、最初の <employee> 要素の文字列表現が返されます。

var johnText = xmlEmployees..employee[0].toXMLString();

xmlElement.xpath(xPathExpression)

xmlElement をコンテキスト ノードとして使用して(xpathExpression で指定された) XPath 式を評価します。

次の例では、XPath 式を使用して、<firstname> 要素の値が「Sue」である <employee> 要素が返されます。 戻り値は、要素が含まれた XMLList です。

結果: Sue についての情報が格納された <employee> 要素が返されます。

var xmlSue = xmlEmployees.xpath("//employee[firstname='Sue']")

関連トピック

ECMAScript の拡張機能で XML を処理する

ECMAScript 言語の拡張機能の概要