次の方法で共有


xsl:attribute Element (Windows Embedded CE 6.0)

1/6/2010

Creates an attribute node and attaches it to an output element.

Syntax

<xsl:attribute
  name = "attribute-name"  
  namespace = "uri-reference">
</xsl:attribute>

Attributes

  • name
    [required] Name of the attribute to create. If this value is a Qualified Name, the attribute node is created in the name space currently bound to the prefix unless otherwise overridden by the presence of a namespace attribute. The value of the name attribute is interpreted as an attribute value template (expressions in curly braces are evaluated and converted to strings as in <xsl:value-of>). This allows the name of the attribute to be calculated or obtained from the source XML.
  • namespace
    The name space Uniform Resource Identifier (URI) of the created attribute. If the name attribute contains a Qualified Name, the prefix specified there will be bound to the name space specified in the name space attribute, possibly resulting in the addition of other name space declarations when serializing. This value is interpreted as an attribute value template.

Element Information

Number of occurrences

Unlimited

Parent elements

xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:param, xsl:template, xsl:variable, xsl:when, xsl:with-param

Child elements

xsl:apply-templates, xsl:call-template, xsl:choose, xsl:copy, xsl:copy-of, xsl:for-each, xsl:if, xsl:text, xsl:variable

Remarks

The contents of this element specify the value of the attribute.

Attributes can be added or modified during transformation by placing the <xsl:attribute> element within elements that generate output, such as the <xsl:copy> element. Note that <xsl:attribute> can be used directly on output elements and not only in conjunction with <xsl:element>.

All attributes must be applied before children are added to the element.

Example

This example generates an attribute that obtains its value from the XML source. It generates output in the form <IMG src="value-from-XML-source"/>, using an XPath expression that retrieves the appropriate data from the XML source — here, "imagenames/imagename".

<IMG>
  <xsl:attribute name="src">
    <xsl:value-of select="imagenames/imagename" />
  </xsl:attribute>
</IMG>

This example copies the "myElement" element and adds a "copied" attribute with the value "true".

<xsl:template match="myElement">
  <xsl:copy>
    <xsl:attribute name="copied">true</xsl:attribute>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

The following samples demonstrate how to use attribute value templates to use content in the XML source document as the name of an attribute.

Attribute.xml
<?xml version="1.0"?>
<investment>
  <type>stock</type>
  <name>Microsoft</name>
  <price type="high">100</price>
  <price type="low">94</price>
</investment>
Attribute.xsl
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <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>
XSLT Output (viewed with the XSLT viewer)
<?xml version="1.0" encoding="UTF-16"?>
<stock name="Microsoft" high="100" low="94">
</stock>

The following example shows the use of <xsl:attribute name="{name()}"/>.

Attribute.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Attribute.xsl"?>
<root>
   <bar>bar1</bar>
   <bar>bar2</bar>
   <bar>bar3</bar>
   <bar>bar4</bar>
   <bar>bar5</bar>
</root>
Attribute.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="//bar">
      <xsl:element name="sample">
         <xsl:attribute name="{name()}">
         <xsl:value-of select="."/>
         </xsl:attribute>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>
Output ( Using the XSLT viewer )
<?xml version="1.0" encoding="UTF-16"?>
<sample bar="bar1" />
<sample bar="bar2" />
<sample bar="bar3" />
<sample bar="bar4" />
<sample bar="bar5" />

See Also

Reference

XSLT Elements

Concepts

Accessing and Outputting Attributes
Generating More Sophisticated XML Output
Qualified Names