0

Element オブジェクトを HTMLOption オブジェクトに変換する直接的な方法はありますか? 次の XML があるとします。

<?xml version='1.0'?>
    <options>
        <option value="1">Hello1</option>
        <option value="2">Hello2</option>
    </options>

この選択に各オプションを挿入したい

これらの XML をオプションに直接変換する方法はありますか、または XML をナビゲートして必要なすべての情報を取得し、新しいオプションを作成してそのオプションを選択に追加する必要がありますか? 何かのようなもの:

var options = XmlCode.getElementsByTagName('option');
for(var i = 0; i < options.length; i++){
    selectBox.add(options[i]);
}

ネイティブ コードがいいでしょう ^^ 注: ライブラリやフレームワークは使用したくありません。私は自分でこれを学び、やりたいと思っています。

4

1 に答える 1

1

XSLT は、XML から HTML への変換用に作成されています。このようなものはトリックを行います:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="select2option.xml" ?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml"
            >

<xsl:output method="html" encoding="utf-8" indent="yes" standalone="yes" media-type="text/html" omit-xml-declaration="yes" doctype-system="about:legacy-compat" />

<html:options>
  <html:option name="foo">bar</html:option>
</html:options>

<xsl:template match="xsl:stylesheet">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
 <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  </head>
  <body>
    <xsl:apply-templates/>
  </body>
 </html>
</xsl:template>

<xsl:template match="html:options">
 <select>
  <xsl:apply-templates />
 </select>
</xsl:template>

<xsl:template match="html:option">
 <option name="@name">
  <xsl:apply-templates />
 </option>
</xsl:template>
</xsl:stylesheet>
于 2012-04-25T06:36:51.040 に答える