XML Tutorial/XPath/attribute
Содержание
- 1 Attributes can be accessed in similar way as elements
- 2 Attributes can be processed in the same way as elements
- 3 Check attribute existance
- 4 Check value of attribute
- 5 for-each select="attribute::*"
- 6 If the element has an attribute
- 7 includes or excludes elements if the specified attribute is present
- 8 Select elements, which contain or do not contain the given attribute
- 9 select="state/@joined"
Attributes can be accessed in similar way as elements
<source lang="xml">
"@" 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> <xsl:text>Dog: </xsl:text> <xsl:value-of select="@name"/> </paragraph> <paragraph> <xsl:text>Color: </xsl:text> <xsl:value-of select="data/@color"/> </paragraph> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><paragraph>Dog: Joe</paragraph><paragraph>Color: black</paragraph></source>
Attributes can be processed in the same way as elements
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?> <employee id="js0034">
Joe Smith
</employee> File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/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"> <xsl:value-of select="."/> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
Joe Smith
[js0034]</source>
Check attribute existance
<source lang="xml">
File: Data.xml <?xml version="1.0" standalone="yes"?> <poem author="jm" year="1667">
<verse>line 1</verse> <verse>line 2</verse> <verse>line 3</verse> <verse>line 4</verse>
</poem> File: Transform.xslt <?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="text" /> <xsl:template match="poem"> <xsl:if test="@author="jm"">1. The poem"s author is jm.</xsl:if> </xsl:template>
</xsl:stylesheet> Output: 1. The poem"s author is jm.</source>
Check value of attribute
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <advertisement action="create">
asdf
</advertisement> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="text" indent="no" /> <xsl:variable select="advertisement/text" name="path" /> <xsl:template match="/"> <xsl:apply-templates select="$path" /> </xsl:template> <xsl:template match="text"> <xsl:apply-templates /> </xsl:template> <xsl:template match="headline"> <xsl:text>\F4</xsl:text> <xsl:value-of select="text()" /> </xsl:template> <xsl:template match="para"> <xsl:if test="self::para[@fontsize="agate"]"> <xsl:text> \F1</xsl:text> <xsl:value-of select="text()" /> </xsl:if> <xsl:if test="self::para[@justify="flushleft"]"> <xsl:text><</xsl:text> </xsl:if> </xsl:template>
</xsl:stylesheet></source>
for-each select="attribute::*"
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<AAA id="a1" pos="start"> <BBB id="b1"/> <BBB id="b2"/> </AAA> <AAA id="a2"> <BBB id="b3"/> <BBB id="b4"/> <CCC id="c1"> <CCC id="c2"/> </CCC> <BBB id="b5"> <CCC id="c3"/> </BBB> </AAA>
File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">
Axis: attribute | |
Element | Node-set |
</xsl:template> <xsl:template name="print"> <tr> <td> <xsl:value-of select="name()"/> <xsl:text> id = </xsl:text> <xsl:value-of select="./@id"/> </td> <td> <xsl:for-each select="attribute::*"> <xsl:if test="not(@id)"> <xsl:value-of select="name()"/> </xsl:if> <xsl:value-of select="./@id"/> <xsl:text/> </xsl:for-each> </td> </tr> </xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>Axis: attribute | |
---|---|
Element</th><td>Node-set</th> |