XSLT - 要素の作成 (xsl:element)

Advertisement

書式

<xsl:element name="要素名">
    要素の内容
</xsl:element>

<xsl:element name="price">
    1000
</xsl:element>
上記の例では <price>1000</price> という要素を作成する。

サンプルコード

sample.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="./style.xsl"?>

<document>
    <book id="10">
        <title>AAA</title>
        <price>4000</price>
    </book>
    <book id="20">
        <title>BBB</title>
        <price>1000</price>
    </book>
    <book id="30">
        <title>CCC</title>
        <price>2000</price>
    </book>
    <book id="40">
        <title>DDD</title>
        <price>1500</price>
    </book>
    <book id="50">
        <title>EEE</title>
        <price>500</price>
    </book>
</document>
style.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="document">
    <html>
    <head>
    <title>XSLT</title>
    </head>
    <body>
    <ul>
    <xsl:apply-templates/>
    </ul>
    </body>
    </html>
</xsl:template>

<xsl:template match="book">
    <xsl:element name="li">
      <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>


</xsl:stylesheet>
生成されるコード
    <html>
    <head>
    <title>XSLT</title>
    </head>
    <body>
    <ul>
    <li>AAA 4000</li>
    <li>BBB 1000</li> 
    <li>CCC 2000</li>
    <li>DDD 1500</li> 
    <li>EEE 500</li>
    </ul>
    </body>
    </html>

Advertisement

ショートカット

634トップページ
このカテゴリのトップページに戻る
634ラボ

サイト検索

Google

Web サイト内

Y!ログール