Share via


<xsl:attribute> の例 2

属性値テンプレートを使用して、XML ソース ドキュメント内のコンテンツを属性の名前として使用する例を次に示します。

XML ファイル (attribute.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="attribute.xsl"?>
<investment>
   <type>stock</type>
   <name>Microsoft</name>
   <price type="high">100</price>
   <price type="low">94</price>
</investment>

XSLT ファイル (attribute.xsl)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>

<xsl:template match="investment">
   <xsl:element name="{type}">
      <xsl:attribute name="name" >
         <xsl:value-of select="name"/>
      </xsl:attribute>
      <xsl:for-each select="price">
      <xsl:attribute name="{@type}" >
         <xsl:value-of select="."/>
      </xsl:attribute>
      </xsl:for-each>
   </xsl:element>
</xsl:template>
</xsl:stylesheet>

出力

書式付き出力はありません。 これはプロセッサ出力です。

<?xml version="1.0"?>

<stock name="Microsoft" high="100" low="94">

</stock>