Mega Code Archive

 
Categories / XML Tutorial / Xpath
 

Attributes can be accessed in similar way as elements

"@" is in the front of attribute names. "@name" matches name attribute of an element. "data/@color" matches color attribute of data element. File: Data.xml <?xml version="1.0"?> <dog name="Joe">   <data weight="18 kg" color="black"/> </dog> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="dog">       <paragraph>         <b>           <xsl:text>Dog: </xsl:text>         </b>         <xsl:value-of select="@name"/>       </paragraph>       <paragraph>         <b>           <xsl:text>Color: </xsl:text>         </b>         <xsl:value-of select="data/@color"/>       </paragraph>     </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><paragraph><b>Dog: </b>Joe</paragraph><paragraph><b>Color: </b>black</paragraph>