The methods for accessing attributes are the same as those for accessing elements. Note that the attribute name is preceded by the "@" symbol.
XML source code<Source> <Dog name = "Joe"> <Data Weight = "18 kg" color = "black"/> </Dog> </Source> Output<P> <B> dog: </B> Joe </P> <P> <B> color: </B> black </P> View in HTML Dog:Joe Color:Black |
XSLT stylesheet <XSL: stylesheet version = '1. 0' Xmlns: XSL = 'HTTP: // www.w3.org/5o/#/transform'> <XSL: template match = "dog"> < P > < B > <XSL: Text> dog: </XSL: Text> </ B > <XSL: value-of select = "@ name"/> </ P > < P > <B > <XSL: Text> color: </XSL: Text> </ B > <XSL: value-of select = "Data/@ color"/> </ P > </XSL: Template> </XSL: stylesheet> |
Attributes and elements are processed in the same way.
XSLT stylesheet 1
XML source code<Source> <Employee ID = "js0034"> Joe Smith </employee> </Source> OutputJoe Smith [<B> <I> js0034 </I> </B>] View in HTMLJoe Smith [ Js0034 ] |
XSLT stylesheet <XSL: stylesheet version = '1. 0' Xmlns: XSL = 'HTTP: // www.w3.org/5o/#/transform'> <XSL: template match = "employee"> <XSL: value-of select = "."/> <XSL: Text> [</XSL: Text> <XSL: Apply-templates select = "@ ID"/> <XSL: Text>] </XSL: Text> </XSL: Template> <XSL: template match = "@ ID"> <B> <I> <XSL: value-of select = "."/> </I> </B> </XSL: Template> </XSL: stylesheet> |
You can also select an element by checking whether it contains certain attribute values. XSLT stylesheet 1 is selected, while XSLT stylesheet 2 exclude elements that contain specific attributes.
XSLT stylesheet 1
XML Source Code output Car: a234 Car: a111 View in HTML Car: a234 Car: a111 |
XSLT stylesheet xmlns: XSL = 'HTTP: // www.w3.org/5o/#/transform'> P Car: P |
XSLT stylesheet 2
XML Source Code output Car: a005 View with HTML Car: a005 |
XSLT stylesheet xmlns: XSL = 'HTTP: // www.w3.org/5o/#/transform'> P Car: P |