此範例將說明如何使用屬性值範本,以 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>